Skip to content

Commit 5e58e90

Browse files
committed
Merge pull request #5284 from mdboom/colormap-docs
New Colormaps to docs
2 parents 50f373f + e01552c commit 5e58e90

File tree

6 files changed

+216
-111
lines changed

6 files changed

+216
-111
lines changed

doc/users/colormaps.rst

+118-26
Original file line numberDiff line numberDiff line change
@@ -8,91 +8,184 @@ Choosing Colormaps
88
Overview
99
========
1010

11-
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:
11+
The idea behind choosing a good colormap is to find a good representation in 3D
12+
colorspace for your data set. The best colormap for any given data set depends
13+
on many things including:
1214

1315
- Whether representing form or metric data ([Ware]_)
14-
- Your knowledge of the data set (*e.g.*, is there a critical value from which the other values deviate?)
16+
17+
- Your knowledge of the data set (*e.g.*, is there a critical value
18+
from which the other values deviate?)
19+
1520
- If there is an intuitive color scheme for the parameter you are plotting
21+
1622
- If there is a standard in the field the audience may be expecting
1723

18-
For many applications, a perceptual colormap is the best choice --- one 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.
24+
For many applications, a perceptually uniform colormap is the best
25+
choice --- one in which equal steps in data are perceived as equal
26+
steps in the color space. Researchers have found that the human brain
27+
perceives changes in the lightness parameter as changes in the data
28+
much better than, for example, changes in hue. Therefore, colormaps
29+
which have monotonically increasing lightness through the colormap
30+
will be better interpreted by the viewer.
1931

20-
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, :math:`L^*`; red-green, :math:`a^*`; and yellow-blue, :math:`b^*`. The lightness parameter :math:`L^*` can then be used to learn more about how the matplotlib colormaps will be perceived by viewers.
32+
Color can be represented in 3D space in various ways. One way to represent color
33+
is using CIELAB. In CIELAB, color space is represented by lightness,
34+
:math:`L^*`; red-green, :math:`a^*`; and yellow-blue, :math:`b^*`. The lightness
35+
parameter :math:`L^*` can then be used to learn more about how the matplotlib
36+
colormaps will be perceived by viewers.
2137

22-
An excellent starting resource for learning about human perception of colormaps is from [IBM]_.
38+
An excellent starting resource for learning about human perception of colormaps
39+
is from [IBM]_.
2340

2441

2542
Classes of colormaps
2643
====================
2744

28-
Colormaps are often split into several categories based on their function (see, *e.g.*, [Moreland]_):
45+
Colormaps are often split into several categories based on their function (see,
46+
*e.g.*, [Moreland]_):
47+
48+
1. Sequential: change in lightness and often saturation of color
49+
incrementally, often using a single hue; should be used for
50+
representing information that has ordering.
51+
52+
2. Diverging: change in lightness and possibly saturation of two
53+
different colors that meet in the middle at an unsaturated color;
54+
should be used when the information being plotted has a critical
55+
middle value, such as topography or when the data deviates around
56+
zero.
2957

30-
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.
31-
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.
32-
3. Qualitative: often are miscellaneous colors; should be used to represent information which does not have ordering or relationships.
58+
3. Qualitative: often are miscellaneous colors; should be used to
59+
represent information which does not have ordering or
60+
relationships.
3361

3462

3563
Lightness of matplotlib colormaps
3664
=================================
3765

38-
Here we examine the lightness values of the matplotlib colormaps. Note that some documentation on the colormaps is available ([list-colormaps]_).
66+
Here we examine the lightness values of the matplotlib colormaps. Note that some
67+
documentation on the colormaps is available ([list-colormaps]_).
3968

4069
Sequential
4170
----------
4271

43-
For the Sequential plots, the lightness value increases monotonically through the colormaps. This is good. Some of the :math:`L^*` values in the colormaps span from 0 to 100 (binary and the other grayscale), and others start around :math:`L^*=20`. Those that have a smaller range of :math:`L^*` will accordingly have a smaller perceptual range. Note also that the :math:`L^*` function varies amongst the colormaps: some are approximately linear in :math:`L^*` and others are more curved.
72+
For the Sequential plots, the lightness value increases monotonically through
73+
the colormaps. This is good. Some of the :math:`L^*` values in the colormaps
74+
span from 0 to 100 (binary and the other grayscale), and others start around
75+
:math:`L^*=20`. Those that have a smaller range of :math:`L^*` will accordingly
76+
have a smaller perceptual range. Note also that the :math:`L^*` function varies
77+
amongst the colormaps: some are approximately linear in :math:`L^*` and others
78+
are more curved.
4479

