Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52d585a

Browse files
committed
now with scripts actually included!
1 parent 11243ff commit 52d585a

File tree

3 files changed

+383
-0
lines changed

3 files changed

+383
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
'''
2+
Recreate Josef Albers plot illustrating the Weber-Fechner law and illustrate
3+
with the binary matplotlib colormap, too. Trying to show the difference between
4+
adding blackness to a color at different rates.
5+
'''
6+
7+
import numpy as np
8+
import matplotlib.pyplot as plt
9+
from skimage import io, color
10+
import pdb
11+
import matplotlib as mpl
12+
from mpl_toolkits.mplot3d import Axes3D
13+
from matplotlib import cm, colors
14+
15+
16+
mpl.rcParams.update({'font.size': 20})
17+
mpl.rcParams['font.sans-serif'] = 'Arev Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Helvetica, Avant Garde, sans-serif'
18+
mpl.rcParams['mathtext.fontset'] = 'custom'
19+
mpl.rcParams['mathtext.cal'] = 'cursive'
20+
mpl.rcParams['mathtext.rm'] = 'sans'
21+
mpl.rcParams['mathtext.tt'] = 'monospace'
22+
mpl.rcParams['mathtext.it'] = 'sans:italic'
23+
mpl.rcParams['mathtext.bf'] = 'sans:bold'
24+
mpl.rcParams['mathtext.sf'] = 'sans'
25+
mpl.rcParams['mathtext.fallback_to_cm'] = 'True'
26+
27+
28+
### Red, original Albers plot
29+
30+
nrows = 5
31+
32+
# Start with red
33+
red = np.array([np.hstack([np.ones((nrows,1)), np.zeros((nrows,2))])])
34+
35+
# Get basic red in LAB
36+
lab_add = color.rgb2lab(red)
37+
lab_geometric = lab_add.copy()
38+
39+
# Alter successive rows with more black
40+
k = 1
41+
for i in xrange(red.shape[1]):
42+
# more blackness is closer to 0 than one, and in first column of LAB
43+
lab_add[0,i,0] = lab_add[0,i,0] - 10*i
44+
print i,k
45+
if i != 0:
46+
lab_geometric[0,i,0] = lab_geometric[0,i,0] - 10*k
47+
k *= 2
48+
49+
# Change LAB back to RGB for plotting
50+
rgb_add = red.copy() # only change red values
51+
temp = color.lab2rgb(lab_add)
52+
rgb_add[0,:,0] = temp[0,:,0]
53+
rgb_geometric = red.copy() # only change red values
54+
temp = color.lab2rgb(lab_geometric)
55+
rgb_geometric[0,:,0] = temp[0,:,0]
56+
57+
fig = plt.figure()
58+
k = 1
59+
for i in xrange(red.shape[1]):
60+
61+
# LHS: additive
62+
ax1 = fig.add_subplot(nrows,2,i*2+1, axisbg=tuple(rgb_add[0,i,:]))
63+
print tuple(lab_add[0,i,:])#, tuple(rgb_add[0,i,:])
64+
65+
# RHS: multiplicative
66+
ax2 = fig.add_subplot(nrows,2,i*2+2, axisbg=tuple(rgb_geometric[0,i,:]))
67+
print tuple(lab_geometric[0,i,:])#, tuple(rgb_geometric[0,i,:])
68+
69+
# ylabels
70+
if i!=0:
71+
ax1.set_ylabel(str(1*i))
72+
ax2.set_ylabel(str(k))
73+
k *= 2
74+
75+
# Turn off ticks
76+
ax1.get_xaxis().set_ticks([])
77+
ax2.get_xaxis().set_ticks([])
78+
ax1.get_yaxis().set_ticks([])
79+
ax2.get_yaxis().set_ticks([])
80+
81+
# Turn off black edges
82+
ax1.spines['right'].set_visible(False)
83+
ax1.spines['top'].set_visible(False)
84+
ax1.spines['bottom'].set_visible(False)
85+
ax1.spines['left'].set_visible(False)
86+
ax2.spines['right'].set_visible(False)
87+
ax2.spines['top'].set_visible(False)
88+
ax2.spines['bottom'].set_visible(False)
89+
ax2.spines['left'].set_visible(False)
90+
91+
92+
# common ylabel
93+
ax1.text(-0.3, 3.8, 'Additional Parts Black',
94+
rotation=90, transform=ax1.transAxes)
95+
96+
97+
fig.subplots_adjust(hspace=0.0)
98+
plt.show()
99+
100+
101+
### Albers plot with linear scale black and white
102+
103+
nrows = 5
104+
ncols = 2
105+
106+
x = np.linspace(0.0, 1.0, 100)
107+
cmap = 'binary'
108+
109+
# Get binary colormap entries for full 100 entries
110+
rgb = cm.get_cmap(cmap)(x)[np.newaxis,:,:3]
111+
112+
# Sample 100-entry rgb additively and geometrically
113+
rgb_add = np.empty((1,nrows,3))
114+
rgb_geometric = np.empty((1,nrows,3))
115+
116+
k = 1
117+
di = 8
118+
I0 = 5
119+
for i in xrange(nrows):
120+
# Do more blackness via increasing indices
121+
rgb_add[:,i,:] = rgb[:,i*di+I0,:]
122+
123+
if i != 0:
124+
print i*di+I0, di*k+I0, (I0**(1./3)+i*di**(1./3))**3
125+
rgb_geometric[:,i,:] = rgb[:,I0+di*k,:]
126+
k *= 2
127+
elif i==0:
128+
print i*di+I0, I0, (I0**(1./3)+i*di**(1./3))**3
129+
rgb_geometric[:,i,:] = rgb[:,I0,:]
130+
131+
lab_add = color.rgb2lab(rgb_add)
132+
lab_geometric = color.rgb2lab(rgb_geometric)
133+
134+
fig = plt.figure()
135+
k = 1
136+
for i in xrange(nrows):
137+
138+
# LHS: additive
139+
ax1 = fig.add_subplot(nrows,ncols,i*2+1, axisbg=tuple(rgb_add[0,i,:]))
140+
141+
# middle: multiplicative
142+
ax2 = fig.add_subplot(nrows,ncols,i*2+2, axisbg=tuple(rgb_geometric[0,i,:]))
143+
144+
# ylabels
145+
if i!=0:
146+
ax1.set_ylabel(str(1*i))
147+
ax2.set_ylabel(str(k))
148+
k *= 2
149+
150+
# Turn off ticks
151+
ax1.get_xaxis().set_ticks([])
152+
ax2.get_xaxis().set_ticks([])
153+
ax1.get_yaxis().set_ticks([])
154+
ax2.get_yaxis().set_ticks([])
155+
156+
# Turn off black edges
157+
ax1.spines['right'].set_visible(False)
158+
ax1.spines['top'].set_visible(False)
159+
ax1.spines['bottom'].set_visible(False)
160+
ax1.spines['left'].set_visible(False)
161+
ax2.spines['right'].set_visible(False)
162+
ax2.spines['top'].set_visible(False)
163+
ax2.spines['bottom'].set_visible(False)
164+
ax2.spines['left'].set_visible(False)
165+
166+
# common ylabel
167+
ax1.text(-0.3, 4.0, 'Steps through map indices',
168+
rotation=90, transform=ax1.transAxes)
169+
170+
fig.subplots_adjust(hspace=0.0)
171+
plt.show()
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'''
2+
Show what matplotlib colormaps look like in grayscale.
3+
Uses lightness L* as a proxy for grayscale value.
4+
'''
5+
6+
from skimage import io, color
7+
import numpy as np
8+
import matplotlib.pyplot as plt
9+
from matplotlib import cm
10+
import matplotlib as mpl
11+
import pdb
12+
from scipy.optimize import curve_fit
13+
14+
mpl.rcParams.update({'font.size': 14})
15+
mpl.rcParams['font.sans-serif'] = 'Arev Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Helvetica, Avant Garde, sans-serif'
16+
mpl.rcParams['mathtext.fontset'] = 'custom'
17+
mpl.rcParams['mathtext.cal'] = 'cursive'
18+
mpl.rcParams['mathtext.rm'] = 'sans'
19+
mpl.rcParams['mathtext.tt'] = 'monospace'
20+
mpl.rcParams['mathtext.it'] = 'sans:italic'
21+
mpl.rcParams['mathtext.bf'] = 'sans:bold'
22+
mpl.rcParams['mathtext.sf'] = 'sans'
23+
mpl.rcParams['mathtext.fallback_to_cm'] = 'True'
24+
25+
# Have colormaps separated into categories: http://matplotlib.org/examples/color/colormaps_reference.html
26+
27+
cmaps = [('Sequential', ['binary', 'Blues', 'BuGn', 'BuPu', 'gist_yarg',
28+
'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd',
29+
'PuBu', 'PuBuGn', 'PuRd', 'Purples', 'RdPu',
30+
'Reds', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd']),
31+
('Sequential2', ['afmhot', 'autumn', 'bone', 'cool', 'copper',
32+
'gist_gray', 'gist_heat', 'gray', 'hot', 'pink',
33+
'spring', 'summer', 'winter']),
34+
('Diverging', ['BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn', 'PuOr',
35+
'RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'seismic']),
36+
('Qualitative', ['Accent', 'Dark2', 'hsv', 'Paired', 'Pastel1',
37+
'Pastel2', 'Set1', 'Set2', 'Set3', 'spectral']),
38+
('Miscellaneous', ['gist_earth', 'gist_ncar', 'gist_rainbow',
39+
'gist_stern', 'jet', 'brg', 'CMRmap', 'cubehelix',
40+
'gnuplot', 'gnuplot2', 'ocean', 'rainbow',
41+
'terrain', 'flag', 'prism'])]
42+
43+
# indices to step through colormap
44+
x = np.linspace(0.0, 1.0, 100)
45+
46+
nrows = max(len(cmap_list) for cmap_category, cmap_list in cmaps)
47+
gradient = np.linspace(0, 1, 256)
48+
gradient = np.vstack((gradient, gradient))
49+
50+
def plot_color_gradients(cmap_category, cmap_list):
51+
fig, axes = plt.subplots(nrows=nrows, ncols=2)
52+
fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99, wspace=0.05)
53+
fig.suptitle(cmap_category + ' colormaps', fontsize=14, y=1.0, x=0.6)
54+
55+
for ax, name in zip(axes, cmap_list):
56+
57+
# Get rgb values for colormap
58+
rgb = cm.get_cmap(plt.get_cmap(name))(x)[np.newaxis,:,:3]
59+
60+
# Get colormap in CIE LAB. We want the L here.
61+
lab = color.rgb2lab(rgb)
62+
L = lab[0,:,0]
63+
L = np.float32(np.vstack((L, L, L)))
64+
65+
ax[0].imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
66+
ax[1].imshow(L, aspect='auto', cmap='binary_r', vmin=0., vmax=100.)
67+
pos = list(ax[0].get_position().bounds)
68+
x_text = pos[0] - 0.01
69+
y_text = pos[1] + pos[3]/2.
70+
fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)
71+
72+
# Turn off *all* ticks & spines, not just the ones with colormaps.
73+
for ax in axes:
74+
ax[0].set_axis_off()
75+
ax[1].set_axis_off()
76+
77+
78+
for cmap_category, cmap_list in cmaps:
79+
80+
plot_color_gradients(cmap_category, cmap_list)
81+
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
'''
2+
For each colormap, plot the lightness parameter L* from CIELAB colorspace along the y axis vs index through the colormap. Colormaps are examined in categories as in the original matplotlib gallery of colormaps.
3+
'''
4+
5+
from skimage import io, color
6+
import numpy as np
7+
import matplotlib.pyplot as plt
8+
from matplotlib import cm
9+
import matplotlib as mpl
10+
import pdb
11+
from scipy.optimize import curve_fit
12+
13+
mpl.rcParams.update({'font.size': 14})
14+
mpl.rcParams['font.sans-serif'] = 'Arev Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Helvetica, Avant Garde, sans-serif'
15+
mpl.rcParams['mathtext.fontset'] = 'custom'
16+
mpl.rcParams['mathtext.cal'] = 'cursive'
17+
mpl.rcParams['mathtext.rm'] = 'sans'
18+
mpl.rcParams['mathtext.tt'] = 'monospace'
19+
mpl.rcParams['mathtext.it'] = 'sans:italic'
20+
mpl.rcParams['mathtext.bf'] = 'sans:bold'
21+
mpl.rcParams['mathtext.sf'] = 'sans'
22+
mpl.rcParams['mathtext.fallback_to_cm'] = 'True'
23+
24+
# Have colormaps separated into categories: http://matplotlib.org/examples/color/colormaps_reference.html
25+
26+
cmaps = [('Sequential', ['binary', 'Blues', 'BuGn', 'BuPu', 'gist_yarg',
27+
'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd',
28+
'PuBu', 'PuBuGn', 'PuRd', 'Purples', 'RdPu',
29+
'Reds', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd']),
30+
('Sequential2', ['afmhot', 'autumn', 'bone', 'cool', 'copper',
31+
'gist_gray', 'gist_heat', 'gray', 'hot', 'pink',
32+
'spring', 'summer', 'winter']),
33+
('Diverging', ['BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn', 'PuOr',
34+
'RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'seismic']),
35+
('Qualitative', ['Accent', 'Dark2', 'hsv', 'Paired', 'Pastel1',
36+
'Pastel2', 'Set1', 'Set2', 'Set3', 'spectral']),
37+
('Miscellaneous', ['gist_earth', 'gist_ncar', 'gist_rainbow',
38+
'gist_stern', 'jet', 'brg', 'CMRmap', 'cubehelix',
39+
'gnuplot', 'gnuplot2', 'ocean', 'rainbow',
40+
'terrain', 'flag', 'prism'])]
41+
42+
# indices to step through colormap
43+
x = np.linspace(0.0, 1.0, 100)
44+
45+
# Do plot
46+
for cmap_category, cmap_list in cmaps:
47+
48+
# Do subplots so that colormaps have enough space. 5 per subplot?
49+
dsub = 5 # number of colormaps per subplot
50+
if cmap_category == 'Diverging': # because has 13 colormaps
51+
dsub = 6
52+
elif cmap_category == 'Sequential2':
53+
dsub = 7
54+
elif cmap_category == 'Sequential':
55+
dsub = 7
56+
nsubplots = int(np.ceil(len(cmap_list)/float(dsub)))
57+
58+
fig = plt.figure(figsize=(11.5,4*nsubplots))
59+
60+
for i, subplot in enumerate(xrange(nsubplots)):
61+
62+
locs = [] # locations for text labels
63+
64+
ax = fig.add_subplot(nsubplots, 1, i+1)
65+
# pdb.set_trace()
66+
67+
for j, cmap in enumerate(cmap_list[i*dsub:(i+1)*dsub]):
68+
69+
# Get rgb values for colormap
70+
rgb = cm.get_cmap(cmap)(x)[np.newaxis,:,:3]
71+
72+
# Get colormap in CIE LAB. We want the L here.
73+
lab = color.rgb2lab(rgb)
74+
75+
# Plot colormap L values
76+
# Do separately for each category so each plot can be pretty
77+
# to make scatter markers change color along plot: http://stackoverflow.com/questions/8202605/matplotlib-scatterplot-colour-as-a-function-of-a-third-variable
78+
if cmap_category=='Sequential':
79+
dc = 0.6 # spacing between colormaps
80+
ax.scatter(x+j*dc, lab[0,::-1,0], c=x, cmap=cmap + '_r', s=300, linewidths=0.)
81+
if i==2:
82+
ax.axis([-0.1,4.1,0,100])
83+
else:
84+
ax.axis([-0.1,4.7,0,100])
85+
locs.append(x[-1]+j*dc) # store locations for colormap labels
86+
87+
elif cmap_category=='Sequential2':
88+
dc = 1.15
89+
ax.scatter(x+j*dc, lab[0,:,0], c=x, cmap=cmap, s=300, linewidths=0.)
90+
if i==0:
91+
ax.axis([-0.1,8.1,0,100])
92+
else:
93+
ax.axis([-0.1,7.0,0,100])
94+
locs.append(x[-1]+j*dc) # store locations for colormap labels
95+
96+
elif cmap_category=='Diverging':
97+
dc = 1.2
98+
ax.scatter(x+j*dc, lab[0,:,0], c=x, cmap=cmap, s=300, linewidths=0.)
99+
if i==0:
100+
ax.axis([-0.1,7.1,0,100])
101+
else:
102+
ax.axis([-0.1,6,0,100])
103+
locs.append(x[int(x.size/2.)]+j*dc) # store locations for colormap labels
104+
105+
elif cmap_category=='Qualitative':
106+
dc = 1.3
107+
ax.scatter(x+j*dc, lab[0,:,0], c=x, cmap=cmap, s=300, linewidths=0.)
108+
ax.axis([-0.1,6.3,0,100])
109+
locs.append(x[int(x.size/2.)]+j*dc) # store locations for colormap labels
110+
111+
elif cmap_category=='Miscellaneous':
112+
dc = 1.25
113+
ax.scatter(x+j*dc, lab[0,:,0], c=x, cmap=cmap, s=300, linewidths=0.)
114+
ax.axis([-0.1,6.1,0,100])
115+
locs.append(x[int(x.size/2.)]+j*dc) # store locations for colormap labels
116+
117+
# Set up labels for colormaps
118+
ax.xaxis.set_ticks_position('top')
119+
ticker = mpl.ticker.FixedLocator(locs)
120+
ax.xaxis.set_major_locator(ticker)
121+
formatter = mpl.ticker.FixedFormatter(cmap_list[i*dsub:(i+1)*dsub])
122+
ax.xaxis.set_major_formatter(formatter)
123+
labels = ax.get_xticklabels()
124+
for label in labels:
125+
label.set_rotation(60)
126+
127+
ax.set_xlabel(cmap_category + ' colormaps', fontsize=22)
128+
fig.text(-0.005, 0.55, 'Lightness $L^*$', fontsize=18, transform=fig.transFigure, rotation=90)
129+
130+
fig.tight_layout(h_pad=0.05)
131+
plt.show

0 commit comments

Comments
 (0)