Skip to content

Error on bad input to hexbin extents #27607

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
Jan 11, 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
4 changes: 4 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4999,6 +4999,10 @@ def reduce_C_function(C: array) -> float
ty = np.log10(ty)
if extent is not None:
xmin, xmax, ymin, ymax = extent
if xmin > xmax:
raise ValueError("In extent, xmax must be greater than xmin")
if ymin > ymax:
raise ValueError("In extent, ymax must be greater than ymin")
else:
xmin, xmax = (tx.min(), tx.max()) if len(x) else (0, 1)
ymin, ymax = (ty.min(), ty.max()) if len(y) else (0, 1)
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,18 @@ def test_hexbin_extent():
ax.hexbin("x", "y", extent=[.1, .3, .6, .7], data=data)


def test_hexbin_bad_extents():
fig, ax = plt.subplots()
data = (np.arange(20) / 20).reshape((2, 10))
x, y = data

with pytest.raises(ValueError, match="In extent, xmax must be greater than xmin"):
ax.hexbin(x, y, extent=(1, 0, 0, 1))

with pytest.raises(ValueError, match="In extent, ymax must be greater than ymin"):
ax.hexbin(x, y, extent=(0, 1, 1, 0))


@image_comparison(['hexbin_empty.png'], remove_text=True)
def test_hexbin_empty():
# From #3886: creating hexbin from empty dataset raises ValueError
Expand Down