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

Skip to content

Commit d2c7af2

Browse files
committed
scatter() should not rescale if norm is given
1 parent 24caa1b commit d2c7af2

File tree

5 files changed

+71
-30
lines changed

5 files changed

+71
-30
lines changed

doc/api/next_api_changes/deprecations.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@ Flags containing "U" passed to `.cbook.to_filehandle` and `.cbook.open_file_cm`
4141
Please remove "U" from flags passed to `.cbook.to_filehandle` and
4242
`.cbook.open_file_cm`. This is consistent with their removal from `open` in
4343
Python 3.9.
44+
45+
Flags containing "U" passed to `.cbook.to_filehandle` and `.cbook.open_file_cm`
46+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47+
Passing parameters *norm* and *vmin*/*vmax* simultaneously to functions using
48+
colormapping such as ``scatter()`` and ``imshow()`` is deprecated.
49+
Inestead of ``norm=LogNorm(), vmin=min_val, vmax=max_val`` pass
50+
``norm=LogNorm(min_val, max_val)``. *vmin* and *vmax* should only be used
51+
without setting *norm*.

lib/matplotlib/axes/_axes.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4344,8 +4344,8 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
43444344
vmin, vmax : scalar, optional, default: None
43454345
*vmin* and *vmax* are used in conjunction with *norm* to normalize
43464346
luminance data. If None, the respective min and max of the color
4347-
array is used. *vmin* and *vmax* are ignored if you pass a *norm*
4348-
instance.
4347+
array is used.
4348+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
43494349
43504350
alpha : scalar, optional, default: None
43514351
The alpha blending value, between 0 (transparent) and 1 (opaque).
@@ -4470,11 +4470,7 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
44704470
collection.set_array(c)
44714471
collection.set_cmap(cmap)
44724472
collection.set_norm(norm)
4473-
4474-
if vmin is not None or vmax is not None:
4475-
collection.set_clim(vmin, vmax)
4476-
else:
4477-
collection.autoscale_None()
4473+
collection._scale_norm(norm, vmin, vmax)
44784474

44794475
# Classic mode only:
44804476
# ensure there are margins to allow for the
@@ -4580,7 +4576,8 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
45804576
The colorbar range. If *None*, suitable min/max values are
45814577
automatically chosen by the `~.Normalize` instance (defaults to
45824578
the respective min/max values of the bins in case of the default
4583-
linear scaling). This is ignored if *norm* is given.
4579+
linear scaling).
4580+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
45844581
45854582
alpha : float between 0 and 1, optional
45864583
The alpha blending value, between 0 (transparent) and 1 (opaque).
@@ -4824,11 +4821,7 @@ def reduce_C_function(C: array) -> float
48244821
collection.set_norm(norm)
48254822
collection.set_alpha(alpha)
48264823
collection.update(kwargs)
4827-
4828-
if vmin is not None or vmax is not None:
4829-
collection.set_clim(vmin, vmax)
4830-
else:
4831-
collection.autoscale_None()
4824+
collection._scale_norm(norm, vmin, vmax)
48324825

48334826
corners = ((xmin, ymin), (xmax, ymax))
48344827
self.update_datalim(corners)
@@ -5555,7 +5548,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
55555548
When using scalar data and no explicit *norm*, *vmin* and *vmax*
55565549
define the data range that the colormap covers. By default,
55575550
the colormap covers the complete value range of the supplied
5558-
data. *vmin*, *vmax* are ignored if the *norm* parameter is used.
5551+
data. It is deprecated to use *vmin*/*vmax* when *norm* is given.
55595552
55605553
origin : {'upper', 'lower'}, optional
55615554
Place the [0, 0] index of the array in the upper left or lower left
@@ -5652,10 +5645,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
56525645
if im.get_clip_path() is None:
56535646
# image does not already have clipping set, clip to axes patch
56545647
im.set_clip_path(self.patch)
5655-
if vmin is not None or vmax is not None:
5656-
im.set_clim(vmin, vmax)
5657-
else:
5658-
im.autoscale_None()
5648+
im._scale_norm(norm, vmin, vmax)
56595649
im.set_url(url)
56605650

