Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a1a654

Browse files
committed
scatter() should not rescale if norm is given
1 parent 0cf0eda commit 5a1a654

File tree

5 files changed

+72
-30
lines changed

5 files changed

+72
-30
lines changed

doc/api/next_api_changes/deprecations.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,11 @@ This attribute is unused and deprecated.
6767
``widgets.TextBox.params_to_disable``
6868
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6969
This attribute is deprecated.
70+
71+
Parameters *norm* and *vmin*/*vmax* should not be used simultaneously
72+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73+
Passing parameters *norm* and *vmin*/*vmax* simultaneously to functions using
74+
colormapping such as ``scatter()`` and ``imshow()`` is deprecated.
75+
Inestead of ``norm=LogNorm(), vmin=min_val, vmax=max_val`` pass
76+
``norm=LogNorm(min_val, max_val)``. *vmin* and *vmax* should only be used
77+
without setting *norm*.

lib/matplotlib/axes/_axes.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4293,8 +4293,8 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
42934293
vmin, vmax : scalar, default: None
42944294
*vmin* and *vmax* are used in conjunction with *norm* to normalize
42954295
luminance data. If None, the respective min and max of the color
4296-
array is used. *vmin* and *vmax* are ignored if you pass a *norm*
4297-
instance.
4296+
array is used.
4297+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
42984298
42994299
alpha : scalar, default: None
43004300
The alpha blending value, between 0 (transparent) and 1 (opaque).
@@ -4415,11 +4415,7 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
44154415
collection.set_array(c)
44164416
collection.set_cmap(cmap)
44174417
collection.set_norm(norm)
4418-
4419-
if vmin is not None or vmax is not None:
4420-
collection.set_clim(vmin, vmax)
4421-
else:
4422-
collection.autoscale_None()
4418+
collection._scale_norm(norm, vmin, vmax)
44234419

44244420
# Classic mode only:
44254421
# ensure there are margins to allow for the
@@ -4525,7 +4521,8 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
45254521
The colorbar range. If *None*, suitable min/max values are
45264522
automatically chosen by the `~.Normalize` instance (defaults to
45274523
the respective min/max values of the bins in case of the default
4528-
linear scaling). This is ignored if *norm* is given.
4524+
linear scaling).
4525+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
45294526
45304527
alpha : float between 0 and 1, optional
45314528
The alpha blending value, between 0 (transparent) and 1 (opaque).
@@ -4769,11 +4766,7 @@ def reduce_C_function(C: array) -> float
47694766
collection.set_norm(norm)
47704767
collection.set_alpha(alpha)
47714768
collection.update(kwargs)
4772-
4773-
if vmin is not None or vmax is not None:
4774-
collection.set_clim(vmin, vmax)
4775-
else:
4776-
collection.autoscale_None()
4769+
collection._scale_norm(norm, vmin, vmax)
47774770

