Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Colormaps

DS 2023 | Communicating with Data

Overview

In Matplotlib, a colormap is used to map scalar data values to color values.

Colors are used to replace numbers in a visualization to display quantitive meaning.

Data are usually normalized to a scale between 0 and 1 and then mapped onto colors represented using the RGBA encoding scheme.

Use cases

Colormaps are used everywhere in visualization.

Nearly every plot type in Matplotlib has a paramter to define color.

Colormaps are especially useful for visualizing 2D or 3D data arrays in plots like heatmaps, surface plots, scatter plots, and contour maps.

Representing color as RGBA

RGBA is a color encoding scheme that defines a color in terms of four channels: Red, Green, Blue, and Alpha.

Each color channel may have a value between 0 and 255, yielding 2563 or 16,777,21616{,}777{,}216 colors.

In this way, every color is defined as a mixture of red, green, and blue.

The alpha channel determines the opacity of a color, ranging from 0.0 to 1.0.

Color codes and names

Here is a table featuring showing some common colors and their codes:

Color NameDecimal Value (R, G, B)Hex CodeSwatch
Red255, 0, 0#FF0000██
Green0, 255, 0#00FF00██
Blue0, 0, 255#0000FF██
Yellow255, 255, 0#FFFF00██
Cyan0, 255, 255#00FFFF██
Magenta255, 0, 255#FF00FF██
Orange255, 165, 0#FFA500██
Purple128, 0, 128#800080██
White255, 255, 255#FFFFFF██
Black0, 0, 0#000000██

Palettes and their uses

Matplotlib comes with a number of colormaps to choose from.

These are grouped into five primary classes based on their mathematical structures and how human eyes perceive them.

Perceptually Uniform

  • General scientific data, accurate perception.

  • Lightness increases monotonically; converts flawlessly to grayscale.

Sequential

  • Ordered data moving from low to high values.

  • Smooth variation between two color tones (e.g., low saturation to high saturation).

Diverging

  • Data with a critical midpoint or zero value.

  • Two divergent hues that meet at an unsaturated, neutral middle color.

Cyclic

  • Wrapping values or periodic data.

  • Start and end colors match smoothly; ideal for phase angles, wind directions, or hourly time.

Qualitative

  • Unordered or categorical classes, as with nominal grouping data.

  • Distinct, miscellaneous hues with non-monotonic lightness to distinguish discrete sets.

Matplotlib’s colormap object

Sometimes it is necessary to access a colormap object, as opposed to just passing its name to the cmap argument of a plotting function.

To do this, you can use PyPlot’s colormaps object.

Here we call the object and show all the colormap names it has.

Loading...

Let’s look at one:

256
Loading...

You can pass a scaled number to the object to get its RGBA values.

(np.float64(0.127568), np.float64(0.566949), np.float64(0.550556), np.float64(1.0))

You can also pass it a number between 0 and NN, the number of values in the colormap.

You can get the number of values in the colormap by appending .N to the object, like so:

256

And here we acces the color at 233.

(np.float64(0.772852), np.float64(0.877868), np.float64(0.131109), np.float64(1.0))

The return value can be converted to a code suitable for using in HTML.

'#21918c'

Here is an example of this code being used to color some text.

<span style="color:#21918c;font-size:64px;">&block;&block; Here is an example.</span>

██ Here is an example.

Putting colormaps to work

Let’s put the colormap to work.

In the bad old days, Pandas’ scatter plot function did not allow you to pass a variable name to the c argument.

To get that effect, you had to do it by hand.

Tedious as it was, it illustrated how colormaps can be used.

This is useful knowledge in other contexts.

Let’s look at an example.

Let’s create a scatter plot of two variables from the iris dataset and color them by species.

Loading...

To this, we first generate a number to go with each member of the species domain.

If the variable is cast as a Categorical data type, this is easy: we just grab the codes associated with the elements (levels) of the category.

0 0 1 0 2 0 3 0 4 0 .. 145 2 146 2 147 2 148 2 149 2 Length: 150, dtype: int8

Now all we do is pass the series of codes to the c argument.

We also specify a colormap with cmap.

Here we choose on from the qualitative group.

<Figure size 640x480 with 2 Axes>

The result is not ideal because the colorback represents the number as a continuous variable, and does not show the relationship between color and category.

To get the proper result, we turn off the colorbar and add a legend.

<Figure size 640x480 with 1 Axes>

Again, this is tedious, but you can see how colormaps are used.

They take a numeric represenation of variable and return a color that can be used for visualization.

Color names

Finally, here are some of the color names available to use in Matplotlib whenever you are asked to define a color for some visual feature.

For example, you may want to define the color of a line as magenta.

Matplotlib comes with a few libraries of colors:

  • BASE_COLORS

  • CSS4_COLORS

  • TABLEAU_COLORS

  • XKCD_COLORS

The names and their color values are available as dictionaries from the mpl.colors object.

For example:

{'b': (0, 0, 1), 'g': (0, 0.5, 0), 'r': (1, 0, 0), 'c': (0, 0.75, 0.75), 'm': (0.75, 0, 0.75), 'y': (0.75, 0.75, 0), 'k': (0, 0, 0), 'w': (1, 1, 1)}
{'tab:blue': '#1f77b4', 'tab:orange': '#ff7f0e', 'tab:green': '#2ca02c', 'tab:red': '#d62728', 'tab:purple': '#9467bd', 'tab:brown': '#8c564b', 'tab:pink': '#e377c2', 'tab:gray': '#7f7f7f', 'tab:olive': '#bcbd22', 'tab:cyan': '#17becf'}

Let’s create a function to display a color given its name.

<Figure size 200x200 with 1 Axes>

Here is a function to show all the colors in a collection.

<Figure size 400x220 with 8 Axes>
<Figure size 500x220 with 10 Axes>
<Figure size 800x2090 with 152 Axes>

The XKCD_COLORS dictionary has too many values to print, so we show a sample from it instead.

949
<Figure size 800x880 with 64 Axes>