Skip to content

Commit 9de0239

Browse files
committed
Add whats_new and update docstring
1 parent 6f0a342 commit 9de0239

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.

lib/matplotlib/colors.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,6 @@ def truncate(self, minval=0.0, maxval=1.0, name=None, N=None):
773773
def __getitem__(self, item):
774774
"""Advanced indexing for colorbars.
775775
776-
777776
Examples
778777
--------
779778
import matplotlib.pyplat as plt
@@ -792,7 +791,7 @@ def __getitem__(self, item):
792791
793792
# Same as above, but specify the number of points
794793
# using `np.mgrid` complex-indexing:
795-
new_cm = cmap[0.2:-0.4:1j * 64]
794+
new_cm = cmap[0.2:-0.4:64j]
796795
797796
# ### Int-style indexing
798797
# for int-style indexing, the values must be in [0, self.N]
@@ -805,7 +804,7 @@ def __getitem__(self, item):
805804
new_cm = cmap[12:-28:4]
806805
807806
# And so is `np.mgrid` complex-indexing (same as above)
808-
new_cm = cmap[12:-28:1j * 22]
807+
new_cm = cmap[12:-28:22j]
809808
810809
# ### Array/list-style indexing
811810
# In this case, you specify specific points in the colormap

0 commit comments

Comments
 (0)