8000 Merge pull request #5283 from mdboom/colormap-fixes · matplotlib/matplotlib@e7e0807 · GitHub
[go: up one dir, main page]

Skip to content

Commit e7e0807

Browse files
committed
Merge pull request #5283 from mdboom/colormap-fixes
Make new colormaps full-fledged citizens
1 parent 99fe9a2 commit e7e0807

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

examples/pylab_examples/demo_bboximage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
a = np.linspace(0, 1, 256).reshape(1, -1)
2626
a = np.vstack((a, a))
2727

28-
maps = sorted(m for m in plt.cm.datad if not m.endswith("_r"))
28+
maps = sorted(m for m in plt.cm.cmap_d if not m.endswith("_r"))
2929
#nmaps = len(maps) + 1
3030

3131
#fig.subplots_adjust(top=0.99, bottom=0.01, left=0.2, right=0.99)

lib/matplotlib/cm.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ def get_cmap(name=None, lut=None):
146146
returned.
147147
148148
If *lut* is not None it must be an integer giving the number of
149-
entries desired in the lookup table, and *name* must be a
150-
standard mpl colormap name with a corresponding data dictionary
151-
in *datad*.
149+
entries desired in the lookup table, and *name* must be a standard
150+
mpl colormap name.
152151
"""
153152
if name is None:
154153
name = mpl.rcParams['image.cmap']
@@ -159,12 +158,12 @@ def get_cmap(name=None, lut=None):
159158
if name in cmap_d:
160159
if lut is None:
161160
return cmap_d[name]
162-
elif name in datad:
163-
return _generate_cmap(name, lut)
161+
else:
162+
return cmap_d[name]._resample(lut)
164163
else:
165164
raise ValueError(
166165
"Colormap %s is not recognized. Possible values are: %s"
167-
% (name, ', '.join(cmap_d.keys())))
166+
% (name, ', '.join(sorted(cmap_d.keys()))))
168167

169168

170169
class ScalarMappable(object):

lib/matplotlib/colors.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,12 @@ def is_gray(self):
659659
return (np.alltrue(self._lut[:, 0] == self._lut[:, 1]) and
660660
np.alltrue(self._lut[:, 0] == self._lut[:, 2]))
661661

662+
def _resample(self, lutsize):
663+
"""
664+
Return a new color map with *lutsize* entries.
665+
"""
666+
raise NotImplementedError()
667+
662668

663669
class LinearSegmentedColormap(Colormap):
664670
"""Colormap objects based on lookup tables using linear segments.
@@ -772,6 +778,12 @@ def from_list(name, colors, N=256, gamma=1.0):
772778

773779
return LinearSegmentedColormap(name, cdict, N, gamma)
774780

781+
def _resample(self, lutsize):
782+
"""
783+
Return a new color map with *lutsize* entries.
784+
"""
785+
return LinearSegmentedColormap(self.name, self._segmentdata, lutsize)
786+
775787

776788
class ListedColormap(Colormap):
777789
"""Colormap object generated from a list of colors.
@@ -836,6 +848,12 @@ def _init(self):
836848
self._isinit = True
837849
self._set_extremes()
838850

851+
def _resample(self, lutsize):
852+
"""
853+
Return a new color map with *lutsize* entries.
854+
"""
855+
return ListedColormap(self.name, self.colors, lutsize)
856+
839857

840858
class Normalize(object):
841859
"""

0 commit comments

Comments
 (0)
0