python matplotlib table without borders

2020-07-18 09:21发布

问题:

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.

回答1:

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:



回答2:

"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')


回答3:

Try this:

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

Does this work for you?