Skip to content

Commit 50bb88a

Browse files
authored
Merge pull request #8156 from afvincent/add_missing_cmaps_to_perception_doc
DOC: Add missing cmaps to perception doc (fix for #8073)
2 parents d10d9d9 + e442647 commit 50bb88a

File tree

4 files changed

+121
-140
lines changed

4 files changed

+121
-140
lines changed
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
# Have colormaps separated into categories:
22
# http://matplotlib.org/examples/color/colormaps_reference.html
33

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'])]
4+
cmaps = [('Perceptually Uniform Sequential', [
5+
'viridis', 'plasma', 'inferno', 'magma']),
6+
('Sequential', [
7+
'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
8+
'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
9+
'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']),
10+
('Sequential (2)', [
11+
'binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink',
12+
'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia',
13+
'hot', 'afmhot', 'gist_heat', 'copper']),
14+
('Diverging', [
15+
'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu',
16+
'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']),
17+
('Qualitative', [
18+
'Pastel1', 'Pastel2', 'Paired', 'Accent',
19+
'Dark2', 'Set1', 'Set2', 'Set3',
20+
'tab10', 'tab20', 'tab20b', 'tab20c']),
21+
('Miscellaneous', [
22+
'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern',
23+
'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'hsv',
24+
'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar'])]

doc/users/plotting/colormaps/grayscale.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
11
'''
2-
Show what matplotlib colormaps look like in grayscale.
2+
==================================
3+
Grayscale version of the colormaps
4+
==================================
5+
6+
Show what Matplotlib colormaps look like in grayscale.
37
Uses lightness L* as a proxy for grayscale value.
48
'''
59

6-
from colormaps import cmaps
7-
8-
#from skimage import color
9-
# we are using a local copy of colorconv from scikit-image to reduce dependencies.
10-
# You should probably use the one from scikit-image in most cases.
1110
import numpy as np
11+
import matplotlib as mpl
1212
import matplotlib.pyplot as plt
1313
from matplotlib import cm
14-
import matplotlib as mpl
1514
from colorspacious import cspace_converter
15+
from colormaps import cmaps # the colormaps, grouped by category
1616

1717
mpl.rcParams.update({'font.size': 14})
1818

19-
20-
# indices to step through colormap
19+
# Indices to step through colormap.
2120
x = np.linspace(0.0, 1.0, 100)
2221

23-
# nrows = max(len(cmap_list) for cmap_category, cmap_list in cmaps)
2422
gradient = np.linspace(0, 1, 256)
2523
gradient = np.vstack((gradient, gradient))
2624

25+
2726
def plot_color_gradients(cmap_category, cmap_list):
28-
nrows = len(cmap_list)
29-
fig, axes = plt.subplots(nrows=nrows, ncols=2)
27+
fig, axes = plt.subplots(nrows=len(cmap_list), ncols=2)
3028
fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99,
3129
wspace=0.05)
3230
fig.suptitle(cmap_category + ' colormaps', fontsize=14, y=1.0, x=0.6)
3331

3432
for ax, name in zip(axes, cmap_list):
3533

36-
# Get rgb values for colormap
34+
# Get RGB values for colormap.
3735
rgb = cm.get_cmap(plt.get_cmap(name))(x)[np.newaxis,:,:3]
3836

3937
# Get colormap in CAM02-UCS colorspace. We want the lightness.
@@ -49,9 +47,9 @@ def plot_color_gradients(cmap_category, cmap_list):
4947
fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)
5048

5149
# Turn off *all* ticks & spines, not just the ones with colormaps.
52-
for ax in axes:
53-
ax[0].set_axis_off()
54-
ax[1].set_axis_off()
50+
for ax in axes.flat:
51+
ax.set_axis_off()
52+
5553
plt.show()
5654

5755

Lines changed: 65 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,100 @@
11
'''
2+
==========================
3+
Lightness of the colormaps
4+
==========================
5+
26
For each colormap, plot the lightness parameter L* from CIELAB colorspace
37
along the y axis vs index through the colormap. Colormaps are examined in
48
categories as in the original matplotlib gallery of colormaps.
59
'''
610

7-
from colormaps import cmaps
811
import numpy as np
12+
import matplotlib as mpl
913
import matplotlib.pyplot as plt
1014
from matplotlib import cm
11-
import matplotlib as mpl
1215
from colorspacious import cspace_converter
16+
from colormaps import cmaps # the colormaps, grouped by category
1317

1418
mpl.rcParams.update({'font.size': 12})
1519

16-
# indices to step through colormap
20+
# Number of colormap per subplot for particular cmap categories
21+
_DSUBS = {'Perceptually Uniform Sequential': 4, 'Sequential': 6,
22+
'Sequential (2)': 6, 'Diverging': 6, 'Qualitative': 4,
23+
'Miscellaneous': 6}
24+
25+
# Spacing between the colormaps of a subplot
26+
_DC = {'Perceptually Uniform Sequential': 1.4, 'Sequential': 0.7,
27+
'Sequential (2)': 1.4, 'Diverging': 1.4, 'Qualitative': 1.4,
28+
'Miscellaneous': 1.4}
29+
30+
# Indices to step through colormap
1731
x = np.linspace(0.0, 1.0, 100)
1832

