没有边框的python matplotlib表

时间:2015-12-03 15:16:51

标签: python matplotlib

我在下面的示例生成的表格顶部有一个绘图。表格数据被随机数替换,实际图表被一些仲裁函数取代:

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()

我的问题是:是否有可能删除表格边框?或者,我会将颜色更改为浅灰色并应用虚线样式。

3 个答案:

答案 0 :(得分:9)

如果要从每个表格单元格中删除边框,可以将其添加到每个表格单元格的现有循环中:

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

这允许您使用例如更改每个单元格的所有属性。 set_linestyle()set_edgecolor()set_fontsize()等等:

enter image description here

答案 1 :(得分:0)

“如果在对表的调用中将edge ='open'设置为,则无需擦洗边框” -MD004 https://stackoverflow.com/a/56515764/927972

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

答案 2 :(得分:-1)

试试这个:

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

这对你有用吗?