python matplotlib table without borders

2020-07-18 09:17发布

I have a plot on top of a table as generated by following example. Table data is replaced by random numbers and the actual plot is replaced by some arbitryry function:

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

My question is: Is there a possibility to remove the table borders? Alternatively, I would change the color to a lighter grey and apply a dashed line style.

3条回答
萌系小妹纸
2楼-- · 2020-07-18 09:40

Try this:

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

Does this work for you?

查看更多
混吃等死
3楼-- · 2020-07-18 09:44

If you want to remove the border from each table cell, you could add this to your existing loop over each table cell:

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

This allows you to change all properties of each cell using e.g. set_linestyle(), set_edgecolor(), set_fontsize(), etc:

enter image description here

查看更多
冷血范
4楼-- · 2020-07-18 09:48

"Scrubbing borders is not needed if you set edges='open' in the call to table" -MD004 https://stackoverflow.com/a/56515764/927972

ax.table(...,edges='open')
查看更多
登录 后发表回答