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

Skip to content

Commit 031a0dc

Browse files
committed
scatter() should not rescale if norm is given
1 parent 04668b7 commit 031a0dc

File tree

5 files changed

+74
-30
lines changed

5 files changed

+74
-30
lines changed

doc/api/next_api_changes/deprecations.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,11 @@ Case-insensitive capstyles and joinstyles
5252
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5353
Please pass capstyles ("miter", "round", "bevel") and joinstyles ("butt",
5454
"round", "projecting") as lowercase.
55+
56+
Parameters *norm* and *vmin*/*vmax* should not be used simultaneously
57+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58+
Passing parameters *norm* and *vmin*/*vmax* simultaneously to functions using
59+
colormapping such as ``scatter()`` and ``imshow()`` is deprecated.
60+
Inestead of ``norm=LogNorm(), vmin=min_val, vmax=max_val`` pass
61+
``norm=LogNorm(min_val, max_val)``. *vmin* and *vmax* should only be used
62+
without setting *norm*.

lib/matplotlib/axes/_axes.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4324,8 +4324,8 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
43244324
vmin, vmax : scalar, default: None
43254325
*vmin* and *vmax* are used in conjunction with *norm* to normalize
43264326
luminance data. If None, the respective min and max of the color
4327-
array is used. *vmin* and *vmax* are ignored if you pass a *norm*
4328-
instance.
4327+
array is used.
4328+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
43294329
43304330
alpha : scalar, default: None
43314331
The alpha blending value, between 0 (transparent) and 1 (opaque).
@@ -4450,11 +4450,7 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
44504450
collection.set_array(c)
44514451
collection.set_cmap(cmap)
44524452
collection.set_norm(norm)
4453-
4454-
if vmin is not None or vmax is not None:
4455-
collection.set_clim(vmin, vmax)
4456-
else:
4457-
collection.autoscale_None()
4453+
collection._scale_norm(norm, vmin, vmax)
44584454

44594455
# Classic mode only:
44604456
# ensure there are margins to allow for the
@@ -4560,7 +4556,8 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
45604556
The colorbar range. If *None*, suitable min/max values are
45614557
automatically chosen by the `~.Normalize` instance (defaults to
45624558
the respective min/max values of the bins in case of the default
4563-
linear scaling). This is ignored if *norm* is given.
4559+
linear scaling).
4560+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
45644561
45654562
alpha : float between 0 and 1, optional
45664563
The alpha blending value, between 0 (transparent) and 1 (opaque).
@@ -4804,11 +4801,7 @@ def reduce_C_function(C: array) -> float
48044801
collection.set_norm(norm)
48054802
collection.set_alpha(alpha)
48064803
collection.update(kwargs)
4807-
4808-
if vmin is not None or vmax is not None:
4809-
collection.set_clim(vmin, vmax)
4810-
else:
4811-
collection.autoscale_None()
4804+
collection._scale_norm(norm, vmin, vmax)
48124805

