diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 74243b71a152..ecb0e611af89 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -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) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index d8d7cb0144f1..e3379b7ed78f 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -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