【问题标题】:python matplotlib table without borderspython matplotlib表无边框
【发布时间】:2016-03-08 06:39:14
【问题描述】:

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

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

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

【问题讨论】:

  • 您是指每个表格单元格的边框,还是只指表格的外边框(轴)?

标签: python matplotlib


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    “如果您在对表格的调用中设置 edges='open',则不需要擦边” -MD004 https://stackoverflow.com/a/56515764/927972

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

    【讨论】:

    【解决方案3】:

    试试这个:

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

    这对你有用吗?

    【讨论】:

    • 您的回复是从here 复制而来的,似乎适用于Pandas。您能否更具体地说明如何使用它来解决我的问题?
    • @Simon 我改变了答案!实际上,我没有从其他线程中复制它。好像我们俩都是从网站上复制的。 >.
    猜你喜欢
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2017-05-27
    • 2019-04-12
    • 2011-08-17
    • 2013-09-07
    • 1970-01-01
    相关资源
    最近更新 更多