Herpes Free Engineer
Herpes Free Engineer

Reputation: 2652

How to change the transparency/opaqueness of a Matplotlib Table?

Aim: To make a matplotlib.pyplot.table opaque, so that the major and minor grid lines of a background plot do not appear within the foreground table.

Problem: I could not operate either matplotlib.pyplot.table kwarg alpha or matplotlib.artist.Artist.set_alpha function correctly to change the transparency of a table plotted within a plot.

MWE: Please consider the following code example given as an answer for the question: How can I place a table on a plot in Matplotlib?

import matplotlib.pylab as plt

plt.figure()
ax=plt.gca()
plt.grid('on', linestyle='--')
y=[1,2,3,4,5,4,3,2,1,1,1,1,1,1,1,1]
col_labels=['col1','col2','col3']
row_labels=['row1','row2','row3']
table_vals=[[11,12,13],[21,22,23],[31,32,33]]
#
the_table = plt.table(cellText=table_vals,
                  colWidths = [0.1]*3,
                  rowLabels=row_labels,
                  colLabels=col_labels,
                  loc='center right')
plt.plot(y)
plt.show()

which produces the following:

matplotlib-table

Attempts:

I tried to get rid of the background grid lines within the table by adding alpha keyword into plt.table:

the_table = plt.table(cellText=table_vals,
                  colWidths = [0.1]*3,
                  rowLabels=row_labels,
                  colLabels=col_labels,
                  loc='center right',
                  alpha=1.0)

and then by calling set_alpha:

the_table.set_alpha(1.0)

None of them resolved the issue or raised an error.

Upvotes: 9

Views: 9607

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339200

To set the alpha of a table you need to set the alpha of each cell. Unfortunately, the alpha argument to the table itself is ignored (the reason it is there in the first place is simply that the table accepts all arguments that any matplotlib.artist.Artist accepts, but does not make use of them all.)

To set the cell alpha use e.g.:

for cell in the_table._cells:
    the_table._cells[cell].set_alpha(.5)

Of course this would only make sense if you first made sure the table is above the gridlines. This can be done using the zorder argument - the higher the zorder, the more in front the table appears. Gridlines have by default zorder 1, so any number above 1 would do.

the_table = plt.table(...,  zorder=2)

To see the effect of alpha one may colorize the table, e.g. in blue. Complete example:

import matplotlib.pylab as plt

plt.figure()
ax=plt.gca()
plt.grid('on', linestyle='--')
y=[1,2,3,4,5,4,3,2,1,1,1,1,1,1,1,1]
col_labels=['col1','col2','col3']
row_labels=['row1','row2','row3']
table_vals=[[11,12,13],[21,22,23],[31,32,33]]
#
colors = [["b"]*3 for _ in range(3)]
the_table = plt.table(cellText=table_vals,
                  colWidths = [0.1]*3,
                  rowLabels=row_labels,
                  colLabels=col_labels,
                  loc='center right', zorder=2, 
                  cellColours=colors)

for cell in the_table._cells:
    the_table._cells[cell].set_alpha(.7)

plt.plot(y)
plt.show()

enter image description here

Upvotes: 4

JE_Muc
JE_Muc

Reputation: 5774

alpha does afaik not work for tables, but you can change the zorder:

the_table = plt.table(cellText=table_vals,
                  colWidths = [0.1]*3,
                  rowLabels=row_labels,
                  colLabels=col_labels,
                  loc='center right', zorder=3)

In matplotlib.axes.Axes.table the keyword argument alpha is mentioned. But it seems to have no effect.
Changing the zorder (the vertical order of the elements) makes the table appear on top of your plots. This does not allow semi-opaque tables, but is at least a work-around to make the grid-lines disappear.

Upvotes: 5

Related Questions