|
| 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() |
0 commit comments