48134806
corners = ((xmin, ymin), (xmax, ymax))
48144807
self.update_datalim(corners)
@@ -5530,7 +5523,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
55305523
When using scalar data and no explicit *norm*, *vmin* and *vmax*
55315524
define the data range that the colormap covers. By default,
55325525
the colormap covers the complete value range of the supplied
5533-
data. *vmin*, *vmax* are ignored if the *norm* parameter is used.
5526+
data. It is deprecated to use *vmin*/*vmax* when *norm* is given.
55345527
55355528
origin : {'upper', 'lower'}, optional
55365529
Place the [0, 0] index of the array in the upper left or lower left
@@ -5627,10 +5620,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
56275620
if im.get_clip_path() is None:
56285621
# image does not already have clipping set, clip to axes patch
56295622
im.set_clip_path(self.patch)
5630-
if vmin is not None or vmax is not None:
5631-
im.set_clim(vmin, vmax)
5632-
else:
5633-
im.autoscale_None()
5623+
im._scale_norm(norm, vmin, vmax)
56345624
im.set_url(url)
56355625

56365626
# update ax.dataLim, and, if autoscaling, set viewLim
@@ -5769,6 +5759,7 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
57695759
automatically chosen by the `~.Normalize` instance (defaults to
57705760
the respective min/max values of *C* in case of the default linear
57715761
scaling).
5762+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
57725763
57735764
edgecolors : {'none', None, 'face', color, color sequence}, optional
57745765
The color of the edges. Defaults to 'none'. Possible values:
@@ -5905,8 +5896,7 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
59055896
collection.set_array(C)
59065897
collection.set_cmap(cmap)
59075898
collection.set_norm(norm)
5908-
collection.set_clim(vmin, vmax)
5909-
collection.autoscale_None()
5899+
collection._scale_norm(norm, vmin, vmax)
59105900
self.grid(False)
59115901

59125902
x = X.compressed()
@@ -6000,6 +5990,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
60005990
automatically chosen by the `~.Normalize` instance (defaults to
60015991
the respective min/max values of *C* in case of the default linear
60025992
scaling).
5993+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
60035994
60045995
edgecolors : {'none', None, 'face', color, color sequence}, optional
60055996
The color of the edges. Defaults to 'none'. Possible values:
@@ -6119,8 +6110,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
61196110
collection.set_array(C)
61206111
collection.set_cmap(cmap)
61216112
collection.set_norm(norm)
6122-
collection.set_clim(vmin, vmax)
6123-
collection.autoscale_None()
6113+
collection._scale_norm(norm, vmin, vmax)
61246114

61256115
self.grid(False)
61266116

@@ -6234,6 +6224,7 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
62346224
automatically chosen by the `~.Normalize` instance (defaults to
62356225
the respective min/max values of *C* in case of the default linear
62366226
scaling).
6227+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
62376228
62386229
alpha : scalar, default: None
62396230
The alpha blending value, between 0 (transparent) and 1 (opaque).
@@ -6316,10 +6307,9 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
63166307
self.add_image(im)
63176308
ret = im
63186309

6319-
if vmin is not None or vmax is not None:
6320-
ret.set_clim(vmin, vmax)
6321-
elif np.ndim(C) == 2: # C.ndim == 3 is RGB(A) so doesn't need scaling.
6322-
ret.autoscale_None()
6310+
if np.ndim(C) == 2: # C.ndim == 3 is RGB(A) so doesn't need scaling.
6311+
ret._scale_norm(norm, vmin, vmax)
6312+
63236313
if ret.get_clip_path() is None:
63246314
# image does not already have clipping set, clip to axes patch
63256315
ret.set_clip_path(self.patch)

lib/matplotlib/cm.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,29 @@ def __init__(self, norm=None, cmap=None):
175175
self.colorbar = None
176176
self.update_dict = {'array': False}
177177

178+
def _scale_norm(self, norm, vmin, vmax):
179+
"""
180+
Helper for initial scaling.
181+
182+
Used by public functions that create a ScalarMappable and support
183+
parameters *vmin*, *vmax* and *norm*. This makes sure that a *norm*
184+
will take precedence over *vmin*, *vmax*.
185+
186+
Note that this method does not set the norm.
187+
"""
188+
if vmin is not None or vmax is not None:
189+
if norm is None:
190+
self.set_clim(vmin, vmax)
191+
else:
192+
self.set_clim(vmin, vmax)
193+
cbook.warn_deprecated(
194+
"3.3",
195+
message="Passing parameters norm and vmin/vmax "
196+
"simultaneously is deprecated. Please pass "
197+
"vmin/vmax directly to the norm when creating it.")
198+
else:
199+
self.autoscale_None()
200+
178201
def to_rgba(self, x, alpha=None, bytes=False, norm=True):
179202
"""
180203
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)