Skip to content

Commit b6f3650

Browse files
authored
Merge pull request matplotlib#30582 from timhoffm/no-cmap-setters
MNT: Do not use colormap setters in tests
2 parents 9da2022 + a159804 commit b6f3650

File tree

8 files changed

+25
-48
lines changed

8 files changed

+25
-48
lines changed

lib/matplotlib/contour.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -735,13 +735,12 @@ def __init__(self, ax, *args,
735735
i0 = 1
736736

737737
cmap = mcolors.ListedColormap(
738-
cbook._resize_sequence(color_sequence[i0:], ncolors))
739-
740-
if use_set_under_over:
741-
if self._extend_min:
742-
cmap.set_under(color_sequence[0])
743-
if self._extend_max:
744-
cmap.set_over(color_sequence[-1])
738+
cbook._resize_sequence(color_sequence[i0:], ncolors),
739+
under=(color_sequence[0]
740+
if use_set_under_over and self._extend_min else None),
741+
over=(color_sequence[-1]
742+
if use_set_under_over and self._extend_max else None),
743+
)
745744

746745
# label lists must be initialized here
747746
self.labelTexts = []

lib/matplotlib/tests/test_axes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,8 +2983,7 @@ def test_scatter_invalid_color(self, fig_test, fig_ref):
29832983
def test_scatter_no_invalid_color(self, fig_test, fig_ref):
29842984
# With plotnonfinite=False we plot only 2 points.
29852985
ax = fig_test.subplots()
2986-
cmap = mpl.colormaps["viridis"].resampled(16)
2987-
cmap.set_bad("k", 1)
2986+
cmap = mpl.colormaps["viridis"].resampled(16).with_extremes(bad="k")
29882987
ax.scatter(range(4), range(4),
29892988
c=[1, np.nan, 2, np.nan], s=[1, 2, 3, 4],
29902989
cmap=cmap, plotnonfinite=False)

lib/matplotlib/tests/test_colorbar.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -946,9 +946,10 @@ def test_proportional_colorbars():
946946

947947
levels = [-1.25, -0.5, -0.125, 0.125, 0.5, 1.25]
948948
cmap = mcolors.ListedColormap(
949-
['0.3', '0.5', 'white', 'lightblue', 'steelblue'])
950-
cmap.set_under('darkred')
951-
cmap.set_over('crimson')
949+
['0.3', '0.5', 'white', 'lightblue', 'steelblue'],
950+
under='darkred',
951+
over='crimson',
952+
)
952953
norm = mcolors.BoundaryNorm(levels, cmap.N)
953954

954955
extends = ['neither', 'both']

