|
| 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