8000 MNT: Check the input sizes of regular X,Y in pcolorfast (#28853) · matplotlib/matplotlib@f8666be · GitHub
[go: up one dir, main page]

Skip to content

Commit f8666be

Browse files
timhoffmQuLogic
andauthored
MNT: Check the input sizes of regular X,Y in pcolorfast (#28853)
* MNT: Check the input sizes of regular X,Y in pcolorfast Closes #28059. * Apply suggestions from code review Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> --------- Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
1 parent 7c7f94c commit f8666be

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
@@ -6627,6 +6627,15 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
66276627
if x.size == 2 and y.size == 2:
66286628
style = "image"
66296629
else:
6630+
if x.size != nc + 1:
6631+
raise ValueError(
6632+
f"Length of X ({x.size}) must be one larger than the "
6633+
f"number of columns in C ({nc})")
6634+
if y.size != nr + 1:
6635+
raise ValueError(
6636+
f"Length of Y ({y.size}) must be one larger than the "
6637+
f"number of rows in C ({nr})"
6638+
)
66306639
dx = np.diff(x)
66316640
dy = np.diff(y)
66326641
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
@@ -6647,6 +6647,27 @@ def test_pcolorfast_bad_dims():
66476647
ax.pcolorfast(np.empty(6), np.empty((4, 7)), np.empty((8, 8)))
66486648

66496649

6650+
def test_pcolorfast_regular_xy_incompatible_size():
6651+
"""
6652+
Test that the sizes of X, Y, C are compatible for regularly spaced X, Y.
6653+
6654+
Note that after the regualar-spacing check, pcolorfast may go into the
6655+
fast "image" mode, where the individual X, Y positions are not used anymore.
6656+
Therefore, the algorithm had worked with any regularly number of regularly
6657+
spaced values, but discarded their values.
6658+
"""
6659+
fig, ax = plt.subplots()
6660+
with pytest.raises(
6661+
ValueError, match=r"Length of X \(5\) must be one larger than the "
6662+
r"number of columns in C \(20\)"):
6663+
ax.pcolorfast(np.arange(5), np.arange(11), np.random.rand(10, 20))
6664+
6665+
with pytest.raises(
6666+
ValueError, match=r"Length of Y \(5\) must be one larger than the "
6667+
r"number of rows in C \(10\)"):
6668+
ax.pcolorfast(np.arange(21), np.arange(5), np.random.rand(10, 20))
6669+
6670+
66506671
def test_shared_scale():
66516672
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
66526673

0 commit comments

Comments
 (0)
0