lib/matplotlib/tests/test_colors.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,9 @@ def test_resampled():
5353
colorlist[:, 1] = 0.2
5454
colorlist[:, 2] = np.linspace(1, 0, n)
5555
colorlist[:, 3] = 0.7
56-
lsc = mcolors.LinearSegmentedColormap.from_list('lsc', colorlist)
57-
lc = mcolors.ListedColormap(colorlist)
58-
# Set some bad values for testing too
59-
for cmap in [lsc, lc]:
60-
cmap.set_under('r')
61-
cmap.set_over('g')
62-
cmap.set_bad('b')
56+
lsc = mcolors.LinearSegmentedColormap.from_list(
57+
'lsc', colorlist, under='red', over='green', bad='blue')
58+
lc = mcolors.ListedColormap(colorlist, under='red', over='green', bad='blue')
6359
lsc3 = lsc.resampled(3)
6460
lc3 = lc.resampled(3)
6561
expected = np.array([[0.0, 0.2, 1.0, 0.7],
@@ -371,9 +367,7 @@ def test_BoundaryNorm():
371367
assert_array_equal(mynorm(x), ref)
372368

373369
# Without interpolation
374-
cmref = mcolors.ListedColormap(['blue', 'red'])
375-
cmref.set_over('black')
376-
cmref.set_under('white')
370+
cmref = mcolors.ListedColormap(['blue', 'red'], under='white', over='black')
377371
cmshould = mcolors.ListedColormap(['white', 'blue', 'red', 'black'])
378372

379373
assert mcolors.same_color(cmref.get_over(), 'black')
@@ -395,8 +389,7 @@ def test_BoundaryNorm():
395389
assert_array_equal(cmshould(mynorm(x)), cmref(refnorm(x)))
396390

397391
# Just min
398-
cmref = mcolors.ListedColormap(['blue', 'red'])
399-
cmref.set_under('white')
392+
cmref = mcolors.ListedColormap(['blue', 'red'], under='white')
400393
cmshould = mcolors.ListedColormap(['white', 'blue', 'red'])
401394

402395
assert mcolors.same_color(cmref.get_under(), 'white')
@@ -413,8 +406,7 @@ def test_BoundaryNorm():
413406
assert_array_equal(cmshould(mynorm(x)), cmref(refnorm(x)))
414407

415408
# Just max
416-
cmref = mcolors.ListedColormap(['blue', 'red'])
417-
cmref.set_over('black')
409+
cmref = mcolors.ListedColormap(['blue', 'red'], over='black')
418410
cmshould = mcolors.ListedColormap(['blue', 'red', 'black'])
419411

420412
assert mcolors.same_color(cmref.get_over(), 'black')
@@ -928,7 +920,7 @@ def test_cmap_and_norm_from_levels_and_colors2():
928920
for extend, i1, cases in tests:
929921
cmap, norm = mcolors.from_levels_and_colors(levels, colors[0:i1],
930922
extend=extend)
931-
cmap.set_bad(bad)
923+
cmap = cmap.with_extremes(bad=bad)
932924
for d_val, expected_color in cases.items():
933925
if d_val == masked_value:
934926
d_val = np.ma.array([1], mask=True)

lib/matplotlib/tests/test_contour.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,7 @@ def test_contourf_legend_elements():
616616
cs = plt.contourf(h, levels=[10, 30, 50],
617617
colors=['#FFFF00', '#FF00FF', '#00FFFF'],
618618
extend='both')
619-
cs.cmap.set_over('red')
620-
cs.cmap.set_under('blue')
619+
cs.cmap = cs.cmap.with_extremes(over='red', under='blue')
621620
cs.changed()
622621
artists, labels = cs.legend_elements()
623622
assert labels == ['$x \\leq -1e+250s$',

lib/matplotlib/tests/test_image.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from contextlib import ExitStack
2-
from copy import copy
32
import functools
43
import io
54
import os
@@ -1194,8 +1193,7 @@ def test_image_array_alpha_validation():
11941193

11951194
@mpl.style.context('mpl20')
11961195
def test_exact_vmin():
1197-
cmap = copy(mpl.colormaps["autumn_r"])
1198-
cmap.set_under(color="lightgrey")
1196+
cmap = mpl.colormaps["autumn_r"].with_extremes(under="lightgrey")
11991197

12001198
# make the image exactly 190 pixels wide
12011199
fig = plt.figure(figsize=(1.9, 0.1), dpi=100)
@@ -1469,9 +1467,7 @@ def test_rgba_antialias():
14691467
aa[70:90, 195:215] = 1e6
14701468
aa[20:30, 195:215] = -1e6
14711469

1472-
cmap = plt.colormaps["RdBu_r"]
1473-
cmap.set_over('yellow')
1474-
cmap.set_under('cyan')
1470+
cmap = plt.colormaps["RdBu_r"].with_extremes(over='yellow', under='cyan')
14751471

14761472
axs = axs.flatten()
14771473
# zoom in
@@ -1726,8 +1722,7 @@ def test_downsampling_speckle():
17261722
axs = axs.flatten()
17271723
img = ((np.arange(1024).reshape(-1, 1) * np.ones(720)) // 50).T
17281724

1729-
cm = plt.get_cmap("viridis")
1730-
cm.set_over("m")
1725+
cm = plt.get_cmap("viridis").with_extremes(over="m")
17311726
norm = colors.LogNorm(vmin=3, vmax=11)
17321727

17331728
# old default cannot be tested because it creates over/under speckles

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,11 @@ def test_contourf3d_extend(fig_test, fig_ref, extend, levels):
285285
# Z is in the range [0, 8]
286286
Z = X**2 + Y**2
287287

288-
# Manually set the over/under colors to be the end of the colormap
289-
cmap = mpl.colormaps['viridis'].copy()
290-
cmap.set_under(cmap(0))
291-
cmap.set_over(cmap(255))
292-
# Set vmin/max to be the min/max values plotted on the reference image
293-
kwargs = {'vmin': 1, 'vmax': 7, 'cmap': cmap}
294-
295288
ax_ref = fig_ref.add_subplot(projection='3d')
296-
ax_ref.contourf(X, Y, Z, levels=[0, 2, 4, 6, 8], **kwargs)
289+
ax_ref.contourf(X, Y, Z, levels=[0, 2, 4, 6, 8], vmin=1, vmax=7)
297290

298291
ax_test = fig_test.add_subplot(projection='3d')
299-
ax_test.contourf(X, Y, Z, levels, extend=extend, **kwargs)
292+
ax_test.contourf(X, Y, Z, levels, extend=extend, vmin=1, vmax=7)
300293

301294
for ax in [ax_ref, ax_test]:
302295
ax.set_xlim(-2, 2)

lib/mpl_toolkits/mplot3d/tests/test_legend3d.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ def test_contourf_legend_elements():
9090
cs = ax.contourf(x, y, h, levels=[10, 30, 50],
9191
colors=['#FFFF00', '#FF00FF', '#00FFFF'],
9292
extend='both')
93-
cs.cmap.set_over('red')
94-
cs.cmap.set_under('blue')
93+
cs.cmap = cs.cmap.with_extremes(over='red', under='blue')
9594
cs.changed()
9695
artists, labels = cs.legend_elements()
9796
assert labels == ['$x \\leq -1e+250s$',

0 commit comments

Comments
 (0)