簡體   English   中英

python matplotlib表沒有邊框

[英]python matplotlib table without borders

我在以下示例生成的表格頂部繪制了一個圖。 表數據被隨機數替換,實際圖被一些任意函數替換:

import numpy as np
import matplotlib.pylab as plt

fig, ax = plt.subplots()

ntp = 17 # number of peak periods
nhs = 30 # number of wave heights

scatter_table = np.random.randint(0,10,(nhs,ntp)) # wave scatter diagram

tp = np.linspace(3,20,ntp+1) # peak period array
hs = np.linspace(0,15,nhs+1) # significant wave height

# axis limits to be in line with scatter diagram
ax.set_xlim((min(tp),max(tp)))
ax.set_ylim((min(hs),max(hs)))

# axis ticks as per scatter table bins
ax.set_xticks(tp)
ax.set_yticks(hs)

# matplotlib table
the_table = plt.table(cellText=scatter_table,loc=(0,0),cellLoc='center')

# change table properties to match plot window
table_props = the_table.properties()
table_cells = table_props['child_artists']
for cell in table_cells: 
    cell.set_height(1/float(nhs))
    cell.set_width(1/float(ntp))

# plot!
ax.plot(tp,4+0.2*tp+np.sin(tp)*0.25*tp)

plt.grid()
plt.show()

我的問題是:是否有可能刪除表格邊框? 或者,我會將顏色更改為淺灰色並應用虛線樣式。

如果要從每個表格單元格中刪除邊框,可以將其添加到每個表格單元格的現有循環中:

for key, cell in tab.get_celld().items():
    cell.set_linewidth(0)

這允許您使用例如set_linestyle()set_edgecolor()set_fontsize()等更改每個單元格的所有屬性:

在此處輸入圖片說明

“如果您在對表的調用中設置了 edge='open',則不需要擦洗邊框”-MD004 https://stackoverflow.com/a/56515764/927972

ax.table(...,edges='open')

嘗試這個:

[i.set_linewidth(0.1) for i in ax.spines.itervalues(0)]

這對你有用嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM