color pallets Choosing Colormaps in Matplotlib — Matplotlib 3.9.2 documentation
color pallets Choosing Colormaps in Matplotlib — Matplotlib 3.9.2 documentation
Overview
The idea behind choosing a good colormap is to find a good representation in 3D colorspace for
your data set. The best colormap for any given data set depends on many things including:
For many applications, a perceptually uniform colormap is the best choice; i.e. a colormap in
which equal steps in data are perceived as equal steps in the color space. Researchers have found
that the human brain perceives changes in the lightness parameter as changes in the data much
better than, for example, changes in hue. Therefore, colormaps which have monotonically
increasing lightness through the colormap will be better interpreted by the viewer. Wonderful
examples of perceptually uniform colormaps can be found in the Third-party colormaps section
as well.
Color can be represented in 3D space in various ways. One way to represent color is using
CIELAB. In CIELAB, color space is represented by lightness, L∗; red-green, a∗; and yellow-blue, b∗.
The lightness parameter L∗ can then be used to learn more about how the matplotlib colormaps
will be perceived by viewers.
An excellent starting resource for learning about human perception of colormaps is from [IBM].
Classes of colormaps
Colormaps are often split into several categories based on their function (see, e.g., [Moreland]):
1. Sequential: change in lightness and often saturation of color incrementally, often using a
single hue; should be used for representing information that has ordering.
2. Diverging: change in lightness and possibly saturation of two different colors that meet in
the middle at an unsaturated color; should be used when the information being plotted has
a critical middle value, such as topography or when the data deviates around zero.
3. Cyclic: change in lightness of two different colors that meet in the middle and beginning/end
at an unsaturated color; should be used for values that wrap around at the endpoints, such
as phase angle, wind direction, or time of day.
4. Qualitative: often are miscellaneous colors; should be used to represent information which
does not have ordering or relationships.
First, we'll show the range of each colormap. Note that some seem to change more "quickly"
than others.
cmaps = {}
# Turn off *all* ticks & spines, not just the ones with colormaps.
for ax in axs:
ax.set_axis_off()
Sequential
For the Sequential plots, the lightness value increases monotonically through the colormaps. This
is good. Some of the L∗ values in the colormaps span from 0 to 100 (binary and the other
grayscale), and others start around L∗ = 20. Those that have a smaller range of L∗ will
accordingly have a smaller perceptual range. Note also that the L∗ function varies amongst the
colormaps: some are approximately linear in L∗ and others are more curved.
Sequential2
Many of the L∗ values from the Sequential2 plots are monotonically increasing, but some
(autumn, cool, spring, and winter) plateau or even go both up and down in L∗ space. Others
(afmhot, copper, gist_heat, and hot) have kinks in the L∗ functions. Data that is being
represented in a region of the colormap that is at a plateau or kink will lead to a perception of
banding of the data in those values in the colormap (see [mycarta-banding] for an excellent
example of this).
plot_color_gradients('Sequential (2)',
['binary', 'gist_yarg', 'gist_gray', 'gray', 'bone',
'pink', 'spring', 'summer', 'autumn', 'winter', 'cool',
'Wistia', 'hot', 'afmhot', 'gist_heat', 'copper'])
Diverging
For the Diverging maps, we want to have monotonically increasing L∗ values up to a maximum,
which should be close to L∗ = 100, followed by monotonically decreasing L∗ values. We are
looking for approximately equal minimum L∗ values at opposite ends of the colormap. By these
measures, BrBG and RdBu are good options. coolwarm is a good option, but it doesn't span a
wide range of L∗ values (see grayscale section below).
plot_color_gradients('Diverging',
['PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu',
'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic'])
Cyclic
For Cyclic maps, we want to start and end on the same color, and meet a symmetric center point
in the middle. L∗ should change monotonically from start to middle, and inversely from middle
to end. It should be symmetric on the increasing and decreasing side, and only differ in hue. At
the ends and middle, L∗ will reverse direction, which should be smoothed in L∗ space to reduce
artifacts. See [kovesi-colormaps] for more information on the design of cyclic maps.
The often-used HSV colormap is included in this set of colormaps, although it is not symmetric to
a center point. Additionally, the L∗ values vary widely throughout the colormap, making it a poor
choice for representing data for viewers to see perceptually. See an extension on this idea at
[mycarta-jet].
Qualitative
Qualitative colormaps are not aimed at being perceptual maps, but looking at the lightness
parameter can verify that for us. The L∗ values move all over the place throughout the colormap,
and are clearly not monotonically increasing. These would not be good options for use as
perceptual colormaps.
plot_color_gradients('Qualitative',
['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2',
'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b',
'tab20c'])
Miscellaneous
Some of the miscellaneous colormaps have particular uses for which they have been created. For
example, gist_earth, ocean, and terrain all seem to be created for plotting topography
(green/brown) and water depths (blue) together. We would expect to see a divergence in these
colormaps, then, but multiple kinks may not be ideal, such as in gist_earth and terrain. CMRmap
was created to convert well to grayscale, though it does appear to have some small kinks in L∗.
cubehelix was created to vary smoothly in both lightness and hue, but appears to have a small
hump in the green hue area. turbo was created to display depth and disparity data.
The often-used jet colormap is included in this set of colormaps. We can see that the L∗ values
vary widely throughout the colormap, making it a poor choice for representing data for viewers
to see perceptually. See an extension on this idea at [mycarta-jet] and [turbo].
plot_color_gradients('Miscellaneous',
['flag', 'prism', 'ocean', 'gist_earth', 'terrain',
'gist_stern', 'gnuplot', 'gnuplot2', 'CMRmap',
'cubehelix', 'brg', 'gist_rainbow', 'rainbow', 'jet',
'turbo', 'nipy_spectral', 'gist_ncar'])
plt.show()
# Do plot
for cmap_category, cmap_list in cmaps.items():
for i, ax in enumerate(axs.flat):
if cmap_category == 'Sequential':
# These colormaps all start at high lightness, but we want them
# reversed to look nice in the plot, so reverse the order.
y_ = lab[0, ::-1, 0]
c_ = x[::-1]
else:
y_ = lab[0, :, 0]
c_ = x
fig.tight_layout(h_pad=0.0, pad=1.5)
plt.show()
Grayscale conversion
It is important to pay attention to conversion to grayscale for color plots, since they may be
printed on black and white printers. If not carefully considered, your readers may end up with
indecipherable plots because the grayscale changes unpredictably through the colormap.
Conversion to grayscale is done in many different ways [bw]. Some of the better ones use a linear
combination of the rgb values of a pixel, but weighted according to how we perceive color
intensity. A nonlinear method of conversion to grayscale is to use the L∗ values of the pixels. In
general, similar principles apply for this question as they do for presenting one's information
perceptually; that is, if a colormap is chosen that is monotonically increasing in L∗ values, it will
print in a reasonable manner to grayscale.
With this in mind, we see that the Sequential colormaps have reasonable representations in
grayscale. Some of the Sequential2 colormaps have decent enough grayscale representations,
though some (autumn, spring, summer, winter) have very little grayscale change. If a colormap
like this was used in a plot and then the plot was printed to grayscale, a lot of the information
may map to the same gray values. The Diverging colormaps mostly vary from darker gray on the
outer edges to white in the middle. Some (PuOr and seismic) have noticeably darker gray on one
side than the other and therefore are not very symmetric. coolwarm has little range of gray scale
and would print to a more uniform plot, losing a lot of detail. Note that overlaid, labeled
contours could help differentiate between one side of the colormap vs. the other since color
cannot be used once a plot is printed to grayscale. Many of the Qualitative and Miscellaneous
colormaps, such as Accent, hsv, jet and turbo, change from darker to lighter and back to darker
grey throughout the colormap. This would make it impossible for a viewer to interpret the
information in a plot once it is printed in grayscale.
mpl.rcParams.update({'font.size': 14})
# Turn off *all* ticks & spines, not just the ones with colormaps.
for ax in axs.flat:
ax.set_axis_off()
plt.show()
plot_color_gradients(cmap_category, cmap_list)