Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign specific colors to specific cells in a Matplotlib table?

Following the pylab_examples, I have created a simple 2x5 cells table in matplotlib.

Code:

# Prepare table
columns = ('A', 'B', 'C', 'D', 'E')
rows = ["A", "B"]
cell_text = [["1", "1","1","1","1"], ["2","2","2","2","2"]]
# Add a table at the bottom of the axes
ax[4].axis('tight')
ax[4].axis('off')
the_table = ax[4].table(cellText=cell_text,colLabels=columns,loc='center')

Now, I want to color cell A1 with color = "#56b5fd" and cell A2 with color = "#1ac3f5". All other cells should remain white. Matplotlib's table_demo.py as well as this example only show me how to apply a color map with pre-defined colors that depend on the values in the cell.

How to assign specific colors to specific cells in a Matplotlib-generated table?

like image 570
sudonym Avatar asked Oct 10 '17 10:10

sudonym


People also ask

How do I specify colors in matplotlib?

The usual way to set the line color in matplotlib is to specify it in the plot command. This can either be done by a string after the data, e.g. "r-" for a red line, or by explicitely stating the color argument.

Can we select Colours of the visualization using matplotlib?

With matplotlibYou can pass plt. scatter a c argument, which allows you to select the colors.

What is the function to give color to plot in matplotlib?

pyplot. colors() This function is used to specify the color.

How do I change the color of a column in Matplotlib?

matplotlib.pyplot.table gives you already the tool to do what you need: If you want all the cells in a column to have a specific colour you can do something like this and then call plt.table (although I suggest you to change to ax.table) adding the cellColours=cellcolours keyword.

What do the colors mean on a Matplotlib plot?

"Red", "Green", and "Blue" are the intensities of those colors. In combination, they represent the colorspace. Matplotlib draws Artists based on the zorder parameter. If there are no specified values, Matplotlib defaults to the order of the Artists added to the Axes. The alpha for an Artist controls opacity.

What does CN mean in Matplotlib?

"CN" color spec where 'C' precedes a number acting as an index into the default property cycle. Matplotlib indexes color at draw time and defaults to black if cycle does not include color. "Red", "Green", and "Blue" are the intensities of those colors.

How do you highlight cells in a table in Matplotlib?

With tables, we can highlight the desired cells by making them different from others in size, weight, color, shape, spacing, or borders. This article expands on my earlier article, “Simple Little Tables with Matplotlib,” by providing a set of techniques we can use to highlight cells within a Matplotlib table.


2 Answers

The easiest way to colorize the background of cells in a table is to use the cellColours argument. You may supply a list of lists or an array with the same shape as the data.

import matplotlib.pyplot as plt
# Prepare table
columns = ('A', 'B', 'C', 'D', 'E')
rows = ["A", "B"]
cell_text = [["1", "1","1","1","1"], ["2","2","2","2","2"]]
# Add a table at the bottom of the axes
colors = [["#56b5fd","w","w","w","w"],[ "#1ac3f5","w","w","w","w"]]

fig, ax = plt.subplots()
ax.axis('tight')
ax.axis('off')
the_table = ax.table(cellText=cell_text,cellColours=colors,
                     colLabels=columns,loc='center')

plt.show()

enter image description here

Alternatively, you can set the facecolor of a specific cell as

the_table[(1, 0)].set_facecolor("#56b5fd")
the_table[(2, 0)].set_facecolor("#1ac3f5")

Resulting in the same output as above.

like image 129
ImportanceOfBeingErnest Avatar answered Oct 10 '22 02:10

ImportanceOfBeingErnest


@ImportanceOfBeingErnest provided an excellent answer. However, for earlier versions of Matplotlib, the second approach:

the_table[(1, 0)].set_facecolor("#56b5fd")

will result in a TypeError: TypeError: 'Table' object has no attribute '__getitem__' The TypeError can be overcome by using the following syntax instead:

the_table.get_celld()[(1,0)].set_facecolor("#56b5fd")
the_table.get_celld()[(2,0)].set_facecolor("#1ac3f5")

See also this example.

(Confirmed on Matplotlib 1.3.1)

Sample plot

like image 38
DaveL17 Avatar answered Oct 10 '22 02:10

DaveL17