Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 858
Herpes Free Engineer Avatar asked Jul 12 '18 15:07

Herpes Free Engineer


People also ask

How do I change the transparency in Matplotlib?

Matplotlib allows you to adjust the transparency of a graph plot using the alpha attribute. If you want to make the graph plot more transparent, then you can make alpha less than 1, such as 0.5 or 0.25. If you want to make the graph plot less transparent, then you can make alpha greater than 1.

How do I make markers transparent in Matplotlib?

How to make the marker face color transparent without making the line transparent in Matplotlib? Create x_data and y_data(sin(x_data)), using numpy. Plot curve using x_data and y_data, with marker style and marker size. By changing the alpha, we can make it transparent to opaque.

Which parameter should you use to change the plots transparency?

Inmatplotlib, the plots have a parameter, alpha= to change transparency.


2 Answers

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.

like image 175
JE_Muc Avatar answered Oct 28 '22 10:10

JE_Muc


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

like image 23
ImportanceOfBeingErnest Avatar answered Oct 28 '22 09:10

ImportanceOfBeingErnest