56615651
# update ax.dataLim, and, if autoscaling, set viewLim
@@ -5794,6 +5784,7 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
57945784
automatically chosen by the `~.Normalize` instance (defaults to
57955785
the respective min/max values of *C* in case of the default linear
57965786
scaling).
5787+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
57975788
57985789
edgecolors : {'none', None, 'face', color, color sequence}, optional
57995790
The color of the edges. Defaults to 'none'. Possible values:
@@ -5930,8 +5921,7 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
59305921
collection.set_array(C)
59315922
collection.set_cmap(cmap)
59325923
collection.set_norm(norm)
5933-
collection.set_clim(vmin, vmax)
5934-
collection.autoscale_None()
5924+
collection._scale_norm(norm, vmin, vmax)
59355925
self.grid(False)
59365926

59375927
x = X.compressed()
@@ -6025,6 +6015,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
60256015
automatically chosen by the `~.Normalize` instance (defaults to
60266016
the respective min/max values of *C* in case of the default linear
60276017
scaling).
6018+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
60286019
60296020
edgecolors : {'none', None, 'face', color, color sequence}, optional
60306021
The color of the edges. Defaults to 'none'. Possible values:
@@ -6144,8 +6135,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
61446135
collection.set_array(C)
61456136
collection.set_cmap(cmap)
61466137
collection.set_norm(norm)
6147-
collection.set_clim(vmin, vmax)
6148-
collection.autoscale_None()
6138+
collection._scale_norm(norm, vmin, vmax)
61496139

61506140
self.grid(False)
61516141

@@ -6259,6 +6249,7 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
62596249
automatically chosen by the `~.Normalize` instance (defaults to
62606250
the respective min/max values of *C* in case of the default linear
62616251
scaling).
6252+
It is deprecated to use *vmin*/*vmax* when *norm* is given.
62626253
62636254
alpha : scalar, optional, default: None
62646255
The alpha blending value, between 0 (transparent) and 1 (opaque).
@@ -6341,10 +6332,9 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
63416332
self.add_image(im)
63426333
ret = im
63436334

6344-
if vmin is not None or vmax is not None:
6345-
ret.set_clim(vmin, vmax)
6346-
elif np.ndim(C) == 2: # C.ndim == 3 is RGB(A) so doesn't need scaling.
6347-
ret.autoscale_None()
6335+
if np.ndim(C) == 2: # C.ndim == 3 is RGB(A) so doesn't need scaling.
6336+
ret._scale_norm(norm, vmin, vmax)
6337+
63486338
if ret.get_clip_path() is None:
63496339
# image does not already have clipping set, clip to axes patch
63506340
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,17 @@ def test_imshow_clip():
10041004
ax.imshow(r, clip_path=clip_path)
10051005

10061006

1007+
@check_figures_equal(extensions=["png"])
1008+
def test_impshow_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, norm=mcolors.Normalize(-10, 10))
1013+
ax = fig_test.subplots()
1014+
with pytest.warns(UserWarning, match="Parameters vmin, vmax are ignored"):
1015+
ax.imshow(a, norm=mcolors.Normalize(-10, 10), vmin=0, vmax=5)
1016+
1017+
10071018
@image_comparison(['polycollection_joinstyle'], remove_text=True)
10081019
def test_polycollection_joinstyle():
10091020
# Bug #2890979 reported by Matthew West
@@ -1973,6 +1984,18 @@ def test_scatter_no_invalid_color(self, fig_test, fig_ref):
19731984
ax = fig_ref.subplots()
19741985
ax.scatter([0, 2], [0, 2], c=[1, 2], s=[1, 3], cmap=cmap)
19751986

1987+
@check_figures_equal(extensions=["png"])
1988+
def test_scatter_norm_vminvmax(self, fig_test, fig_ref):
1989+
"""Parameters vmin, vmax should be ignored if norm is given."""
1990+
x = [1, 2, 3]
1991+
ax = fig_ref.subplots()
1992+
ax.scatter(x, x, c=x, norm=mcolors.Normalize(-10, 10))
1993+
ax = fig_test.subplots()
1994+
with pytest.warns(UserWarning,
1995+
match="Parameters vmin, vmax are ignored"):
1996+
ax.scatter(x, x, c=x, norm=mcolors.Normalize(-10, 10),
1997+
vmin=0, vmax=5)
1998+
19761999
@check_figures_equal(extensions=["png"])
19772000
def test_scatter_single_point(self, fig_test, fig_ref):
19782001
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)