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

Skip to content

Commit 2318dec

Browse files
committed
updates from code review
thank you @story645 @ksunden
1 parent c334208 commit 2318dec

5 files changed

Lines changed: 54 additions & 56 deletions

File tree

lib/matplotlib/axes/_axes.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6158,8 +6158,9 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
61586158
See :doc:`/gallery/images_contours_and_fields/image_antialiasing` for
61596159
a discussion of image antialiasing.
61606160
6161-
Only 'data' is available when using `~matplotlib.colors.BivarColormap`
6162-
or `~matplotlib.colors.MultivarColormap`
6161+
When using a `~matplotlib.colors.BivarColormap` or
6162+
`~matplotlib.colors.MultivarColormap`, 'data' is the only valid
6163+
interpolation_stage.
61636164
61646165
alpha : float or array-like, optional
61656166
The alpha blending value, between 0 (transparent) and 1 (opaque).
@@ -6422,10 +6423,10 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
64226423
64236424
Parameters
64246425
----------
6425-
C : 2D or 3D array-like
6426+
C : 2D (I, J) or 3D (v, I, J) array-like
64266427
The color-mapped values. Color-mapping is controlled by *cmap*,
64276428
*norm*, *vmin*, and *vmax*. 3D arrays are supported only if the
6428-
cmap supports v channels, where v is the size along the first axis.
6429+
cmap supports v channels.
64296430
64306431
X, Y : array-like, optional
64316432
The coordinates of the corners of quadrilaterals of a pcolormesh::
@@ -6552,12 +6553,14 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
65526553
shading = mpl.rcParams['pcolor.shading']
65536554
shading = shading.lower()
65546555

6556+
mcolorizer.ColorizingArtist._check_exclusionary_keywords(colorizer,
6557+
vmin=vmin, vmax=vmax,
6558+
norm=norm, cmap=cmap)
65556559
if colorizer is None:
6556-
cmap = mcolorizer._ensure_cmap(cmap, accept_multivariate=True)
6557-
C = mcolorizer._ensure_multivariate_data(args[-1], cmap.n_variates)
6558-
else:
6559-
C = mcolorizer._ensure_multivariate_data(args[-1],
6560-
colorizer.cmap.n_variates)
6560+
colorizer = mcolorizer.Colorizer(cmap=cmap, norm=norm)
6561+
6562+
C = mcolorizer._ensure_multivariate_data(args[-1],
6563+
colorizer.cmap.n_variates)
65616564

65626565
X, Y, C, shading = self._pcolorargs('pcolor', *args[:-1], C,
65636566
shading=shading, kwargs=kwargs)
@@ -6596,9 +6599,7 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
65966599
coords = stack([X, Y], axis=-1)
65976600

65986601
collection = mcoll.PolyQuadMesh(
6599-
coords, array=C, cmap=cmap, norm=norm, colorizer=colorizer,
6600-
alpha=alpha, **kwargs)
6601-
collection._check_exclusionary_keywords(colorizer, vmin=vmin, vmax=vmax)
6602+
coords, array=C, colorizer=colorizer, alpha=alpha, **kwargs)
66026603
collection._scale_norm(norm, vmin, vmax)
66036604

66046605
coords = coords.reshape(-1, 2) # flatten the grid structure; keep x, y
@@ -6800,13 +6801,14 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
68006801
shading = mpl._val_or_rc(shading, 'pcolor.shading').lower()
68016802
kwargs.setdefault('edgecolors', 'none')
68026803

6804+
mcolorizer.ColorizingArtist._check_exclusionary_keywords(colorizer,
6805+
vmin=vmin, vmax=vmax,
6806+
norm=norm, cmap=cmap)
68036807
if colorizer is None:
6804-
cmap = mcolorizer._ensure_cmap(cmap, accept_multivariate=True)
6805-
C = mcolorizer._ensure_multivariate_data(args[-1], cmap.n_variates)
6806-
else:
6807-
C = mcolorizer._ensure_multivariate_data(args[-1],
6808-
colorizer.cmap.n_variates)
6808+
colorizer = mcolorizer.Colorizer(cmap=cmap, norm=norm)
68096809

6810+
C = mcolorizer._ensure_multivariate_data(args[-1],
6811+
colorizer.cmap.n_variates)
68106812

68116813
X, Y, C, shading = self._pcolorargs('pcolormesh', *args[:-1], C,
68126814
shading=shading, kwargs=kwargs)
@@ -6816,8 +6818,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
68166818

