8000 scatter() should not rescale if norm is given · matplotlib/matplotlib@f0e007c · GitHub
[go: up one dir, main page]

Skip to content

Commit f0e007c

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

File tree

4 files changed

+45
-22
lines changed

4 files changed

+45
-22
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -4824,11 +4820,7 @@ def reduce_C_function(C: array) -> float
48244820
collection.set_norm(norm)
48254821
collection.set_alpha(alpha)
48264822
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()
4823+
collection._scale_norm(norm, vmin, vmax)
48324824

48334825
corners = ((xmin, ymin), (xmax, ymax))
48344826
self.update_datalim(corners)
@@ -5652,10 +5644,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
56525644
if im.get_clip_path() is None:
56535645
# image does not already have clipping set, clip to axes patch
56545646
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()
5647+
im._scale_norm(norm, vmin, vmax)
56595648
im.set_url(url)
56605649

56615650
# update ax.dataLim, and, if autoscaling, set viewLim
@@ -6341,10 +6330,9 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
63416330
self.add_image(im)
63426331
ret = im
63436332

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()
6333+
if np.ndim(C) == 2: # C.ndim == 3 is RGB(A) so doesn't need scaling.
6334+
ret._scale_norm(norm, vmin, vmax)
6335+
63486336
if ret.get_clip_path() is None:
63496337
# image does not already have clipping set, clip to axes patch
63506338
ret.set_clip_path(self.patch)

lib/matplotlib/cm.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,25 @@ 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+
cbook._warn_external("Parameters vmin, vmax are ignored "
193+
"because norm is given")
194+
else:
195+
self.autoscale_None()
196+
178197
def to_rgba(self, x, alpha=None, bytes=False, norm=True):
179198
"""
180199
Return a normalized rgba array corresponding to *x*.

lib/matplotlib/tests/test_axes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,16 @@ 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+
ax.imshow(a, norm=mcolors.Normalize(-10, 10), vmin=0, vmax=5)
1015+
1016+
10071017
@image_comparison(['polycollection_joinstyle'], remove_text=True)
10081018
def test_polycollection_joinstyle():
10091019
# Bug #2890979 reported by Matthew West
@@ -1973,6 +1983,15 @@ def test_scatter_no_invalid_color(self, fig_test, fig_ref):
19731983
ax = fig_ref.subplots()
19741984
ax.scatter([0, 2], [0, 2], c=[1, 2], s=[1, 3], cmap=cmap)
19751985

1986+
@check_figures_equal(extensions=["png"])
1987+
def test_scatter_norm_vminvmax(self, fig_test, fig_ref):
1988+
"""Parameters vmin, vmax should be ignored if norm is given."""
1989+
x = [1, 2, 3]
1990+
ax = fig_ref.subplots()
1991+
ax.scatter(x, x, c=x, norm=mcolors.Normalize(-10, 10))
1992+
ax = fig_test.subplots()
1993+
ax.scatter(x, x, c=x, norm=mcolors.Normalize(-10, 10), vmin=0, vmax=5)
1994+
19761995
@check_figures_equal(extensions=["png"])
19771996
def test_scatter_single_point(self, fig_test, fig_ref):
19781997
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)
0