1933
# Do plot
2034
for cmap_category, cmap_list in cmaps:
2135

22-
# Do subplots so that colormaps have enough space. 5 per subplot?
23-
dsub = 5 # number of colormaps per subplot
24-
if cmap_category == 'Diverging': # because has 12 colormaps
25-
dsub = 6
26-
elif cmap_category == 'Sequential (2)':
27-
dsub = 6
28-
elif cmap_category == 'Sequential':
29-
dsub = 7
30-
nsubplots = int(np.ceil(len(cmap_list)/float(dsub)))
31-
32-
fig = plt.figure(figsize=(7,2.6*nsubplots))
36+
# Do subplots so that colormaps have enough space.
37+
# Default is 6 colormaps per subplot.
38+
dsub = _DSUBS.get(cmap_category, 6)
39+
nsubplots = int(np.ceil(len(cmap_list) / float(dsub)))
3340

34-
for i, subplot in enumerate(range(nsubplots)):
41+
# squeeze=False to handle similarly the case of a single subplot
42+
fig, axes = plt.subplots(nrows=nsubplots, squeeze=False,
43+
figsize=(7, 2.6*nsubplots))
3544

36-
locs = [] # locations for text labels
45+
for i, ax in enumerate(axes.flat):
3746

38-
ax = fig.add_subplot(nsubplots, 1, i+1)
47+
locs = [] # locations for text labels
3948

4049
for j, cmap in enumerate(cmap_list[i*dsub:(i+1)*dsub]):
4150

42-
# Get rgb values for colormap
43-
rgb = cm.get_cmap(cmap)(x)[np.newaxis,:,:3]
44-
45-
# Get colormap in CAM02-UCS colorspace. We want the lightness.
51+
# Get RGB values for colormap and convert the colormap in
52+
# CAM02-UCS colorspace. lab[0, :, 0] is the lightness.
53+
rgb = cm.get_cmap(cmap)(x)[np.newaxis, :, :3]
4654
lab = cspace_converter("sRGB1", "CAM02-UCS")(rgb)
4755

48-
# Plot colormap L values
49-
# Do separately for each category so each plot can be pretty
50-
# to make scatter markers change color along plot:
56+
# Plot colormap L values. Do separately for each category
57+
# so each plot can be pretty. To make scatter markers change
58+
# color along plot:
5159
# http://stackoverflow.com/questions/8202605/matplotlib-scatterplot-colour-as-a-function-of-a-third-variable
52-
if cmap_category=='Perceptually Uniform Sequential':
53-
dc = 1.15 # spacing between colormaps
54-
ax.scatter(x+j*dc, lab[0,:,0], c=x, cmap=cmap,
55-
s=300, linewidths=0.)
56-
if i==2:
57-
ax.axis([-0.1,4.1,0,100])
58-
else:
59-
ax.axis([-0.1,4.7,0,100])
60-
locs.append(x[-1]+j*dc) # store locations for colormap labels
61-
62-
elif cmap_category=='Sequential':
63-
dc = 0.6 # spacing between colormaps
60+
61+
if cmap_category == 'Sequential':
6462
# These colormaps all start at high lightness but we want them
6563
# reversed to look nice in the plot, so reverse the order.
66-
ax.scatter(x+j*dc, lab[0,::-1,0], c=x[::-1], cmap=cmap,
67-
s=300, linewidths=0.)
68-
if i==2:
69-
ax.axis([-0.1,4.1,0,100])
70-
else:
71-
ax.axis([-0.1,4.7,0,100])
72-
locs.append(x[-1]+j*dc) # store locations for colormap labels
73-
74-
elif cmap_category=='Sequential (2)':
75-
dc = 1.15
76-
ax.scatter(x+j*dc, lab[0,:,0], c=x, cmap=cmap,
77-
s=300, linewidths=0.)
78-
ax.axis([-0.1,7.0,0,100])
79-
# store locations for colormap labels
80-
locs.append(x[-1]+j*dc)
81-
82-
elif cmap_category=='Diverging':
83-
dc = 1.2
84-
ax.scatter(x+j*dc, lab[0,:,0], c=x, cmap=cmap,
85-
s=300, linewidths=0.)
86-
ax.axis([-0.1,7.1,0,100])
87-
# store locations for colormap labels
88-
locs.append(x[int(x.size/2.)]+j*dc)
89-
elif cmap_category=='Qualitative':
90-
dc = 1.3
91-
ax.scatter(x+j*dc, lab[0,:,0], c=x, cmap=cmap,
92-
s=300, linewidths=0.)
93-
ax.axis([-0.1,6.3,0,100])
94-
# store locations for colormap labels
95-
locs.append(x[int(x.size/2.)]+j*dc)
96-
97-
elif cmap_category=='Miscellaneous':
98-
dc = 1.25
99-
ax.scatter(x+j*dc, lab[0,:,0], c=x, cmap=cmap,
100-
s=300, linewidths=0.)
101-
ax.axis([-0.1,6.1,0,100])
102-
# store locations for colormap labels
103-
locs.append(x[int(x.size/2.)]+j*dc)
104-
105-
# Set up labels for colormaps
106-
ax.xaxis.set_ticks_position('top')
107-
ticker = mpl.ticker.FixedLocator(locs)
108-
ax.xaxis.set_major_locator(ticker)
109-
formatter = mpl.ticker.FixedFormatter(cmap_list[i*dsub:(i+1)*dsub])
110-
ax.xaxis.set_major_formatter(formatter)
111-
labels = ax.get_xticklabels()
112-
for label in labels:
113-
label.set_rotation(60)
64+
y_ = lab[0, ::-1, 0]
65+
c_ = x[::-1]
66+
else:
67+
y_ = lab[0, :, 0]
68+
c_ = x
69+
70+
dc = _DC.get(cmap_category, 1.4) # cmaps horizontal spacing
71+
ax.scatter(x + j*dc, y_, c=c_, cmap=cmap, s=300, linewidths=0.0)
72+
73+
# Store locations for colormap labels
74+
if cmap_category in ('Perceptually Uniform Sequential',
75+
'Sequential'):
76+
locs.append(x[-1] + j*dc)
77+
elif cmap_category in ('Diverging', 'Qualitative',
78+
'Miscellaneous', 'Sequential (2)'):
79+
locs.append(x[int(x.size/2.)] + j*dc)
80+
81+
# Set up the axis limits:
82+
# * the 1st subplot is used as a reference for the x-axis limits
83+
# * lightness values goes from 0 to 100 (y-axis limits)
84+
ax.set_xlim(axes[0, 0].get_xlim())
85+
ax.set_ylim(0.0, 100.0)
86+
87+
# Set up labels for colormaps
88+
ax.xaxis.set_ticks_position('top')
89+
ticker = mpl.ticker.FixedLocator(locs)
90+
ax.xaxis.set_major_locator(ticker)
91+
formatter = mpl.ticker.FixedFormatter(cmap_list[i*dsub:(i+1)*dsub])
92+
ax.xaxis.set_major_formatter(formatter)
93+
ax.xaxis.set_tick_params(rotation=50)
11494

