Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grid appearing inside table in matplotlib/pyplot graph

I'm trying to add a data summary table to a plot I'm creating like so:

import matplotlib.pyplot as plt

plt.figure()
plt.plot([0,2], [0,2])
plt.grid('on')

values = [[0,1],[2,3]]
rowLabels = ['row1', 'row2']
colLabels = ['col1', 'col2']

table = plt.table(cellText=values, colWidths=[0.1]*3, rowLabels = rowLabels, 
      colLabels=colLabels, loc = 'center right')

But where the grid and the table overlap the grid is still visible which makes the table hard to read.

So I tried adding this code to set the background of the table cells to white and opaque like so:

cells = table.get_celld()

for cell in cells:
    cells[cell].set_facecolor('white')
    cells[cell].set_alpha(1)

But it doesn't change. If I use a color other than white I can see it is setting the cell color but the grid is still visible, which makes me think that the grid is being drawn on top of the table rather than behind it.

Anybody know of an approach to get the table on a graph with a grid but keep the grid out of the table?

Thanks!

Using current latest matplotlib: version 1.3.1

like image 633
MattskECE Avatar asked Dec 20 '13 17:12

MattskECE


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


1 Answers

try this:

table.set_zorder(100)

from docs:

Artists with lower zorder values are drawn first.

So a high zorder will bring up the artist.

like image 105
behzad.nouri Avatar answered Oct 11 '22 17:10

behzad.nouri