|
| 1 | +""" |
| 2 | +================== |
| 3 | +Colormap reference |
| 4 | +================== |
| 5 | +
|
| 6 | +Reference for colormaps included with Matplotlib. |
| 7 | +
|
| 8 | +This reference example shows all colormaps included with Matplotlib. Note that |
| 9 | +any colormap listed here can be reversed by appending "_r" (e.g., "pink_r"). |
| 10 | +These colormaps are divided into the following categories: |
| 11 | +
|
| 12 | +Sequential: |
| 13 | + These colormaps are approximately monochromatic colormaps varying smoothly |
| 14 | + between two color tones---usually from low saturation (e.g. white) to high |
| 15 | + saturation (e.g. a bright blue). Sequential colormaps are ideal for |
| 16 | + representing most scientific data since they show a clear progression from |
| 17 | + low-to-high values. |
| 18 | +
|
| 19 | +Diverging: |
| 20 | + These colormaps have a median value (usually light in color) and vary |
| 21 | + smoothly to two different color tones at high and low values. Diverging |
| 22 | + colormaps are ideal when your data has a median value that is significant |
| 23 | + (e.g. 0, such that positive and negative values are represented by |
| 24 | + different colors of the colormap). |
| 25 | +
|
| 26 | +Qualitative: |
| 27 | + These colormaps vary rapidly in color. Qualitative colormaps are useful |
| 28 | +for |
| 29 | + choosing a set of discrete colors. For example:: |
| 30 | +
|
| 31 | + color_list = plt.cm.Set3(np.linspace(0, 1, 12)) |
| 32 | +
|
| 33 | + gives a list of RGB colors that are good for plotting a series of lines on |
| 34 | + a dark background. |
| 35 | +
|
| 36 | +Miscellaneous: |
| 37 | + Colormaps that don't fit into the categories above. |
| 38 | +
|
| 39 | +""" |
| 40 | +import numpy as np |
| 41 | +import matplotlib.pyplot as plt |
| 42 | + |
| 43 | + |
| 44 | +# Have colormaps separated into categories: |
| 45 | +# http://matplotlib.org/examples/color/colormaps_reference.html |
| 46 | +cmaps = [('Perceptually Uniform Sequential', [ |
| 47 | + 'viridis', 'plasma', 'inferno', 'magma']), |
| 48 | + ('Sequential', [ |
| 49 | + 'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds', |
| 50 | + 'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu', |
| 51 | + 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']), |
| 52 | + ('Sequential (2)', [ |
| 53 | + 'binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink', |
| 54 | + 'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia', |
| 55 | + 'hot', 'afmhot', 'gist_heat', 'copper']), |
| 56 | + ('Diverging', [ |
| 57 | + 'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', |
| 58 | + 'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']), |
| 59 | + ('Qualitative', [ |
| 60 | + 'Pastel1', 'Pastel2', 'Paired', 'Accent', |
| 61 | + 'Dark2', 'Set1', 'Set2', 'Set3', |
| 62 | + 'tab10', 'tab20', 'tab20b', 'tab20c']), |
| 63 | + ('Miscellaneous', [ |
| 64 | + 'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern', |
| 65 | + 'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'hsv', |
| 66 | + 'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar'])] |
| 67 | + |
| 68 | + |
| 69 | +nrows = max(len(cmap_list) for cmap_category, cmap_list in cmaps) |
| 70 | +gradient = np.linspace(0, 1, 256) |
| 71 | +gradient = np.vstack((gradient, gradient)) |
| 72 | + |
| 73 | + |
| 74 | +def plot_color_gradients(cmap_category, cmap_list, nrows): |
| 75 | + fig, axes = plt.subplots(nrows=nrows) |
| 76 | + fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99) |
| 77 | + axes[0].set_title(cmap_category + ' colormaps', fontsize=14) |
| 78 | + |
| 79 | + for ax, name in zip(axes, cmap_list): |
| 80 | + ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name)) |
| 81 | + pos = list(ax.get_position().bounds) |
| 82 | + x_text = pos[0] - 0.01 |
| 83 | + y_text = pos[1] + pos[3]/2. |
| 84 | + fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10) |
| 85 | + |
| 86 | + # Turn off *all* ticks & spines, not just the ones with colormaps. |
| 87 | + for ax in axes: |
| 88 | + ax.set_axis_off() |
| 89 | + |
| 90 | + |
| 91 | +for cmap_category, cmap_list in cmaps: |
| 92 | + plot_color_gradients(cmap_category, cmap_list, nrows) |
| 93 | + |
| 94 | +plt.show() |
0 commit comments