Python Matplotlib无边框表格

6

我有一个表格上的图表,如下面的示例所示。 表格数据由随机数字替换,实际绘图由某些任意函数替换:

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

我想问一下:是否有可能去掉表格的边框?或者,我可以将颜色改成浅灰色,并应用虚线样式。


你是指每个表格单元的边框,还是只是表格的外部边框(轴)? - Bart
3个回答

16
如果你想要去除每个表格单元格的边框,可以将以下代码添加到您现有的循环遍历每个表格单元格中:

如果你想要去除每个表格单元格的边框,可以将以下代码添加到您现有的循环遍历每个表格单元格中:

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

这使您能够使用 set_linestyle()set_edgecolor()set_fontsize() 等来更改每个单元格的所有属性:

输入图像描述


4

太简单了。从文档中无法理解它是如何工作的。https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.table.html 要用线绘制的单元格边缘。另请参见visible_edges。不够描述性。 - Marc Maxmeister

-2

试试这个:

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

这个对你有用吗?


您的回复是从这里复制的,似乎适用于Pandas。您能否更具体地说明如何使用它来解决我的问题? - some_weired_user
@Simon 我改变了我的回答!实际上,我没有从其他帖子复制它。看起来好像我们两个都是从一个网站复制的。 >.< - babyguineapig11

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接