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

Skip to content

MNT: Check the input sizes of regular X,Y in pcolorfast #28853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6634,6 +6634,15 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
if x.size == 2 and y.size == 2:
style = "image"
else:
if x.size != nc + 1:
raise ValueError(
f"Length of X ({x.size}) must be one larger than the "
f"number of columns in C ({nc})")
if y.size != nr + 1:
raise ValueError(
f"Length of Y ({y.size}) must be one larger than the "
f"number of rows in C ({nr})"
)
dx = np.diff(x)
dy = np.diff(y)
if (np.ptp(dx) < 0.01 * abs(dx.mean()) and
Expand Down
21 changes: 21 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6626,6 +6626,27 @@ def test_pcolorfast_bad_dims():
ax.pcolorfast(np.empty(6), np.empty((4, 7)), np.empty((8, 8)))


def test_pcolorfast_regular_xy_incompatible_size():
"""
Test that the sizes of X, Y, C are compatible for regularly spaced X, Y.

Note that after the regualar-spacing check, pcolorfast may go into the
fast "image" mode, where the individual X, Y positions are not used anymore.
Therefore, the algorithm had worked with any regularly number of regularly
spaced values, but discarded their values.
"""
fig, ax = plt.subplots()
with pytest.raises(
ValueError, match=r"Length of X \(5\) must be one larger than the "
r"number of columns in C \(20\)"):
ax.pcolorfast(np.arange(5), np.arange(11), np.random.rand(10, 20))

with pytest.raises(
ValueError, match=r"Length of Y \(5\) must be one larger than the "
r"number of rows in C \(10\)"):
ax.pcolorfast(np.arange(21), np.arange(5), np.random.rand(10, 20))


def test_shared_scale():
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)

Expand Down
Loading