Skip to content

Commit 2845643

Browse files
committed
Set polygon offsets for log scaled hexbin
1 parent 5e34777 commit 2845643

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4925,7 +4925,7 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
49254925
A `.PolyCollection` defining the hexagonal bins.
49264926
49274927
- `.PolyCollection.get_offsets` contains a Mx2 array containing
4928-
the x, y positions of the M hexagon centers.
4928+
the x, y positions of the M hexagon centers in data coordinates.
49294929
- `.PolyCollection.get_array` contains the values of the M
49304930
hexagons.
49314931
@@ -5103,7 +5103,7 @@ def reduce_C_function(C: array) -> float
51035103
linewidths = [mpl.rcParams['patch.linewidth']]
51045104

51055105
if xscale == 'log' or yscale == 'log':
5106-
polygons = np.expand_dims(polygon, 0) + np.expand_dims(offsets, 1)
5106+
polygons = np.expand_dims(polygon, 0)
51075107
if xscale == 'log':
51085108
polygons[:, :, 0] = 10.0 ** polygons[:, :, 0]
51095109
xmin = 10.0 ** xmin
@@ -5114,20 +5114,16 @@ def reduce_C_function(C: array) -> float
51145114
ymin = 10.0 ** ymin
51155115
ymax = 10.0 ** ymax
51165116
self.set_yscale(yscale)
5117-
collection = mcoll.PolyCollection(
5118-
polygons,
5119-
edgecolors=edgecolors,
5120-
linewidths=linewidths,
5121-
)
51225117
else:
5123-
collection = mcoll.PolyCollection(
5124-
[polygon],
5125-
edgecolors=edgecolors,
5126-
linewidths=linewidths,
5127-
offsets=offsets,
5128-
offset_transform=mtransforms.AffineDeltaTransform(
5129-
self.transData),
5130-
)
5118+
polygons = [polygon]
5119+
5120+
collection = mcoll.PolyCollection(
5121+
polygons,
5122+
edgecolors=edgecolors,
5123+
linewidths=linewidths,
5124+
offsets=offsets,
5125+
offset_transform=mtransforms.AffineDeltaTransform(self.transData)
5126+
)
51315127

51325128
# Set normalizer if bins is 'log'
51335129
if cbook._str_equal(bins, 'log'):

lib/matplotlib/tests/test_axes.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,27 @@ def test_hexbin_log():
10161016
marginals=True, reduce_C_function=np.sum)
10171017
plt.colorbar(h)
10181018

1019+
# Make sure offsets are set
1020+
assert h.get_offsets().shape == (11558, 2)
1021+
1022+
1023+
def test_hexbin_log_offsets():
1024+
x = np.geomspace(1, 100, 500)
1025+
1026+
fig, ax = plt.subplots()
1027+
h = ax.hexbin(x, x, xscale='log', yscale='log', gridsize=2)
1028+
np.testing.assert_almost_equal(
1029+
h.get_offsets(),
1030+
np.array(
1031+
[[0, 0],
1032+
[0, 2],
1033+
[1, 0],
1034+
[1, 2],
1035+
[2, 0],
1036+
[2, 2],
1037+
[0.5, 1],
1038+
[1.5, 1]]))
1039+
10191040

10201041
@image_comparison(["hexbin_linear.png"], style="mpl20", remove_text=True)
10211042
def test_hexbin_linear():

0 commit comments

Comments
 (0)