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

Skip to content

Commit b935040

Browse files
committed
updates from code review
thank you @story645 @ksunden
1 parent 50f55a8 commit b935040

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
@@ -6248,8 +6248,9 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
62486248
See :doc:`/gallery/images_contours_and_fields/image_antialiasing` for
62496249
a discussion of image antialiasing.
62506250
6251-
Only 'data' is available when using `~matplotlib.colors.BivarColormap`
6252-
or `~matplotlib.colors.MultivarColormap`
6251+
When using a `~matplotlib.colors.BivarColormap` or
6252+
`~matplotlib.colors.MultivarColormap`, 'data' is the only valid
6253+
interpolation_stage.
62536254
62546255
alpha : float or array-like, optional
62556256
The alpha blending value, between 0 (transparent) and 1 (opaque).
@@ -6512,10 +6513,10 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
65126513
65136514
Parameters
65146515
----------
6515-
C : 2D or 3D array-like
6516+
C : 2D (I, J) or 3D (v, I, J) array-like
65166517
The color-mapped values. Color-mapping is controlled by *cmap*,
65176518
*norm*, *vmin*, and *vmax*. 3D arrays are supported only if the
6518-
cmap supports v channels, where v is the size along the first axis.
6519+
cmap supports v channels.
65196520
65206521
X, Y : array-like, optional
65216522
The coordinates of the corners of quadrilaterals of a pcolormesh::
@@ -6642,12 +6643,14 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
66426643
shading = mpl.rcParams['pcolor.shading']
66436644
shading = shading.lower()
66446645

6646+
mcolorizer.ColorizingArtist._check_exclusionary_keywords(colorizer,
6647+
vmin=vmin, vmax=vmax,
6648+
norm=norm, cmap=cmap)
66456649
if colorizer is None:
6646-
cmap = mcolorizer._ensure_cmap(cmap, accept_multivariate=True)
6647-
C = mcolorizer._ensure_multivariate_data(args[-1], cmap.n_variates)
6648-
else:
6649-
C = mcolorizer._ensure_multivariate_data(args[-1],
6650-
colorizer.cmap.n_variates)
6650+
colorizer = mcolorizer.Colorizer(cmap=cmap, norm=norm)
6651+
6652+
C = mcolorizer._ensure_multivariate_data(args[-1],
6653+
colorizer.cmap.n_variates)
66516654

66526655
X, Y, C, shading = self._pcolorargs('pcolor', *args[:-1], C,
66536656
shading=shading, kwargs=kwargs)
@@ -6686,9 +6689,7 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
66866689
coords = stack([X, Y], axis=-1)
66876690

66886691
collection = mcoll.PolyQuadMesh(
6689-
coords, array=C, cmap=cmap, norm=norm, colorizer=colorizer,
6690-
alpha=alpha, **kwargs)
6691-
collection._check_exclusionary_keywords(colorizer, vmin=vmin, vmax=vmax)
6692+
coords, array=C, colorizer=colorizer, alpha=alpha, **kwargs)
66926693
collection._scale_norm(norm, vmin, vmax)
66936694

66946695
coords = coords.reshape(-1, 2) # flatten the grid structure; keep x, y
@@ -6890,13 +6891,14 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
68906891
shading = mpl._val_or_rc(shading, 'pcolor.shading').lower()
68916892
kwargs.setdefault('edgecolors', 'none')
68926893

6894+
mcolorizer.ColorizingArtist._check_exclusionary_keywords(colorizer,
6895+
vmin=vmin, vmax=vmax,
6896+
norm=norm, cmap=cmap)
68936897
if colorizer is None:
6894-
cmap = mcolorizer._ensure_cmap(cmap, accept_multivariate=True)
6895-
C = mcolorizer._ensure_multivariate_data(args[-1], cmap.n_variates)
6896-
else:
6897-
C = mcolorizer._ensure_multivariate_data(args[-1],
6898-
colorizer.cmap.n_variates)
6898+
colorizer = mcolorizer.Colorizer(cmap=cmap, norm=norm)
68996899

6900+
C = mcolorizer._ensure_multivariate_data(args[-1],
6901+
colorizer.cmap.n_variates)
69006902

69016903
X, Y, C, shading = self._pcolorargs('pcolormesh', *args[:-1], C,
69026904
shading=shading, kwargs=kwargs)
@@ -6906,8 +6908,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
69066908

