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

Skip to content

Commit da497df

Browse files
committed
updates from code review
thank you @story645 @ksunden
1 parent 0959f01 commit da497df

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
@@ -6273,8 +6273,9 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
62736273
See :doc:`/gallery/images_contours_and_fields/image_antialiasing` for
62746274
a discussion of image antialiasing.
62756275
6276-
Only 'data' is available when using `~matplotlib.colors.BivarColormap`
6277-
or `~matplotlib.colors.MultivarColormap`
6276+
When using a `~matplotlib.colors.BivarColormap` or
6277+
`~matplotlib.colors.MultivarColormap`, 'data' is the only valid
6278+
interpolation_stage.
62786279
62796280
alpha : float or array-like, optional
62806281
The alpha blending value, between 0 (transparent) and 1 (opaque).
@@ -6537,10 +6538,10 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
65376538
65386539
Parameters
65396540
----------
6540-
C : 2D or 3D array-like
6541+
C : 2D (I, J) or 3D (v, I, J) array-like
65416542
The color-mapped values. Color-mapping is controlled by *cmap*,
65426543
*norm*, *vmin*, and *vmax*. 3D arrays are supported only if the
6543-
cmap supports v channels, where v is the size along the first axis.
6544+
cmap supports v channels.
65446545
65456546
X, Y : array-like, optional
65466547
The coordinates of the corners of quadrilaterals of a pcolormesh::
@@ -6665,12 +6666,14 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
66656666
shading = mpl.rcParams['pcolor.shading']
66666667
shading = shading.lower()
66676668

6669+
mcolorizer.ColorizingArtist._check_exclusionary_keywords(colorizer,
6670+
vmin=vmin, vmax=vmax,
6671+
norm=norm, cmap=cmap)
66686672
if colorizer is None:
6669-
cmap = mcolorizer._ensure_cmap(cmap, accept_multivariate=True)
6670-
C = mcolorizer._ensure_multivariate_data(args[-1], cmap.n_variates)
6671-
else:
6672-
C = mcolorizer._ensure_multivariate_data(args[-1],
6673-
colorizer.cmap.n_variates)
6673+
colorizer = mcolorizer.Colorizer(cmap=cmap, norm=norm)
6674+
6675+
C = mcolorizer._ensure_multivariate_data(args[-1],
6676+
colorizer.cmap.n_variates)
66746677

66756678
X, Y, C, shading = self._pcolorargs('pcolor', *args[:-1], C,
66766679
shading=shading, kwargs=kwargs)
@@ -6709,9 +6712,7 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
67096712
coords = stack([X, Y], axis=-1)
67106713

67116714
collection = mcoll.PolyQuadMesh(
6712-
coords, array=C, cmap=cmap, norm=norm, colorizer=colorizer,
6713-
alpha=alpha, **kwargs)
6714-
collection._check_exclusionary_keywords(colorizer, vmin=vmin, vmax=vmax)
6715+
coords, array=C, colorizer=colorizer, alpha=alpha, **kwargs)
67156716
collection._scale_norm(norm, vmin, vmax)
67166717

67176718
coords = coords.reshape(-1, 2) # flatten the grid structure; keep x, y
@@ -6914,13 +6915,14 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
69146915
shading = mpl._val_or_rc(shading, 'pcolor.shading').lower()
69156916
kwargs.setdefault('edgecolors', 'none')
69166917

6918+
mcolorizer.ColorizingArtist._check_exclusionary_keywords(colorizer,
6919+
vmin=vmin, vmax=vmax,
6920+
norm=norm, cmap=cmap)
69176921
if colorizer is None:
6918-
cmap = mcolorizer._ensure_cmap(cmap, accept_multivariate=True)
6919-
C = mcolorizer._ensure_multivariate_data(args[-1], cmap.n_variates)
6920-
else:
6921-
C = mcolorizer._ensure_multivariate_data(args[-1],
6922-
colorizer.cmap.n_variates)
6922+
colorizer = mcolorizer.Colorizer(cmap=cmap, norm=norm)
69236923

6924+
C = mcolorizer._ensure_multivariate_data(args[-1],
6925+
colorizer.cmap.n_variates)
69246926