68176819
collection = mcoll.QuadMesh(
68186820
coords, antialiased=antialiased, shading=shading,
6819-
array=C, cmap=cmap, norm=norm, colorizer=colorizer, alpha=alpha, **kwargs)
6820-
collection._check_exclusionary_keywords(colorizer, vmin=vmin, vmax=vmax)
6821+
array=C, colorizer=colorizer, alpha=alpha, **kwargs)
68216822
collection._scale_norm(norm, vmin, vmax)
68226823

68236824
coords = coords.reshape(-1, 2) # flatten the grid structure; keep x, y

lib/matplotlib/colorizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def _scale_norm(self, norm, vmin, vmax, A):
7474
"""
7575
if vmin is not None or vmax is not None:
7676
self.set_clim(vmin, vmax)
77-
if isinstance(norm, colors.Normalize):
77+
if isinstance(norm, colors.Norm):
7878
raise ValueError(
7979
"Passing a Normalize instance simultaneously with "
8080
"vmin/vmax is not supported. Please pass vmin/vmax "

lib/matplotlib/image.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ def __init__(self, ax,
270270
self.set_interpolation(interpolation)
271271
if isinstance(self.norm, mcolors.MultiNorm):
272272
if interpolation_stage not in [None, 'data', 'auto']:
273-
raise ValueError("'data' is the only valid interpolation_stage "
274-
"when using multiple color channels, not "
273+
raise ValueError("when using multiple color channels 'data' "
274+
"is the only valid interpolation_stage, not "
275275
f"{interpolation_stage}")
276276
self.set_interpolation_stage('data')
277277
else:
@@ -474,8 +474,23 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
474474
norms = self.norm.norms
475475
dtypes = [A.dtype.fields[f][0] for f in A.dtype.fields]
476476

477-
A_resampled = [_resample(self, a.astype(_get_scaled_dtype(a)),
478-
out_shape, t)
477+
def get_scaled_dt(A):
478+
# gets the scaled dtype
479+
if A.dtype.kind == 'f': # Float dtype: scale to same dtype.
480+
scaled_dtype = np.dtype('f8' if A.dtype.itemsize > 4 else 'f4')
481+
if scaled_dtype.itemsize < A.dtype.itemsize:
482+
_api.warn_external(f"Casting input data from {A.dtype}"
483+
f" to {scaled_dtype} for imshow.")
484+
else: # Int dtype, likely.
485+
# TODO slice input array first
486+
# Scale to appropriately sized float: use float32 if the
487+
# dynamic range is small, to limit the memory footprint.
488+
da = A.max().astype("f8") - A.min().astype("f8")
489+
scaled_dtype = "f8" if da > 1e8 else "f4"
490+
491+
return scaled_dtype
492+
493+
A_resampled = [_resample(self, a.astype(get_scaled_dt(a)), out_shape, t)
479494
for a in arrs]
480495

481496
# if using NoNorm, cast back to the original datatype
@@ -487,8 +502,8 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
487502
# pixels) and out_alpha (to what extent screen pixels are
488503
# covered by data pixels: 0 outside the data extent, 1 inside
489504
# (even for bad data), and intermediate values at the edges).
490-
mask = (np.where(self._getmaskarray(A), np.float32(np.nan),
491-
np.float32(1))
505+
mask = (np.where(self._getmaskarray(A),
506+
np.float32(np.nan), np.float32(1))
492507
if A.mask.shape == A.shape # nontrivial mask
493508
else np.ones_like(A, np.float32))
494509
# we always have to interpolate the mask to account for
@@ -1845,20 +1860,3 @@ def thumbnail(infile, thumbfile, scale=0.1, interpolation='bilinear',
18451860
ax.imshow(im, aspect='auto', resample=True, interpolation=interpolation)
18461861
fig.savefig(thumbfile, dpi=dpi)
18471862
return fig
1848-
1849-
1850-
def _get_scaled_dtype(A):
1851-
1852-
if A.dtype.kind == 'f': # Float dtype: scale to same dtype.
1853-
scaled_dtype = np.dtype('f8' if A.dtype.itemsize > 4 else 'f4')
1854-
if scaled_dtype.itemsize < A.dtype.itemsize:
1855-
_api.warn_external(f"Casting input data from {A.dtype}"
1856-
f" to {scaled_dtype} for imshow.")
1857-
else: # Int dtype, likely.
1858-
# TODO slice input array first
1859-
# Scale to appropriately sized float: use float32 if the
1860-
# dynamic range is small, to limit the memory footprint.
1861-
da = A.max().astype("f8") - A.min().astype("f8")
1862-
scaled_dtype = "f8" if da > 1e8 else "f4"
1863-
1864-
return scaled_dtype
11 KB
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10007,7 +10007,7 @@ def test_multivariate_visualizations():
1000710007

1000810008
fig, axes = plt.subplots(1, 6, figsize=(10, 2))
1000910009

10010-
axes[0].imshow((x_0, x_1, x_2), cmap='3VarAddA', interpolation='nearest')
10010+
axes[0].imshow((x_0, x_1, x_2), cmap='3VarAddA')
1001110011
axes[1].matshow((x_0, x_1, x_2), cmap='3VarAddA')
1001210012
axes[2].pcolor((x_0, x_1, x_2), cmap='3VarAddA')
1001310013
axes[3].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA')
@@ -10087,9 +10087,8 @@ def test_multivariate_imshow_alpha():
1008710087
def test_multivariate_pcolormesh_norm():
1008810088
"""
1008910089
Test vmin, vmax and norm
10090-
Norm is checked via a LogNorm, as this converts
10091-
A LogNorm converts the input to a masked array, masking for X <= 0
10092-
By using a LogNorm, this functionality is also tested.
10090+
Norm is checked via a LogNorm, as this converts the input to a masked array,
10091+
masking for X <= 0. By using a LogNorm, this functionality is also tested.
1009310092
This test covers all plotting modes that use the same pipeline
1009410093
(inherit from Collection).
1009510094
"""
@@ -10102,8 +10101,8 @@ def test_multivariate_pcolormesh_norm():
1010210101
axes[0, 0].pcolormesh(x_1)
1010310102
axes[0, 1].pcolormesh((x_0, x_1), cmap='BiPeak')
1010410103
axes[0, 2].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA')
10105-
axes[0, 3].pcolormesh((x_0, x_1), cmap='BiPeak')
10106-
axes[0, 4].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA')
10104+
axes[0, 3].pcolormesh((x_0, x_1), cmap='BiPeak') # repeated for visual consistency
10105+
axes[0, 4].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA') # repeated
1010710106

1010810107
vmin = 1
1010910108
vmax = 3
@@ -10116,13 +10115,13 @@ def test_multivariate_pcolormesh_norm():
1011610115
axes[1, 4].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA',
1011710116
vmin=(None, vmin, None), vmax=(None, vmax, None))
1011810117

10119-
n = mcolors.LogNorm(vmin=1, vmax=5)
10120-
axes[2, 0].pcolormesh(x_1, norm=n)
10121-
axes[2, 1].pcolormesh((x_0, x_1), cmap='BiPeak', norm=(n, n))
10122-
axes[2, 2].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA', norm=(n, n, n))
10123-
axes[2, 3].pcolormesh((x_0, x_1), cmap='BiPeak', norm=('linear', n))
10118+
norm = mcolors.LogNorm(vmin=1, vmax=5)
10119+
axes[2, 0].pcolormesh(x_1, norm=norm)
10120+
axes[2, 1].pcolormesh((x_0, x_1), cmap='BiPeak', norm=(norm, norm))
10121+
axes[2, 2].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA', norm=(norm, norm, norm))
10122+
axes[2, 3].pcolormesh((x_0, x_1), cmap='BiPeak', norm=('linear', norm))
1012410123
axes[2, 4].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA',
10125-
norm=('linear', n, 'linear'))
10124+
norm=('linear', norm, 'linear'))
1012610125

1012710126
remove_ticks_and_titles(fig)
1012810127

@@ -10189,7 +10188,7 @@ def test_bivariate_cmap_shapes():
1018910188
interpolation='nearest')
1019010189

1019110190
# shape = ignore
10192-
cmap = mpl.bivar_colormaps['BiCone']
10191+
cmap = mpl.bivar_colormaps['BiPeak']
1019310192
cmap = cmap.with_extremes(shape='ignore')
1019410193
axes[2].imshow((x_0, x_1), cmap=cmap, vmin=(1, 1), vmax=(8, 8),
1019510194
interpolation='nearest')

0 commit comments

Comments
 (0)