4580
Sequential2
4681
-----------
4782

48-
Many of the :math:`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 :math:`L^*` space. Others (afmhot, copper, gist_heat, and hot) have kinks in the :math:`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).
83+
Many of the :math:`L^*` values from the Sequential2 plots are monotonically
84+
increasing, but some (autumn, cool, spring, and winter) plateau or even go both
85+
up and down in :math:`L^*` space. Others (afmhot, copper, gist_heat, and hot)
86+
have kinks in the :math:`L^*` functions. Data that is being represented in a
87+
region of the colormap that is at a plateau or kink will lead to a perception of
88+
banding of the data in those values in the colormap (see [mycarta-banding]_ for
89+
an excellent example of this).
4990

5091
Diverging
5192
---------
5293

53-
For the Diverging maps, we want to have monotonically increasing :math:`L^*` values up to a maximum, which should be close to :math:`L^*=100`, followed by monotonically decreasing :math:`L^*` values. We are looking for approximately equal minimum :math:`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 :math:`L^*` values (see grayscale section below).
94+
For the Diverging maps, we want to have monotonically increasing :math:`L^*`
95+
values up to a maximum, which should be close to :math:`L^*=100`, followed by
96+
monotonically decreasing :math:`L^*` values. We are looking for approximately
97+
equal minimum :math:`L^*` values at opposite ends of the colormap. By these
98+
measures, BrBG and RdBu are good options. coolwarm is a good option, but it
99+
doesn't span a wide range of :math:`L^*` values (see grayscale section below).
54100

55101
Qualitative
56102
-----------
57103

58-
Qualitative colormaps are not aimed at being perceptual maps, but looking at the lightness parameter can verify that for us. The :math:`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.
104+
Qualitative colormaps are not aimed at being perceptual maps, but looking at the
105+
lightness parameter can verify that for us. The :math:`L^*` values move all over
106+
the place throughout the colormap, and are clearly not monotonically increasing.
107+
These would not be good options for use as perceptual colormaps.
59108

60109
Miscellaneous
61110
-------------
62111

