Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python matplotlib table without borders

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.

like image 828
some_weired_user Avatar asked Dec 03 '15 15:12

some_weired_user


People also ask

How do I get rid of gridlines in MatPlotLib?

MatPlotLib with Python Convert the image from one color space to another. To remove grid lines, use ax. grid(False).

How do I make plot transparent in MatPlotLib?

Matplotlib allows you to regulate the transparency of a graph plot using the alpha attribute. By default, alpha=1. If you would like to form the graph plot more transparent, then you'll make alpha but 1, such as 0.5 or 0.25.


1 Answers

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

like image 95
Bart Avatar answered Sep 30 '22 10:09

Bart