From 98f30b85b1ac8801dd69a641df7fa37fc9ab09b3 Mon Sep 17 00:00:00 2001 From: Clement Gilli Date: Fri, 19 Apr 2024 17:20:11 +0200 Subject: [PATCH 1/2] solve issue #28105 --- lib/matplotlib/axes/_axes.py | 10 +++++----- lib/matplotlib/tests/test_axes.py | 7 +++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index dc7f0c433fb4..5b8c1303344a 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5228,11 +5228,6 @@ def reduce_C_function(C: array) -> float vmin = vmax = None bins = None - # autoscale the norm with current accum values if it hasn't been set - if norm is not None: - if norm.vmin is None and norm.vmax is None: - norm.autoscale(accum) - if bins is not None: if not np.iterable(bins): minimum, maximum = min(accum), max(accum) @@ -5248,6 +5243,11 @@ def reduce_C_function(C: array) -> float collection._internal_update(kwargs) collection._scale_norm(norm, vmin, vmax) + # autoscale the norm with current accum values if it hasn't been set + if norm is not None: + if collection.norm.vmin is None and collection.norm.vmax is None: + collection.norm.autoscale() + corners = ((xmin, ymin), (xmax, ymax)) self.update_datalim(corners) self._request_autoscale_view(tight=True) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index deb83a26033c..c482bdc391e8 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -976,6 +976,13 @@ def test_hexbin_bad_extents(): with pytest.raises(ValueError, match="In extent, ymax must be greater than ymin"): ax.hexbin(x, y, extent=(0, 1, 1, 0)) +def test_hexbin_string_norm(): + fig, ax = plt.subplots() + hex = ax.hexbin(np.random.rand(10), np.random.rand(10), norm="log",vmin=2,vmax=5) + assert isinstance(hex,matplotlib.collections.PolyCollection) + assert isinstance(hex.norm,matplotlib.colors.LogNorm) + assert hex.norm.vmin == 2 + assert hex.norm.vmax == 5 @image_comparison(['hexbin_empty.png'], remove_text=True) def test_hexbin_empty(): From df94d8ad931e1277bb7c487c66b2690fb553c65c Mon Sep 17 00:00:00 2001 From: Clement Gilli Date: Fri, 26 Apr 2024 16:15:21 +0200 Subject: [PATCH 2/2] fix linter --- lib/matplotlib/tests/test_axes.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index c482bdc391e8..59890a52477c 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -976,14 +976,16 @@ def test_hexbin_bad_extents(): with pytest.raises(ValueError, match="In extent, ymax must be greater than ymin"): ax.hexbin(x, y, extent=(0, 1, 1, 0)) + def test_hexbin_string_norm(): fig, ax = plt.subplots() - hex = ax.hexbin(np.random.rand(10), np.random.rand(10), norm="log",vmin=2,vmax=5) - assert isinstance(hex,matplotlib.collections.PolyCollection) - assert isinstance(hex.norm,matplotlib.colors.LogNorm) + hex = ax.hexbin(np.random.rand(10), np.random.rand(10), norm="log", vmin=2, vmax=5) + assert isinstance(hex, matplotlib.collections.PolyCollection) + assert isinstance(hex.norm, matplotlib.colors.LogNorm) assert hex.norm.vmin == 2 assert hex.norm.vmax == 5 + @image_comparison(['hexbin_empty.png'], remove_text=True) def test_hexbin_empty(): # From #3886: creating hexbin from empty dataset raises ValueError