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

Skip to content

Commit b30a297

Browse files
committed
update based on feedback from @timhoffm
1 parent 16ed34e commit b30a297

7 files changed

Lines changed: 45 additions & 23 deletions

File tree

lib/matplotlib/axes/_axes.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6175,7 +6175,7 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
61756175
- (M, N): an image with scalar data. The values are mapped to
61766176
colors using normalization and a colormap. See parameters *norm*,
61776177
*cmap*, *vmin*, *vmax*.
6178-
- (v, M, N): if coupled with a cmap that supports v scalars
6178+
- (K, M, N): if coupled with a cmap that supports K scalars
61796179
- (M, N, 3): an image with RGB values (0-1 float or 0-255 int).
61806180
- (M, N, 4): an image with RGBA values (0-1 float or 0-255 int),
61816181
i.e. including transparency.
@@ -6538,10 +6538,10 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
65386538
65396539
Parameters
65406540
----------
6541-
C : 2D (M, N) or 3D (v, M, N) array-like
6541+
C : 2D (M, N) or 3D (K, M, N) array-like
65426542
The color-mapped values. Color-mapping is controlled by *cmap*,
65436543
*norm*, *vmin*, and *vmax*. 3D arrays are supported only if the
6544-
cmap supports v channels.
6544+
cmap supports K channels.
65456545
65466546
X, Y : array-like, optional
65476547
The coordinates of the corners of quadrilaterals of a pcolormesh::
@@ -6750,7 +6750,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
67506750
- (M, N) or M*N: a mesh with scalar data. The values are mapped to
67516751
colors using normalization and a colormap. See parameters *norm*,
67526752
*cmap*, *vmin*, *vmax*.
6753-
- (v, M, N): if coupled with a cmap that supports v scalars
6753+
- (K, M, N): if coupled with a cmap that supports K scalars
67546754
- (M, N, 3): an image with RGB values (0-1 float or 0-255 int).
67556755
- (M, N, 4): an image with RGBA values (0-1 float or 0-255 int),
67566756
i.e. including transparency.
@@ -8874,6 +8874,9 @@ def matshow(self, Z, **kwargs):
88748874
88758875
"""
88768876
Z = np.asanyarray(Z)
8877+
if Z.ndim != 2:
8878+
if Z.ndim != 3 or Z.shape[2] not in (1, 3, 4):
8879+
raise TypeError(f"Invalid shape {Z.shape} for image data")
88778880
kw = {'origin': 'upper',
88788881
'interpolation': 'nearest',
88798882
'aspect': 'equal', # (already the imshow default)

lib/matplotlib/colorizer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ def set_clim(self, vmin=None, vmax=None):
277277
def get_clim(self):
278278
"""
279279
Return the values (min, max) that are mapped to the colormap limits.
280+
281+
This function always returns min and max as tuples to ensure type consistency
282+
when working with both scalar and multivariate color mapping.
283+
See also `.ColorizingArtist.get_clim()` which returns scalars but is unavailable
284+
for multivariate color mapping.
280285
"""
281286
if self.norm.n_components == 1:
282287
return (self.norm.vmin, ), (self.norm.vmax, )

lib/matplotlib/colors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,14 +1606,14 @@ def with_extremes(self, *, bad=None, under=None, over=None):
16061606
16071607
Parameters
16081608
----------
1609-
bad: :mpltype:`color`, default: None
1609+
bad : :mpltype:`color`, default: None
16101610
If Matplotlib color, the bad value is set accordingly in the copy
16111611
1612-
under tuple of :mpltype:`color`, default: None
1612+
under : tuple of :mpltype:`color`, default: None
16131613
If tuple, the 'under' value of each component is set with the values
16141614
from the tuple.
16151615
1616-
over tuple of :mpltype:`color`, default: None
1616+
over : tuple of :mpltype:`color`, default: None
16171617
If tuple, the 'over' value of each component is set with the values
16181618
from the tuple.
16191619

lib/matplotlib/image.py

Lines changed: 6 additions & 4 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("when using multiple color channels 'data' "
287-
"is the only valid interpolation_stage, not "
286+
raise ValueError("when using multivariate color mapping 'data' "
287+
"is the only valid interpolation_stage, but got "
288288
f"{interpolation_stage}")
289289
self.set_interpolation_stage('data')
290290
else:
@@ -485,7 +485,7 @@ 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-
def get_scaled_dt(A):
488+
def get_scaled_dtype(A):
489489
# gets the scaled dtype
490490
if A.dtype.kind == 'f': # Float dtype: scale to same dtype.
491491
scaled_dtype = np.dtype('f8' if A.dtype.itemsize > 4 else 'f4')
@@ -501,7 +501,9 @@ def get_scaled_dt(A):
501501

502502
return scaled_dtype
503503

504-
A_resampled = [_resample(self, a.astype(get_scaled_dt(a)), out_shape, t)
504+
A_resampled = [_resample(self,
505+
a.astype(get_scaled_dtype(a)),
506+
out_shape, t)
505507
for a in arrs]
506508

507509
# if using NoNorm, cast back to the original datatype
-655 Bytes
Loading
-666 Bytes
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10368,22 +10368,35 @@ def test_errorbar_uses_rcparams():
1036810368
assert_allclose(barcol.get_linewidths(), 1.75)
1036910369

1037010370

10371+
def test_matshow_not_multivariate():
10372+
"""
10373+
matshow() currently does not support multivariate/bivariate colormaps.
10374+
This test is to ensure coverage for the if-statement that checks for this.
10375+
10376+
This test should be removed if matshow() is updated to support
10377+
multivariate/bivariate colormaps.
10378+
"""
10379+
fig, axes = plt.subplots()
10380+
arr = np.arange(24).reshape((-1, 4, 2))
10381+
with pytest.raises(TypeError, match="Invalid shape"):
10382+
axes.matshow(arr)
10383+
10384+
1037110385
@image_comparison(["bivariate_visualizations.png"], style='mpl20')
1037210386
def test_bivariate_visualizations():
1037310387
x_0 = np.arange(25, dtype='float32').reshape(5, 5) % 5
1037410388
x_1 = np.arange(25, dtype='float32').reshape(5, 5).T % 5
1037510389

10376-
fig, axes = plt.subplots(1, 6, figsize=(10, 2))
10390+
fig, axes = plt.subplots(1, 5, figsize=(8, 2))
1037710391

1037810392
axes[0].imshow((x_0, x_1), cmap='BiPeak', interpolation='nearest')
10379-
axes[1].matshow((x_0, x_1), cmap='BiPeak')
10380-
axes[2].pcolor((x_0, x_1), cmap='BiPeak')
10381-
axes[3].pcolormesh((x_0, x_1), cmap='BiPeak')
10393+
axes[1].pcolor((x_0, x_1), cmap='BiPeak')
10394+
axes[2].pcolormesh((x_0, x_1), cmap='BiPeak')
1038210395

1038310396
x = np.arange(5)
1038410397
y = np.arange(5)
1038510398
X, Y = np.meshgrid(x, y)
10386-
axes[4].pcolormesh(X, Y, (x_0, x_1), cmap='BiPeak')
10399+
axes[3].pcolormesh(X, Y, (x_0, x_1), cmap='BiPeak')
1038710400

1038810401
patches = [
1038910402
mpl.patches.Wedge((.3, .7), .1, 0, 360), # Full circle
@@ -10395,7 +10408,7 @@ def test_bivariate_visualizations():
1039510408
colors_1 = np.arange(len(patches)) % 2
1039610409
p = mpl.collections.PatchCollection(patches, cmap='BiPeak', alpha=0.5)
1039710410
p.set_array((colors_0, colors_1))
10398-
axes[5].add_collection(p)
10411+
axes[4].add_collection(p)
1039910412
remove_ticks_and_titles(fig)
1040010413

1040110414

@@ -10405,17 +10418,16 @@ def test_multivariate_visualizations():
1040510418
x_1 = np.arange(25, dtype='float32').reshape(5, 5).T % 5
1040610419
x_2 = np.arange(25, dtype='float32').reshape(5, 5) % 6
1040710420

10408-
fig, axes = plt.subplots(1, 6, figsize=(10, 2))
10421+
fig, axes = plt.subplots(1, 5, figsize=(8, 2))
1040910422

1041010423
axes[0].imshow((x_0, x_1, x_2), cmap='3VarAddA')
10411-
axes[1].matshow((x_0, x_1, x_2), cmap='3VarAddA')
10412-
axes[2].pcolor((x_0, x_1, x_2), cmap='3VarAddA')
10413-
axes[3].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA')
10424+
axes[1].pcolor((x_0, x_1, x_2), cmap='3VarAddA')
10425+
axes[2].pcolormesh((x_0, x_1, x_2), cmap='3VarAddA')
1041410426

1041510427
x = np.arange(5)
1041610428
y = np.arange(5)
1041710429
X, Y = np.meshgrid(x, y)
10418-
axes[4].pcolormesh(X, Y, (x_0, x_1, x_2), cmap='3VarAddA')
10430+
axes[3].pcolormesh(X, Y, (x_0, x_1, x_2), cmap='3VarAddA')
1041910431

1042010432
patches = [
1042110433
mpl.patches.Wedge((.3, .7), .1, 0, 360), # Full circle
@@ -10428,7 +10440,7 @@ def test_multivariate_visualizations():
1042810440
colors_2 = np.arange(len(patches)) % 3
1042910441
p = mpl.collections.PatchCollection(patches, cmap='3VarAddA', alpha=0.5)
1043010442
p.set_array((colors_0, colors_1, colors_2))
10431-
axes[5].add_collection(p)
10443+
axes[4].add_collection(p)
1043210444
remove_ticks_and_titles(fig)
1043310445

1043410446

0 commit comments

Comments
 (0)