diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index dc7f0c433fb4..cc6f0ba82d3a 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5014,7 +5014,7 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None, A `.PolyCollection` defining the hexagonal bins. - `.PolyCollection.get_offsets` contains a Mx2 array containing - the x, y positions of the M hexagon centers. + the x, y positions of the M hexagon centers in data coordinates. - `.PolyCollection.get_array` contains the values of the M hexagons. @@ -5192,7 +5192,7 @@ def reduce_C_function(C: array) -> float linewidths = [mpl.rcParams['patch.linewidth']] if xscale == 'log' or yscale == 'log': - polygons = np.expand_dims(polygon, 0) + np.expand_dims(offsets, 1) + polygons = np.expand_dims(polygon, 0) if xscale == 'log': polygons[:, :, 0] = 10.0 ** polygons[:, :, 0] xmin = 10.0 ** xmin @@ -5203,20 +5203,16 @@ def reduce_C_function(C: array) -> float ymin = 10.0 ** ymin ymax = 10.0 ** ymax self.set_yscale(yscale) - collection = mcoll.PolyCollection( - polygons, - edgecolors=edgecolors, - linewidths=linewidths, - ) else: - collection = mcoll.PolyCollection( - [polygon], - edgecolors=edgecolors, - linewidths=linewidths, - offsets=offsets, - offset_transform=mtransforms.AffineDeltaTransform( - self.transData), - ) + polygons = [polygon] + + collection = mcoll.PolyCollection( + polygons, + edgecolors=edgecolors, + linewidths=linewidths, + offsets=offsets, + offset_transform=mtransforms.AffineDeltaTransform(self.transData) + ) # Set normalizer if bins is 'log' if cbook._str_equal(bins, 'log'): diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 3666b16e6dad..deeec45332d9 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1017,6 +1017,27 @@ def test_hexbin_log(): marginals=True, reduce_C_function=np.sum) plt.colorbar(h) + # Make sure offsets are set + assert h.get_offsets().shape == (11558, 2) + + +def test_hexbin_log_offsets(): + x = np.geomspace(1, 100, 500) + + fig, ax = plt.subplots() + h = ax.hexbin(x, x, xscale='log', yscale='log', gridsize=2) + np.testing.assert_almost_equal( + h.get_offsets(), + np.array( + [[0, 0], + [0, 2], + [1, 0], + [1, 2], + [2, 0], + [2, 2], + [0.5, 1], + [1.5, 1]])) + @image_comparison(["hexbin_linear.png"], style="mpl20", remove_text=True) def test_hexbin_linear():