69076909
collection = mcoll.QuadMesh(
69086910
coords, antialiased=antialiased, shading=shading,
6909-
array=C, cmap=cmap, norm=norm, colorizer=colorizer, alpha=alpha, **kwargs)
6910-
collection._check_exclusionary_keywords(colorizer, vmin=vmin, vmax=vmax)
6911+
array=C, colorizer=colorizer, alpha=alpha, **kwargs)
69116912
collection._scale_norm(norm, vmin, vmax)
69126913

69136914
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
@@ -10059,7 +10059,7 @@ def test_multivariate_visualizations():
1005910059

1006010060
fig, axes = plt.subplots(1, 6, figsize=(10, 2))
1006110061

10062-
axes[0].imshow((x_0, x_1, x_2), cmap='3VarAddA', interpolation='nearest')
10062+
axes[0].imshow((x_0, x_1, x_2), cmap='3VarAddA')
1006310063
axes[1].matshow((x_0, x_1, x_2), cmap='3VarAddA')
1006410064
axes[2].pcolor((x_0, x_1, x_2), cmap='3VarAddA')
1006510065
axes[3].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA')
@@ -10139,9 +10139,8 @@ def test_multivariate_imshow_alpha():
1013910139
def test_multivariate_pcolormesh_norm():
1014010140
"""
1014110141
Test vmin, vmax and norm
10142-
Norm is checked via a LogNorm, as this converts
10143-
A LogNorm converts the input to a masked array, masking for X <= 0
10144-
By using a LogNorm, this functionality is also tested.
10142+
Norm is checked via a LogNorm, as this converts the input to a masked array,
10143+
masking for X <= 0. By using a LogNorm, this functionality is also tested.
1014510144
This test covers all plotting modes that use the same pipeline
1014610145
(inherit from Collection).
1014710146
"""
@@ -10154,8 +10153,8 @@ def test_multivariate_pcolormesh_norm():
1015410153
axes[0, 0].pcolormesh(x_1)
1015510154
axes[0, 1].pcolormesh((x_0, x_1), cmap='BiPeak')
1015610155
axes[0, 2].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA')
10157-
axes[0, 3].pcolormesh((x_0, x_1), cmap='BiPeak')
10158-
axes[0, 4].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA')
10156+
axes[0, 3].pcolormesh((x_0, x_1), cmap='BiPeak') # repeated for visual consistency
10157+
axes[0, 4].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA') # repeated
1015910158

1016010159
vmin = 1
1016110160
vmax = 3
@@ -10168,13 +10167,13 @@ def test_multivariate_pcolormesh_norm():
1016810167
axes[1, 4].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA',
1016910168
vmin=(None, vmin, None), vmax=(None, vmax, None))
1017010169

10171-
n = mcolors.LogNorm(vmin=1, vmax=5)
10172-
axes[2, 0].pcolormesh(x_1, norm=n)
10173-
axes[2, 1].pcolormesh((x_0, x_1), cmap='BiPeak', norm=(n, n))
10174-
axes[2, 2].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA', norm=(n, n, n))
10175-
axes[2, 3].pcolormesh((x_0, x_1), cmap='BiPeak', norm=('linear', n))
10170+
norm = mcolors.LogNorm(vmin=1, vmax=5)
10171+
axes[2, 0].pcolormesh(x_1, norm=norm)
10172+
axes[2, 1].pcolormesh((x_0, x_1), cmap='BiPeak', norm=(norm, norm))
10173+
axes[2, 2].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA', norm=(norm, norm, norm))
10174+
axes[2, 3].pcolormesh((x_0, x_1), cmap='BiPeak', norm=('linear', norm))
1017610175
axes[2, 4].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA',
10177-
norm=('linear', n, 'linear'))
10176+
norm=('linear', norm, 'linear'))
1017810177

1017910178
remove_ticks_and_titles(fig)
1018010179

@@ -10241,7 +10240,7 @@ def test_bivariate_cmap_shapes():
1024110240
interpolation='nearest')
1024210241

1024310242
# shape = ignore
10244-
cmap = mpl.bivar_colormaps['BiCone']
10243+
cmap = mpl.bivar_colormaps['BiPeak']
1024510244
cmap = cmap.with_extremes(shape='ignore')
1024610245
axes[2].imshow((x_0, x_1), cmap=cmap, vmin=(1, 1), vmax=(8, 8),
1024710246
interpolation='nearest')

0 commit comments

Comments
 (0)