8

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.

Herpes Free Engineer
  • 2,425
  • 2
  • 27
  • 34

2 Answers2

4

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.

JE_Muc
  • 5,403
  • 2
  • 26
  • 41
3

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

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • I really wished to be able accept this answer as well. Yet to respect the previous author's immediate help, I feel to keep it as is. Instead, I upvoted a handful of other questions-answers from you to show my appreciation, if it is legal. Many thanks. – Herpes Free Engineer Jul 13 '18 at 07:08
  • You're free to accept any answer you feel answers your question. Your logic about which one to choose seems reasonable, so don't worry about it. *"I upvoted a handful of other questions-answers from you to show my appreciation"* While I appreciate your thankfulness, this is not how the voting system should be used. Upvote questions and answers judging on their content, not on the user who wrote them. [This is what might happen now](https://meta.stackoverflow.com/questions/351543/reputation-capped-after-serial-voting). – ImportanceOfBeingErnest Jul 13 '18 at 08:05
  • Many thanks for your comments. Although I was aware of the flaw of this upvoting reasoning, it has also another reasoning: I kindly indicate that roughly 3.4 million times users reached questions, and benefited from your work. However, the upvotes you (or others) received is less than few percents. I almost always find this unfair and imho, the upvoting process needs to be improved. That's the reason why I do aggressively guerilla-upvote people who help others. If those wont hurt you, I would like to keep them as is. Otherwise, I can revert them. Many thanks. – Herpes Free Engineer Jul 13 '18 at 11:37