Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

186 řádky
7.3KB

  1. from colorspacious import cspace_converter
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. import matplotlib as mpl
  5. cmaps = {}
  6. gradient = np.linspace(0, 1, 256)
  7. gradient = np.vstack((gradient, gradient))
  8. def plot_color_gradients(category, cmap_list):
  9. # Create figure and adjust figure height to number of colormaps
  10. nrows = len(cmap_list)
  11. figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
  12. fig, axs = plt.subplots(nrows=nrows + 1, figsize=(6.4, figh))
  13. fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
  14. left=0.2, right=0.99)
  15. axs[0].set_title(f'{category} colormaps', fontsize=14)
  16. for ax, name in zip(axs, cmap_list):
  17. ax.imshow(gradient, aspect='auto', cmap=mpl.colormaps[name])
  18. ax.text(-0.01, 0.5, name, va='center', ha='right', fontsize=10,
  19. transform=ax.transAxes)
  20. # Turn off *all* ticks & spines, not just the ones with colormaps.
  21. for ax in axs:
  22. ax.set_axis_off()
  23. # Save colormap list for later.
  24. cmaps[category] = cmap_list
  25. plot_color_gradients('Perceptually Uniform Sequential',
  26. ['viridis', 'plasma', 'inferno', 'magma', 'cividis'])
  27. plot_color_gradients('Sequential',
  28. ['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
  29. 'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
  30. 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn'])
  31. plot_color_gradients('Sequential',
  32. ['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
  33. 'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
  34. 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn'])
  35. plot_color_gradients('Sequential (2)',
  36. ['binary', 'gist_yarg', 'gist_gray', 'gray', 'bone',
  37. 'pink', 'spring', 'summer', 'autumn', 'winter', 'cool',
  38. 'Wistia', 'hot', 'afmhot', 'gist_heat', 'copper'])
  39. plot_color_gradients('Diverging',
  40. ['PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu',
  41. 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic'])
  42. plot_color_gradients('Cyclic', ['twilight', 'twilight_shifted', 'hsv'])
  43. plot_color_gradients('Qualitative',
  44. ['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2',
  45. 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b',
  46. 'tab20c'])
  47. plot_color_gradients('Miscellaneous',
  48. ['flag', 'prism', 'ocean', 'gist_earth', 'terrain',
  49. 'gist_stern', 'gnuplot', 'gnuplot2', 'CMRmap',
  50. 'cubehelix', 'brg', 'gist_rainbow', 'rainbow', 'jet',
  51. 'turbo', 'nipy_spectral', 'gist_ncar'])
  52. plt.show()
  53. mpl.rcParams.update({'font.size': 12})
  54. # Number of colormap per subplot for particular cmap categories
  55. _DSUBS = {'Perceptually Uniform Sequential': 5, 'Sequential': 6,
  56. 'Sequential (2)': 6, 'Diverging': 6, 'Cyclic': 3,
  57. 'Qualitative': 4, 'Miscellaneous': 6}
  58. # Spacing between the colormaps of a subplot
  59. _DC = {'Perceptually Uniform Sequential': 1.4, 'Sequential': 0.7,
  60. 'Sequential (2)': 1.4, 'Diverging': 1.4, 'Cyclic': 1.4,
  61. 'Qualitative': 1.4, 'Miscellaneous': 1.4}
  62. # Indices to step through colormap
  63. x = np.linspace(0.0, 1.0, 100)
  64. # Do plot
  65. for cmap_category, cmap_list in cmaps.items():
  66. # Do subplots so that colormaps have enough space.
  67. # Default is 6 colormaps per subplot.
  68. dsub = _DSUBS.get(cmap_category, 6)
  69. nsubplots = int(np.ceil(len(cmap_list) / dsub))
  70. # squeeze=False to handle similarly the case of a single subplot
  71. fig, axs = plt.subplots(nrows=nsubplots, squeeze=False,
  72. figsize=(7, 2.6*nsubplots))
  73. for i, ax in enumerate(axs.flat):
  74. locs = [] # locations for text labels
  75. for j, cmap in enumerate(cmap_list[i*dsub:(i+1)*dsub]):
  76. # Get RGB values for colormap and convert the colormap in
  77. # CAM02-UCS colorspace. lab[0, :, 0] is the lightness.
  78. rgb = mpl.colormaps[cmap](x)[np.newaxis, :, :3]
  79. lab = cspace_converter("sRGB1", "CAM02-UCS")(rgb)
  80. # Plot colormap L values. Do separately for each category
  81. # so each plot can be pretty. To make scatter markers change
  82. # color along plot:
  83. # https://stackoverflow.com/q/8202605/
  84. if cmap_category == 'Sequential':
  85. # These colormaps all start at high lightness, but we want them
  86. # reversed to look nice in the plot, so reverse the order.
  87. y_ = lab[0, ::-1, 0]
  88. c_ = x[::-1]
  89. else:
  90. y_ = lab[0, :, 0]
  91. c_ = x
  92. dc = _DC.get(cmap_category, 1.4) # cmaps horizontal spacing
  93. ax.scatter(x + j*dc, y_, c=c_, cmap=cmap, s=300, linewidths=0.0)
  94. # Store locations for colormap labels
  95. if cmap_category in ('Perceptually Uniform Sequential',
  96. 'Sequential'):
  97. locs.append(x[-1] + j*dc)
  98. elif cmap_category in ('Diverging', 'Qualitative', 'Cyclic',
  99. 'Miscellaneous', 'Sequential (2)'):
  100. locs.append(x[int(x.size/2.)] + j*dc)
  101. # Set up the axis limits:
  102. # * the 1st subplot is used as a reference for the x-axis limits
  103. # * lightness values goes from 0 to 100 (y-axis limits)
  104. ax.set_xlim(axs[0, 0].get_xlim())
  105. ax.set_ylim(0.0, 100.0)
  106. # Set up labels for colormaps
  107. ax.xaxis.set_ticks_position('top')
  108. ticker = mpl.ticker.FixedLocator(locs)
  109. ax.xaxis.set_major_locator(ticker)
  110. formatter = mpl.ticker.FixedFormatter(cmap_list[i*dsub:(i+1)*dsub])
  111. ax.xaxis.set_major_formatter(formatter)
  112. ax.xaxis.set_tick_params(rotation=50)
  113. ax.set_ylabel('Lightness $L^*$', fontsize=12)
  114. ax.set_xlabel(cmap_category + ' colormaps', fontsize=14)
  115. fig.tight_layout(h_pad=0.0, pad=1.5)
  116. plt.show()
  117. mpl.rcParams.update({'font.size': 14})
  118. # Indices to step through colormap.
  119. x = np.linspace(0.0, 1.0, 100)
  120. gradient = np.linspace(0, 1, 256)
  121. gradient = np.vstack((gradient, gradient))
  122. def plot_color_gradients(cmap_category, cmap_list):
  123. fig, axs = plt.subplots(nrows=len(cmap_list), ncols=2)
  124. fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99,
  125. wspace=0.05)
  126. fig.suptitle(cmap_category + ' colormaps', fontsize=14, y=1.0, x=0.6)
  127. for ax, name in zip(axs, cmap_list):
  128. # Get RGB values for colormap.
  129. rgb = mpl.colormaps[name](x)[np.newaxis, :, :3]
  130. # Get colormap in CAM02-UCS colorspace. We want the lightness.
  131. lab = cspace_converter("sRGB1", "CAM02-UCS")(rgb)
  132. L = lab[0, :, 0]
  133. L = np.float32(np.vstack((L, L, L)))
  134. ax[0].imshow(gradient, aspect='auto', cmap=mpl.colormaps[name])
  135. ax[1].imshow(L, aspect='auto', cmap='binary_r', vmin=0., vmax=100.)
  136. pos = list(ax[0].get_position().bounds)
  137. x_text = pos[0] - 0.01
  138. y_text = pos[1] + pos[3]/2.
  139. fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)
  140. # Turn off *all* ticks & spines, not just the ones with colormaps.
  141. for ax in axs.flat:
  142. ax.set_axis_off()
  143. plt.show()
  144. for cmap_category, cmap_list in cmaps.items():
  145. plot_color_gradients(cmap_category, cmap_list)