|
3 | 3 | Creating Colormaps in Matplotlib |
4 | 4 | ******************************** |
5 | 5 |
|
6 | | -Matplotlib colormaps are implimented as a class, which makes them quite |
7 | | -flexible, but opaque to users as to how to create and/or |
8 | | -manipulate them. This opacity is not helped in the library by the fact that |
9 | | -the named colormaps are accessed via `.matplotlib.cm.get_cmap` module, whereas |
10 | | -the colormap class itself is defined in `.matplotlib.colors.Colormap`! |
11 | | -
|
12 | | -Fortunately, the way to create colormaps is quite straight forward, by creating |
13 | | -an instance of class `.ListedColormap` using a Nx4 numpy array of values |
14 | | -between 0 and 1 to represent the RGBA values of the colormap. |
| 6 | +Creating and manipulating colormaps in Matplotlib is quite straight-forward |
| 7 | +using the class `.ListedColormap` and a Nx4 numpy array of values |
| 8 | +between 0 and 1 to represent the RGBA values of the colormap. There |
| 9 | +is also a `.LinearSegmentedColormap` class that allows colormaps to be |
| 10 | +specified with far fewer anchor points defining segments, and linearly |
| 11 | +interpolating between the anchor points. |
15 | 12 |
|
16 | 13 | Getting colormaps and accessing their values |
17 | 14 | ============================================ |
@@ -93,11 +90,12 @@ def plot_examples(cms): |
93 | 90 | plot_examples([viridis, newcmp]) |
94 | 91 |
|
95 | 92 | ############################################################################## |
96 | | -# We can easily reduce the range of a colormap; here we choose the middle |
97 | | -# 0.5 of the colormap. |
| 93 | +# We can easily reduce the dynamic range of a colormap; here we choose the |
| 94 | +# middle 0.5 of the colormap. However, we need to interpolate from a larger |
| 95 | +# colormap, otherwise the new colormap will have repeated values. |
98 | 96 |
|
99 | | -viridis = cm.get_cmap('viridis', 256) |
100 | | -newcmp = ListedColormap(viridis(np.linspace(0.25, 0.75, 256))) |
| 97 | +viridisBig = cm.get_cmap('viridis', 512) |
| 98 | +newcmp = ListedColormap(viridisBig(np.linspace(0.25, 0.75, 256))) |
101 | 99 | plot_examples([viridis, newcmp]) |
102 | 100 |
|
103 | 101 | ############################################################################## |
|
0 commit comments