Skip to content

Commit 28469b4

Browse files
authored
Merge pull request #27818 from dstansby/log-hexbin-offsets
Set polygon offsets for log scaled hexbin
2 parents 98506d0 + ecb4d65 commit 28469b4

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

lib/matplotlib/axes/_axes.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -5028,7 +5028,7 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
50285028
A `.PolyCollection` defining the hexagonal bins.
50295029
50305030
- `.PolyCollection.get_offsets` contains a Mx2 array containing
5031-
the x, y positions of the M hexagon centers.
5031+
the x, y positions of the M hexagon centers in data coordinates.
50325032
- `.PolyCollection.get_array` contains the values of the M
50335033
hexagons.
50345034
@@ -5206,7 +5206,7 @@ def reduce_C_function(C: array) -> float
52065206
linewidths = [mpl.rcParams['patch.linewidth']]
52075207

52085208
if xscale == 'log' or yscale == 'log':
5209-
polygons = np.expand_dims(polygon, 0) + np.expand_dims(offsets, 1)
5209+
polygons = np.expand_dims(polygon, 0)
52105210
if xscale == 'log':
52115211
polygons[:, :, 0] = 10.0 ** polygons[:, :, 0]
52125212
xmin = 10.0 ** xmin
@@ -5217,20 +5217,16 @@ def reduce_C_function(C: array) -> float
52175217
ymin = 10.0 ** ymin
52185218
ymax = 10.0 ** ymax
52195219
self.set_yscale(yscale)
5220-
collection = mcoll.PolyCollection(
5221-
polygons,
5222-
edgecolors=edgecolors,
5223-
linewidths=linewidths,
5224-
)
52255220
else:
5226-
collection = mcoll.PolyCollection(
5227-
[polygon],
5228-
edgecolors=edgecolors,
5229-
linewidths=linewidths,
5230-
offsets=offsets,
5231-
offset_transform=mtransforms.AffineDeltaTransform(
5232-
self.transData),
5233-
)
5221+
polygons = [polygon]
5222+
5223+
collection = mcoll.PolyCollection(
5224+
polygons,
5225+
edgecolors=edgecolors,
5226+
linewidths=linewidths,
5227+
offsets=offsets,
5228+
offset_transform=mtransforms.AffineDeltaTransform(self.transData)
5229+
)
52345230

52355231
# Set normalizer if bins is 'log'
52365232
if cbook._str_equal(bins, 'log'):

lib/matplotlib/tests/test_axes.py

+21
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,27 @@ def test_hexbin_log():
10201020
marginals=True, reduce_C_function=np.sum)
10211021
plt.colorbar(h)
10221022

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

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

0 commit comments

Comments
 (0)