63-
Some of the miscellaneous colormaps have particular uses they have been created for. 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 :math:`L^*`. cubehelix was created to vary smoothly in both lightness and hue, but appears to have a small hump in the green hue area.
64-
65-
The often-used jet colormap is included in this set of colormaps. We can see that the :math:`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]_.
112+
Some of the miscellaneous colormaps have particular uses for which
113+
they have been created. For example, gist_earth, ocean, and terrain
114+
all seem to be created for plotting topography (green/brown) and water
115+
depths (blue) together. We would expect to see a divergence in these
116+
colormaps, then, but multiple kinks may not be ideal, such as in
117+
gist_earth and terrain. CMRmap was created to convert well to
118+
grayscale, though it does appear to have some small kinks in
119+
:math:`L^*`. cubehelix was created to vary smoothly in both lightness
120+
and hue, but appears to have a small hump in the green hue area.
121+
122+
The often-used jet colormap is included in this set of colormaps. We can see
123+
that the :math:`L^*` values vary widely throughout the colormap, making it a
124+
poor choice for representing data for viewers to see perceptually. See an
125+
extension on this idea at [mycarta-jet]_.
66126

67127
.. plot:: users/plotting/colormaps/lightness.py
68128

69129

70130
:math:`L^*` function
71131
====================
72132

73-
There are multiple approaches to finding the best function for :math:`L^*` across a colormap. Linear gives reasonable results (*e.g.*, [mycarta-banding]_, [mycarta-lablinear]_). However, the Weber-Fechner law, and more generally and recently, Stevens' Law, indicates that a logarithmic or geometric relationship might be better (see effort on this front at [mycarta-cubelaw]_).
133+
There are multiple approaches to finding the best function for :math:`L^*`
134+
across a colormap. Linear gives reasonable results (*e.g.*, [mycarta-banding]_,
135+
[mycarta-lablinear]_). However, the Weber-Fechner law, and more generally and
136+
recently, Stevens' Law, indicates that a logarithmic or geometric relationship
137+
might be better (see effort on this front at [mycarta-cubelaw]_).
74138

75139
.. plot:: users/plotting/colormaps/Lfunction.py
76140

77141

78142
Grayscale conversion
79143
====================
80144

81-
Conversion to grayscale is important to pay attention to for printing publications that have color plots. If this is not paid attention to ahead of time, your readers may end up with indecipherable plots because the grayscale changes unpredictably through the colormap.
82-
83-
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 :math:`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 has monotonically increasing in :math:`L^*` values, it will print in a reasonable manner to grayscale.
84-
85-
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 noticably 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, and jet, change from darker to lighter and back to darker gray throughout the colormap. This would make it impossible for a viewer to interpret the information in a plot once it is printed in grayscale.
145+
It is important to pay attention to conversion to grayscale for color
146+
plots, since they may be printed on black and white printers. If not
147+
carefully considered, your readers may end up with indecipherable
148+
plots because the grayscale changes unpredictably through the
149+
colormap.
150+
151+
Conversion to grayscale is done in many different ways [bw]_. Some of the better
152+
ones use a linear combination of the rgb values of a pixel, but weighted
153+
according to how we perceive color intensity. A nonlinear method of conversion
154+
to grayscale is to use the :math:`L^*` values of the pixels. In general, similar
155+
principles apply for this question as they do for presenting one's information
156+
perceptually; that is, if a colormap is chosen that is monotonically increasing
157+
in :math:`L^*` values, it will print in a reasonable manner to grayscale.
158+
159+
With this in mind, we see that the Sequential colormaps have reasonable
160+
representations in grayscale. Some of the Sequential2 colormaps have decent
161+
enough grayscale representations, though some (autumn, spring, summer, winter)
162+
have very little grayscale change. If a colormap like this was used in a plot
163+
and then the plot was printed to grayscale, a lot of the information may map to
164+
the same gray values. The Diverging colormaps mostly vary from darker gray on
165+
the outer edges to white in the middle. Some (PuOr and seismic) have noticably
166+
darker gray on one side than the other and therefore are not very symmetric.
167+
coolwarm has little range of gray scale and would print to a more uniform plot,
168+
losing a lot of detail. Note that overlaid, labeled contours could help
169+
differentiate between one side of the colormap vs. the other since color cannot
170+
be used once a plot is printed to grayscale. Many of the Qualitative and
171+
Miscellaneous colormaps, such as Accent, hsv, and jet, change from darker to
172+
lighter and back to darker gray throughout the colormap. This would make it
173+
impossible for a viewer to interpret the information in a plot once it is
174+
printed in grayscale.
86175

87176
.. plot:: users/plotting/colormaps/grayscale.py
88177

89178

90179
Color vision deficiencies
91180
=========================
92181

93-
There is a lot of information available about color blindness available (*e.g.*, [colorblindness]_). Additionally, there are tools available to convert images to how they look for different types of color vision deficiencies (*e.g.*, [asp]_).
182+
There is a lot of information available about color blindness available (*e.g.*,
183+
[colorblindness]_). Additionally, there are tools available to convert images to
184+
how they look for different types of color vision deficiencies (*e.g.*, [asp]_).
94185

95-
The most common form of color vision deficiency involves differentiating between red and green. Thus, avoiding colormaps with both red and green will avoid many problems in general.
186+
The most common form of color vision deficiency involves differentiating between
187+
red and green. Thus, avoiding colormaps with both red and green will avoid many
188+
problems in general.
96189

97190

98191
References
@@ -109,4 +202,3 @@ References
109202
.. [colorblindness] http://aspnetresources.com/tools/colorBlindness
110203
.. [asp] http://aspnetresources.com/tools/colorBlindness
111204
.. [IBM] http://www.research.ibm.com/people/l/lloydt/color/color.HTM
112-

