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

Skip to content

Commit d55575e

Browse files
committed
MNT: Check the input sizes of regular X,Y in pcolorfast
Closes #28059.
1 parent fe6389f commit d55575e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6634,6 +6634,15 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
66346634
if x.size == 2 and y.size == 2:
66356635
style = "image"
66366636
else:
6637+
if x.size != nc:
6638+
raise ValueError(
6639+
f"Length of X ({x.size}) does not match the number of "
6640+
f"columns in C ({nc})")
6641+
if y.size != nr:
6642+
raise ValueError(
6643+
f"Length of Y ({y.size}) does not match the number of "
6644+
f"rows in C ({nr})"
6645+
)
66376646
dx = np.diff(x)
66386647
dy = np.diff(y)
66396648
if (np.ptp(dx) < 0.01 * abs(dx.mean()) and

lib/matplotlib/tests/test_axes.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6626,6 +6626,27 @@ def test_pcolorfast_bad_dims():
66266626
ax.pcolorfast(np.empty(6), np.empty((4, 7)), np.empty((8, 8)))
66276627

66286628

6629+
def test_pcolorfast_regular_xy_incompatible_size():
6630+
"""
6631+
Test that the sizes of X, Y, C are compatible for regularly spaced X, Y.
6632+
6633+
Note that after the regualar-spacing check, pcolorfast may go into the
6634+
fast "image" mode, where the individual X, Y positions are not used anymore.
6635+
Therefore, the algorithm had worked with any regularly number of regularly
6636+
spaced values, but discarded their values.
6637+
"""
6638+
fig, ax = plt.subplots()
6639+
with pytest.raises(
6640+
ValueError, match="Length of X (5) does not match the number of "
6641+
"columns in C (20)"):
6642+
ax.pcolorfast(np.arange(5), np.arange(10), np.random.rand(10, 20))
6643+
6644+
with pytest.raises(
6645+
ValueError, match="Length of Y (5) does not match the number of "
6646+
"rows in C (10)"):
6647+
ax.pcolorfast(np.arange(20), np.arange(5), np.random.rand(10, 20))
6648+
6649+
66296650
def test_shared_scale():
66306651
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
66316652

0 commit comments

Comments
 (0)