A beginner’s guide to colormaps in matplotlib

Ethan Kelly
4 min readSep 20, 2021

--

Using the right color map for the right data type helps clarify and deepen meaning. After having taken my first steps into data science, these are the initial concepts I feel are most helpful for someone starting off. Here, I’ll go over the thought process of selecting some of the most common color maps, and some thoughts to keep in mind when selecting them. This is more of a conceptual article; for technical nitty gritty I would point to the matplotlib and seaborn documentation. All the below color maps are pulled from matplotlib. Links to matplotlibs tutorial are found below.

Qualitative Color maps

All the colors in a quantitative map have similar visual weight and are equally spaced out. These maps give each classification an equivalent importance or weight. Spacing them out equivalently along hue separates each class from the other. It also avoids denoting the classes as ordinal by not varying weight too much and by providing enough space between values so that they are read as distinct.

Sequential color maps

These color maps are a gradient, varying in lightness and sometimes color. This continuous change in lightness maps well to data that sits along a number line. This includes discrete or continuous number data, as well as ordinal categorical data. With the relatively constant increase in lightness in these maps, these color scales create the same perception that a linear axis (vs log, for example) would create. These scales don’t have any inflection point within them, so it’s difficult to denote the location of 0 and negative or positive values. If the direction from an inflection point such as 0 is important, it would be best to look for a diverging colormap instead.

When considering what sequential map you want to use, one thing to keep in mind is how wide the lightness range is. The depth of the dark end of the scale may be a dark gray or black. Same with the top of the scale. This range defines how people perceive the distance from the bottom and top of the scale. A wider range can also help a reader differentiate values.

One limitation of some sequential maps, particularly the mono-hue maps, is that they can become washed out in the lighter colors, yet also dominate your attention with the vibrant darker colors. In other words, the map is not perceptually uniform. The reader may interpret the difference between two lighter colors differently than two darker colors, and could also let the darker colors dominate their attention. To avoid this problem the perceptually uniform sequential color maps have been developed. These scales help the reader evaluate the color change in a more balanced and linear way.

Diverging Color Maps

These maps have a neutral value in the center and two different color gradients on each side. As with sequential maps, they work well for ordinal categorical data, or continuous numerical data. With the neutral value, these maps are able to mark an inflection point such as 0. The two directions are also called out with separate colors, allowing both direction from inflection and magnitude to be characterized. Common use cases are correlation heatmaps and election results.

And More…

These are just a select few types of colormaps I’ve found to be most useful starting off. There’s much more that can be done by combining different types or varying how a map moves through lightness. Understanding the connotations of certain colors in certain contexts can help with being mindful of introducing intuitive understanding and avoid introducing misconceptions. Choosing colors with color blindness in mind can help create more inclusive data visualizations. I hope this has helped you on your journey to more understandable graphs and deeper data analysis.

Resources

--

--

Ethan Kelly

Data scientist, science nerd, always excited to learn new things