11595
ax.set_xlabel(cmap_category + ' colormaps', fontsize=14)
11696
fig.text(0.0, 0.55, 'Lightness $L^*$', fontsize=12,
11797
transform=fig.transFigure, rotation=90)
11898

119-
fig.tight_layout(h_pad=0.05, pad=1.5)
99+
fig.tight_layout(h_pad=0.0, pad=1.5)
120100
plt.show()

examples/color/colormaps_reference.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,27 @@
4242

4343
# Have colormaps separated into categories:
4444
# http://matplotlib.org/examples/color/colormaps_reference.html
45-
cmaps = [('Perceptually Uniform Sequential',
46-
['viridis', 'inferno', 'plasma', 'magma']),
47-
('Sequential', ['Blues', 'BuGn', 'BuPu',
48-
'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd',
49-
'PuBu', 'PuBuGn', 'PuRd', 'Purples', 'RdPu',
50-
'Reds', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd']),
51-
('Sequential (2)', ['afmhot', 'autumn', 'bone', 'cool',
52-
'copper', 'gist_heat', 'gray', 'hot',
53-
'pink', 'spring', 'summer', 'winter']),
54-
('Diverging', ['BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn', 'PuOr',
55-
'RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'Spectral',
56-
'seismic']),
57-
('Qualitative', ['Accent', 'Dark2', 'Paired', 'Pastel1',
58-
'Pastel2', 'Set1', 'Set2', 'Set3', 'tab10',
59-
'tab20', 'tab20b', 'tab20c']),
60-
('Miscellaneous', ['gist_earth', 'terrain', 'ocean', 'gist_stern',
61-
'brg', 'CMRmap', 'cubehelix',
62-
'gnuplot', 'gnuplot2', 'gist_ncar',
63-
'nipy_spectral', 'jet', 'rainbow',
64-
'gist_rainbow', 'hsv', 'flag', 'prism'])]
45+
cmaps = [('Perceptually Uniform Sequential', [
46+
'viridis', 'plasma', 'inferno', 'magma']),
47+
('Sequential', [
48+
'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
49+
'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
50+
'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']),
51+
('Sequential (2)', [
52+
'binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink',
53+
'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia',
54+
'hot', 'afmhot', 'gist_heat', 'copper']),
55+
('Diverging', [
56+
'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu',
57+
'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']),
58+
('Qualitative', [
59+
'Pastel1', 'Pastel2', 'Paired', 'Accent',
60+
'Dark2', 'Set1', 'Set2', 'Set3',
61+
'tab10', 'tab20', 'tab20b', 'tab20c']),
62+
('Miscellaneous', [
63+
'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern',
64+
'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'hsv',
65+
'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar'])]
6566

6667

6768
nrows = max(len(cmap_list) for cmap_category, cmap_list in cmaps)

0 commit comments

Comments
 (0)