Color in Matplotlib

Adrian Garrido
4 min readJul 16, 2019

Color (ˈkələr): “Phenomenon of light or visual perception that enables one to differentiate otherwise identical objects”. (source)

Color is one of the most important elements and it is one of the first things anyone notices when looking at a chart. Colors should guide the viewers through your data. It can be used make clear distinctions between the things that are related and the things that are not.

Luckily for us, there are virtually an infinite amount of colors you can use in Matplotlib.

In order to change the color of your graph, add the argument “color = “ or alternatively you can simply use “c =”, like this:

plt.bar([1,2,3,4,5,6,7,8], [9, 2, 3, 7, 1, 3, 5, 1], color = 'green');
Example of a bar chart, using color = ‘green’

Now there are a few things we can put after “color =”, other than just basic colors like blue, red, or green.

If you are in a hurry, you can always use the abbreviated versions of basic colors:

{'b', 'g', 'r’, ‘c', ‘m', 'y', 'k', 'w'}

These stand for blue, green, red, cyan, magenta, yellow, black, and white, in that order.

There are a couple of already established color lists available.

First we have the X11 list:

X11 color list

Again, you can simply throw any of these after “color =” in quotation marks.

The next list you can use is called xkcd. This list was created in 2010 through crowd-sourcing. Around 40,000 women and 100,000 men participated, assigning names to each color they were given. If you want to learn more about this cool experiment you can do so here.

In order to use one of these colors you will have to add “xkcd:” before the color. Like this:

plt.pie([list1, list2, list3], labels = ['Red %', 'Green %', 'Blue %'], colors = ['xkcd:grapefruit', 'xkcd:pastel green', 'xkcd:robin\'s egg blue'], autopct='%.1f%%')
Example of a pie chart, using xkcd colors

Make sure there is no space between “xkcd:” and your color, otherwise you will get a ValueError.

Here are some of the colors you can find in this list:

You can find the full xkdc list of 954 colors here.

In addition to all of these you can also use an HTML hex string directly or use a tuple with the corresponding RGB values. Here is a website you can play with, that has a gradient chart, allowing you to choose any color and its HTML color code and RGB values:

Finally, you can also use palettes if you are using a graph that calls for those, for example, if you are using a heatmap.

Palettes are divided into 3 main categories, sequential, diverging and qualitative.

Sequential
Diverging / Qualitative / Miscellaneous

Here are some examples of palettes being used

plt.figure(figsize = (8, 12))
sns.heatmap(ListofSeries.corr()Series.sort_values(Series, ascending = False),
annot = True,
cmap = 'viridis_r', vmin = -1);
Example of a heatmap showing correlations between series, using a sequential palette
sns.factorplot(x = Series1, y = Series2, hue = Series3, size = 20, kind = 'swarm', data = train);
Example of a factorplot, showing relationship between 3 series, using a qualitative palette

Remember that color is one of the first things people notice in data visualization. So make sure to throw some colors in your charts, to make them stand out, and to help you tell the story you want to tell with your data.

--

--