doc/users/plotting/colormaps/Lfunction.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
with the binary matplotlib colormap, too. Trying to show the difference between
44
adding blackness to a color at different rates.
55
'''
6-
from __future__ import print_function
76
import numpy as np
87
import matplotlib.pyplot as plt
98
import colorconv as color
109
#from skimage import color
11-
# we are using a local copy of colorconv from scikit-image to reduce dependencies.
12-
# You should probably use the one from scikit-image in most cases.
10+
# we are using a local copy of colorconv from scikit-image to reduce dependencies.
11+
# You should probably use the one from scikit-image in most cases.
1312
import matplotlib as mpl
1413
from matplotlib import cm
1514

1615

17-
mpl.rcParams.update({'font.size': 20})
18-
mpl.rcParams['font.sans-serif'] = 'Arev Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Helvetica, Avant Garde, sans-serif'
16+
mpl.rcParams.update({'font.size': 12})
17+
mpl.rcParams['font.sans-serif'] = ('Arev Sans, Bitstream Vera Sans, '
18+
'Lucida Grande, Verdana, Geneva, Lucid, '
19+
'Helvetica, Avant Garde, sans-serif')
1920
mpl.rcParams['mathtext.fontset'] = 'custom'
2021
mpl.rcParams['mathtext.cal'] = 'cursive'
2122
mpl.rcParams['mathtext.rm'] = 'sans'
@@ -42,7 +43,6 @@
4243
for i in range(red.shape[1]):
4344
# more blackness is closer to 0 than one, and in first column of LAB
4445
lab_add[0,i,0] = lab_add[0,i,0] - 10*i
45-
print(i,k)
4646
if i != 0:
4747
lab_geometric[0,i,0] = lab_geometric[0,i,0] - 10*k
4848
k *= 2
@@ -55,17 +55,15 @@
5555
temp = color.lab2rgb(lab_geometric)
5656
rgb_geometric[0,:,0] = temp[0,:,0]
5757

58-
fig = plt.figure()
58+
fig = plt.figure(figsize=(5,3))
5959
k = 1
6060
for i in range(red.shape[1]):
6161

6262
# LHS: additive
6363
ax1 = fig.add_subplot(nrows,2,i*2+1, axisbg=tuple(rgb_add[0,i,:]))
64-
print(tuple(lab_add[0,i,:]))#, tuple(rgb_add[0,i,:])
6564

6665
# RHS: multiplicative
6766
ax2 = fig.add_subplot(nrows,2,i*2+2, axisbg=tuple(rgb_geometric[0,i,:]))
68-
print(tuple(lab_geometric[0,i,:]))#, tuple(rgb_geometric[0,i,:])
6967

7068
# ylabels
7169
if i!=0:
@@ -122,17 +120,15 @@
122120
rgb_add[:,i,:] = rgb[:,i*di+I0,:]
123121

124122
if i != 0:
125-
print(i*di+I0, di*k+I0, (I0**(1./3)+i*di**(1./3))**3)
126123
rgb_geometric[:,i,:] = rgb[:,I0+di*k,:]
127124
k *= 2
128125
elif i==0:
129-
print(i*di+I0, I0, (I0**(1./3)+i*di**(1./3))**3)
130126
rgb_geometric[:,i,:] = rgb[:,I0,:]
131127

132128
lab_add = color.rgb2lab(rgb_add)
133129
lab_geometric = color.rgb2lab(rgb_geometric)
134130

135-
fig = plt.figure()
131+
fig = plt.figure(figsize=(5,3))
136132
k = 1
137133
for i in range(nrows):
138134

@@ -165,8 +161,8 @@
165161
ax2.spines['left'].set_visible(False)
166162

167163
# common ylabel
168-
ax1.text(-0.3, 4.0, 'Steps through map indices',
169-
rotation=90, transform=ax1.transAxes)
164+
ax1.text(-0.3, 4.5, 'Steps through map indices',
165+
rotation=90, transform=ax1.transAxes)
170166

171167
fig.subplots_adjust(hspace=0.0)
172168
plt.show()
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Have colormaps separated into categories:
2+
# http://matplotlib.org/examples/color/colormaps_reference.html
3+
4+
cmaps = [('Perceptually Uniform Sequential',
5+
['viridis', 'inferno', 'plasma', 'magma']),
6+
('Sequential', ['Blues', 'BuGn', 'BuPu',
7+
'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd',
8+
'PuBu', 'PuBuGn', 'PuRd', 'Purples', 'RdPu',
9+
'Reds', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd']),
10+
('Sequential (2)', ['afmhot', 'autumn', 'bone', 'cool',
11+
'copper', 'gist_heat', 'gray', 'hot',
12+
'pink', 'spring', 'summer', 'winter']),
13+
('Diverging', ['BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn', 'PuOr',
14+
'RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'Spectral',
15+
'seismic']),
16+
('Qualitative', ['Accent', 'Dark2', 'Paired', 'Pastel1',
17+
'Pastel2', 'Set1', 'Set2', 'Set3']),
18+
('Miscellaneous', ['gist_earth', 'terrain', 'ocean', 'gist_stern',
19+
'brg', 'CMRmap', 'cubehelix',
20+
'gnuplot', 'gnuplot2', 'gist_ncar',
21+
'nipy_spectral', 'jet', 'rainbow',
22+
'gist_rainbow', 'hsv', 'flag', 'prism'])]

0 commit comments

Comments
 (0)