Skip to content

Commit 1e40f41

Browse files
committed
ENH: Make the ability to resample interpolated colormaps public
1 parent 2da3401 commit 1e40f41

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Make the resample method on `.Colormap` instances public
2+
--------------------------------------------------------
3+
4+
On `.LinearSegmentedColormap` and `.ListedColormap` the previously private
5+
``_resample`` method is made public as `.Colormap.resampled`. This method
6+
creates a new `.Colormap` instance with the specified lookup table size.

lib/matplotlib/cm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def get_cmap(name=None, lut=None):
269269
if lut is None:
270270
return _colormaps[name]
271271
else:
272-
return _colormaps[name]._resample(lut)
272+
return _colormaps[name].resample(lut)
273273

274274

275275
def unregister_cmap(name):

lib/matplotlib/colors.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,17 @@ def is_gray(self):
854854
return (np.all(self._lut[:, 0] == self._lut[:, 1]) and
855855
np.all(self._lut[:, 0] == self._lut[:, 2]))
856856

857-
def _resample(self, lutsize):
857+
def resampled(self, lutsize):
858858
"""Return a new colormap with *lutsize* entries."""
859+
if hasattr(self, '_resample'):
860+
_api.warn_external(
861+
"The ability to resample a color map is now public API "
862+
f"However the class {type(self)} still only implements "
863+
"the previous private _resample method. Please update "
864+
"your class."
865+
)
866+
return self._resample(lutsize)
867+
859868
raise NotImplementedError()
860869

861870
def reversed(self, name=None):
@@ -1050,7 +1059,7 @@ def from_list(name, colors, N=256, gamma=1.0):
10501059

10511060
return LinearSegmentedColormap(name, cdict, N, gamma)
10521061

1053-
def _resample(self, lutsize):
1062+
def resampled(self, lutsize):
10541063
"""Return a new colormap with *lutsize* entries."""
10551064
new_cmap = LinearSegmentedColormap(self.name, self._segmentdata,
10561065
lutsize)
@@ -1154,7 +1163,7 @@ def _init(self):
11541163
self._isinit = True
11551164
self._set_extremes()
11561165

1157-
def _resample(self, lutsize):
1166+
def resampled(self, lutsize):
11581167
"""Return a new colormap with *lutsize* entries."""
11591168
colors = self(np.linspace(0, 1, lutsize))
11601169
new_cmap = ListedColormap(colors, name=self.name)

lib/matplotlib/tests/test_colors.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_create_lookup_table(N, result):
3131

3232
def test_resample():
3333
"""
34-
GitHub issue #6025 pointed to incorrect ListedColormap._resample;
34+
GitHub issue #6025 pointed to incorrect ListedColormap.resample;
3535
here we test the method for LinearSegmentedColormap as well.
3636
"""
3737
n = 101
@@ -47,8 +47,8 @@ def test_resample():
4747
cmap.set_under('r')
4848
cmap.set_over('g')
4949
cmap.set_bad('b')
50-
lsc3 = lsc._resample(3)
51-
lc3 = lc._resample(3)
50+
lsc3 = lsc.resample(3)
51+
lc3 = lc.resample(3)
5252
expected = np.array([[0.0, 0.2, 1.0, 0.7],
5353
[0.5, 0.2, 0.5, 0.7],
5454
[1.0, 0.2, 0.0, 0.7]], float)

0 commit comments

Comments
 (0)