|
| 1 | +Colormap Utilities |
| 2 | +------------------ |
| 3 | + |
| 4 | +Tools for joining, truncating, and resampling colormaps have been added. This grew out of https://gist.github.com/denis-bz/8052855, and http://stackoverflow.com/a/18926541/2121597. |
| 5 | + |
| 6 | + |
| 7 | +Joining Colormaps |
| 8 | +~~~~~~~~~~~~~~~~~ |
| 9 | + |
| 10 | +This includes the :func:`~matplotlib.colors.join_colormaps` function:: |
| 11 | + |
| 12 | + import matplotlib.pyplat as plt |
| 13 | + from matplotlib.colors import join_colormaps |
| 14 | + |
| 15 | + viridis = plt.get_cmap('viridis', 128) |
| 16 | + plasma = plt.get_cmap('plasma_r', 64) |
| 17 | + jet = plt.get_cmap('jet', 64) |
| 18 | + |
| 19 | + joined_cmap = join_colormaps((viridis, plasma, jet)) |
| 20 | + |
| 21 | +This functionality has also been incorporated into the :meth:`~matplotlib.colors.colormap.join` and `~matplotlib.colors.colormap.__add__` methods, so that you can do things like:: |
| 22 | + |
| 23 | + plasma_jet = plasma.join(jet) |
| 24 | + |
| 25 | + joined_cmap = viridis + plasma + jet # Same as `join_colormaps` function call above |
| 26 | + |
| 27 | +Truncating and resampling colormaps |
| 28 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 29 | + |
| 30 | +A :meth:`~matplotlib.colors.colormap.truncate` method has also been added:: |
| 31 | + |
| 32 | + sub_viridis = viridis.truncate(0.3, 0.8) |
| 33 | + |
| 34 | +This gives a new colormap that goes from 30% to 80% of viridis. This functionality has also been implemented in the `~matplotlib.colors.colormap.__getitem__` method, so that the same colormap can be created by:: |
| 35 | + |
| 36 | + sub_viridis = viridis[0.3:0.8] |
| 37 | + |
| 38 | +The `~matplotlib.colors.colormap.__getitem__` method also supports a range of other 'advanced indexing' options, including integer slice indexing:: |
| 39 | + |
| 40 | + sub_viridis2 = viridis[10:90:2] |
| 41 | + |
| 42 | +integer list indexing, which may be particularly useful for creating discrete (low-N) colormaps:: |
| 43 | + |
| 44 | + sub_viridis3 = viridis[[4, 35, 59, 90, 110]] |
| 45 | + |
| 46 | +and `numpy.mgrid` style complex indexing:: |
| 47 | + |
| 48 | + sub_viridis4 = viridis[0.2:0.4:64j] |
| 49 | + |
| 50 | +See the `~matplotlib.colors.colormap.__getitem__` documentation for more details and examples of how to use these advanced indexing options. |
| 51 | + |
| 52 | +Together, the join and truncate/resample methods allow the user to quickly construct new colormaps from existing ones:: |
| 53 | + |
| 54 | + new_cm = viridis[0.5:] + plasma[:0.3] + jet[0.2:0.5:64j] |
| 55 | + |
| 56 | +I doubt this colormap will ever be useful to someone, but hopefully it gives you the idea. |
0 commit comments