47784771
corners = ((xmin, ymin), (xmax, ymax))
47794772
self.update_datalim(corners)
@@ -5489,7 +5482,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
54895482
When using scalar data and no explicit *norm*, *vmin* and *vmax*
54905483
define the data range that the colormap covers. By default,
54915484
the colormap covers the complete value range of the supplied
5492-
data. *vmin*, *vmax* are ignored if the *norm* parameter is used.
5485+
data. It is deprecated to use *vmin*/*vmax* when *norm* is given.
54935486
54945487
origin : {'upper', 'lower'}, default: :rc:`image.origin`
54955488
Place the [0, 0] index of the array in the upper left or lower
@@ -5585,10 +5578,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
55855578
if im.get_clip_path() is None:
55865579
# image does not already have clipping set, clip to axes patch
55875580
im.set_clip_path(self.patch)
5588-
if vmin is not None or vmax is not None:
5589-
im.set_clim(vmin, vmax)
5590-
else:
5591-
im.autoscale_None()
5581+
im._scale_norm(norm, vmin, vmax)
55925582
im.set_url(url)
55935583

55945584
# update ax.dataLim, and, if autoscaling, set viewLim
@@ -5727,6 +5717,7 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
57275717
automatically chosen by the `~.Normalize` instance (defaults to
57285718
the respective min/max values of *C* in case of the default linear
57295719
scaling).
5720+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
57305721
57315722
edgecolors : {'none', None, 'face', color, color sequence}, optional
57325723
The color of the edges. Defaults to 'none'. Possible values:
@@ -5863,8 +5854,7 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
58635854
collection.set_array(C)
58645855
collection.set_cmap(cmap)
58655856
collection.set_norm(norm)
5866-
collection.set_clim(vmin, vmax)
5867-
collection.autoscale_None()
5857+
collection._scale_norm(norm, vmin, vmax)
58685858
self.grid(False)
58695859

58705860
x = X.compressed()
@@ -5958,6 +5948,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
59585948
automatically chosen by the `~.Normalize` instance (defaults to
59595949
the respective min/max values of *C* in case of the default linear
59605950
scaling).
5951+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
59615952
59625953
edgecolors : {'none', None, 'face', color, color sequence}, optional
59635954
The color of the edges. Defaults to 'none'. Possible values:
@@ -6077,8 +6068,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
60776068
collection.set_array(C)
60786069
collection.set_cmap(cmap)
60796070
collection.set_norm(norm)
6080-
collection.set_clim(vmin, vmax)
6081-
collection.autoscale_None()
6071+
collection._scale_norm(norm, vmin, vmax)
60826072

60836073
self.grid(False)
60846074

@@ -6192,6 +6182,7 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
61926182
automatically chosen by the `~.Normalize` instance (defaults to
61936183
the respective min/max values of *C* in case of the default linear
61946184
scaling).
6185+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
61956186
61966187
alpha : scalar, default: None
61976188
The alpha blending value, between 0 (transparent) and 1 (opaque).
@@ -6274,10 +6265,9 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
62746265
self.add_image(im)
62756266
ret = im
62766267

6277-
if vmin is not None or vmax is not None:
6278-
ret.set_clim(vmin, vmax)
6279-
elif np.ndim(C) == 2: # C.ndim == 3 is RGB(A) so doesn't need scaling.
6280-
ret.autoscale_None()
6268+
if np.ndim(C) == 2: # C.ndim == 3 is RGB(A) so doesn't need scaling.
6269+
ret._scale_norm(norm, vmin, vmax)
6270+
62816271
if ret.get_clip_path() is None:
62826272
# image does not already have clipping set, clip to axes patch
62836273
ret.set_clip_path(self.patch)

lib/matplotlib/cm.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,27 @@ def __init__(self, norm=None, cmap=None):
184184
self.colorbar = None
185185
self.update_dict = {'array': False}
186186

187+
def _scale_norm(self, norm, vmin, vmax):
188+
"""
189+
Helper for initial scaling.
190+
191+
Used by public functions that create a ScalarMappable and support
192+
parameters *vmin*, *vmax* and *norm*. This makes sure that a *norm*
193+
will take precedence over *vmin*, *vmax*.
194+
195+
Note that this method does not set the norm.
196+
"""
197+
if vmin is not None or vmax is not None:
198+
self.set_clim(vmin, vmax)
199+
if norm is not None:
200+
cbook.warn_deprecated(
201+
"3.3",
202+
message="Passing parameters norm and vmin/vmax "
203+
"simultaneously is deprecated. Please pass "
204+
"vmin/vmax directly to the norm when creating it.")
205+
else:
206+
self.autoscale_None()
207+
187208
def to_rgba(self, x, alpha=None, bytes=False, norm=True):
188209
"""
189210
Return a normalized rgba array corresponding to *x*.

lib/matplotlib/tests/test_axes.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,19 @@ def test_imshow_clip():
10041004
ax.imshow(r, clip_path=clip_path)
10051005

10061006

1007+
@check_figures_equal(extensions=["png"])
1008+
def test_imshow_norm_vminvmax(fig_test, fig_ref):
1009+
"""Parameters vmin, vmax should be ignored if norm is given."""
1010+
a = [[1, 2], [3, 4]]
1011+
ax = fig_ref.subplots()
1012+
ax.imshow(a, vmin=0, vmax=5)
1013+
ax = fig_test.subplots()
1014+
with pytest.warns(MatplotlibDeprecationWarning,
1015+
match="Passing parameters norm and vmin/vmax "
1016+
"simultaneously is deprecated."):
1017+
ax.imshow(a, norm=mcolors.Normalize(-10, 10), vmin=0, vmax=5)
1018+
1019+
10071020
@image_comparison(['polycollection_joinstyle'], remove_text=True)
10081021
def test_polycollection_joinstyle():
10091022
# Bug #2890979 reported by Matthew West
@@ -1973,6 +1986,19 @@ def test_scatter_no_invalid_color(self, fig_test, fig_ref):
19731986
ax = fig_ref.subplots()
19741987
ax.scatter([0, 2], [0, 2], c=[1, 2], s=[1, 3], cmap=cmap)
19751988

1989+
@check_figures_equal(extensions=["png"])
1990+
def test_scatter_norm_vminvmax(self, fig_test, fig_ref):
1991+
"""Parameters vmin, vmax should be ignored if norm is given."""
1992+
x = [1, 2, 3]
1993+
ax = fig_ref.subplots()
1994+
ax.scatter(x, x, c=x, vmin=0, vmax=5)
1995+
ax = fig_test.subplots()
1996+
with pytest.warns(MatplotlibDeprecationWarning,
1997+
match="Passing parameters norm and vmin/vmax "
1998+
"simultaneously is deprecated."):
1999+
ax.scatter(x, x, c=x, norm=mcolors.Normalize(-10, 10),
2000+
vmin=0, vmax=5)
2001+
19762002
@check_figures_equal(extensions=["png"])
19772003
def test_scatter_single_point(self, fig_test, fig_ref):
19782004
ax = fig_test.subplots()

lib/matplotlib/tri/tripcolor.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None,
120120
cbook._check_isinstance((Normalize, None), norm=norm)
121121
collection.set_cmap(cmap)
122122
collection.set_norm(norm)
123-
if vmin is not None or vmax is not None:
124-
collection.set_clim(vmin, vmax)
125-
else:
126-
collection.autoscale_None()
123+
collection._scale_norm(norm, vmin, vmax)
127124
ax.grid(False)
128125

129126
minx = tri.x.min()

0 commit comments

Comments
 (0)