69256927
X, Y, C, shading = self._pcolorargs('pcolormesh', *args[:-1], C,
69266928
shading=shading, kwargs=kwargs)
@@ -6930,8 +6932,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
69306932

69316933
collection = mcoll.QuadMesh(
69326934
coords, antialiased=antialiased, shading=shading,
6933-
array=C, cmap=cmap, norm=norm, colorizer=colorizer, alpha=alpha, **kwargs)
6934-
collection._check_exclusionary_keywords(colorizer, vmin=vmin, vmax=vmax)
6935+
array=C, colorizer=colorizer, alpha=alpha, **kwargs)
69356936
collection._scale_norm(norm, vmin, vmax)
69366937

69376938
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
@@ -283,8 +283,8 @@ def __init__(self, ax,
283283
self.set_interpolation(interpolation)
284284
if isinstance(self.norm, mcolors.MultiNorm):
285285
if interpolation_stage not in [None, 'data', 'auto']:
286-
raise ValueError("'data' is the only valid interpolation_stage "
287-
"when using multiple color channels, not "
286+
raise ValueError("when using multiple color channels 'data' "
287+
"is the only valid interpolation_stage, not "
288288
f"{interpolation_stage}")
289289
self.set_interpolation_stage('data')
290290
else:
@@ -485,8 +485,23 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
485485
norms = self.norm.norms
486486
dtypes = [A.dtype.fields[f][0] for f in A.dtype.fields]
487487

488-
A_resampled = [_resample(self, a.astype(_get_scaled_dtype(a)),
489-
out_shape, t)
488+
def get_scaled_dt(A):
489+
# gets the scaled dtype
490+
if A.dtype.kind == 'f': # Float dtype: scale to same dtype.
491+
scaled_dtype = np.dtype('f8' if A.dtype.itemsize > 4 else 'f4')
492+
if scaled_dtype.itemsize < A.dtype.itemsize:
493+
_api.warn_external(f"Casting input data from {A.dtype}"
494+
f" to {scaled_dtype} for imshow.")
495+
else: # Int dtype, likely.
496+
# TODO slice input array first
497+
# Scale to appropriately sized float: use float32 if the
498+
# dynamic range is small, to limit the memory footprint.
499+
da = A.max().astype("f8") - A.min().astype("f8")
500+
scaled_dtype = "f8" if da > 1e8 else "f4"
501+
502+
return scaled_dtype
503+
504+
A_resampled = [_resample(self, a.astype(get_scaled_dt(a)), out_shape, t)
490505
for a in arrs]
491506

492507
# if using NoNorm, cast back to the original datatype
@@ -498,8 +513,8 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
498513
# pixels) and out_alpha (to what extent screen pixels are
499514
# covered by data pixels: 0 outside the data extent, 1 inside
500515
# (even for bad data), and intermediate values at the edges).
501-
mask = (np.where(self._getmaskarray(A), np.float32(np.nan),
502-
np.float32(1))
516+
mask = (np.where(self._getmaskarray(A),
517+
np.float32(np.nan), np.float32(1))
503518
if A.mask.shape == A.shape # nontrivial mask
504519
else np.ones_like(A, np.float32))
505520
# we always have to interpolate the mask to account for
@@ -1868,20 +1883,3 @@ def thumbnail(infile, thumbfile, scale=0.1, interpolation='bilinear',
18681883
ax.imshow(im, aspect='auto', resample=True, interpolation=interpolation)
18691884
fig.savefig(thumbfile, dpi=dpi)
18701885
return fig
1871-
1872-
1873-
def _get_scaled_dtype(A):
1874-
1875-
if A.dtype.kind == 'f': # Float dtype: scale to same dtype.
1876-
scaled_dtype = np.dtype('f8' if A.dtype.itemsize > 4 else 'f4')
1877-
if scaled_dtype.itemsize < A.dtype.itemsize:
1878-
_api.warn_external(f"Casting input data from {A.dtype}"
1879-
f" to {scaled_dtype} for imshow.")
1880-
else: # Int dtype, likely.
1881-
# TODO slice input array first
1882-
# Scale to appropriately sized float: use float32 if the
1883-
# dynamic range is small, to limit the memory footprint.
1884-
da = A.max().astype("f8") - A.min().astype("f8")
1885-
scaled_dtype = "f8" if da > 1e8 else "f4"
1886-
1887-
return scaled_dtype
0 Bytes
Loading

lib/matplotlib/tests/test_axes.py

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

1040810408
fig, axes = plt.subplots(1, 6, figsize=(10, 2))
1040910409

10410-
axes[0].imshow((x_0, x_1, x_2), cmap='3VarAddA', interpolation='nearest')
10410+
axes[0].imshow((x_0, x_1, x_2), cmap='3VarAddA')
1041110411
axes[1].matshow((x_0, x_1, x_2), cmap='3VarAddA')
1041210412
axes[2].pcolor((x_0, x_1, x_2), cmap='3VarAddA')
1041310413
axes[3].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA')
@@ -10487,9 +10487,8 @@ def test_multivariate_imshow_alpha():
1048710487
def test_multivariate_pcolormesh_norm():
1048810488
"""
1048910489
Test vmin, vmax and norm
10490-
Norm is checked via a LogNorm, as this converts
10491-
A LogNorm converts the input to a masked array, masking for X <= 0
10492-
By using a LogNorm, this functionality is also tested.
10490+
Norm is checked via a LogNorm, as this converts the input to a masked array,
10491+
masking for X <= 0. By using a LogNorm, this functionality is also tested.
1049310492
This test covers all plotting modes that use the same pipeline
1049410493
(inherit from Collection).
1049510494
"""
@@ -10502,8 +10501,8 @@ def test_multivariate_pcolormesh_norm():
1050210501
axes[0, 0].pcolormesh(x_1)
1050310502
axes[0, 1].pcolormesh((x_0, x_1), cmap='BiPeak')
1050410503
axes[0, 2].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA')
10505-
axes[0, 3].pcolormesh((x_0, x_1), cmap='BiPeak')
10506-
axes[0, 4].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA')
10504+
axes[0, 3].pcolormesh((x_0, x_1), cmap='BiPeak') # repeated for visual consistency
10505+
axes[0, 4].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA') # repeated
1050710506

1050810507
vmin = 1
1050910508
vmax = 3
@@ -10516,13 +10515,13 @@ def test_multivariate_pcolormesh_norm():
1051610515
axes[1, 4].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA',
1051710516
vmin=(None, vmin, None), vmax=(None, vmax, None))
1051810517

10519-
n = mcolors.LogNorm(vmin=1, vmax=5)
10520-
axes[2, 0].pcolormesh(x_1, norm=n)
10521-
axes[2, 1].pcolormesh((x_0, x_1), cmap='BiPeak', norm=(n, n))
10522-
axes[2, 2].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA', norm=(n, n, n))
10523-
axes[2, 3].pcolormesh((x_0, x_1), cmap='BiPeak', norm=('linear', n))
10518+
norm = mcolors.LogNorm(vmin=1, vmax=5)
10519+
axes[2, 0].pcolormesh(x_1, norm=norm)
10520+
axes[2, 1].pcolormesh((x_0, x_1), cmap='BiPeak', norm=(norm, norm))
10521+
axes[2, 2].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA', norm=(norm, norm, norm))
10522+
axes[2, 3].pcolormesh((x_0, x_1), cmap='BiPeak', norm=('linear', norm))
1052410523
axes[2, 4].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA',
10525-
norm=('linear', n, 'linear'))
10524+
norm=('linear', norm, 'linear'))
1052610525

1052710526
remove_ticks_and_titles(fig)
1052810527

@@ -10589,7 +10588,7 @@ def test_bivariate_cmap_shapes():
1058910588
interpolation='nearest')
1059010589

1059110590
# shape = ignore
10592-
cmap = mpl.bivar_colormaps['BiCone']
10591+
cmap = mpl.bivar_colormaps['BiPeak']
1059310592
cmap = cmap.with_extremes(shape='ignore')
1059410593
axes[2].imshow((x_0, x_1), cmap=cmap, vmin=(1, 1), vmax=(8, 8),
1059510594
interpolation='nearest')

0 commit comments

Comments
 (0)