Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ecb4d65

Browse files
committed
Set polygon offsets for log scaled hexbin
1 parent 78b6bdc commit ecb4d65

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
@@ -5014,7 +5014,7 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
50145014
A `.PolyCollection` defining the hexagonal bins.
50155015
50165016
- `.PolyCollection.get_offsets` contains a Mx2 array containing
5017-
the x, y positions of the M hexagon centers.
5017+
the x, y positions of the M hexagon centers in data coordinates.
50185018
- `.PolyCollection.get_array` contains the values of the M
50195019
hexagons.
50205020
@@ -5192,7 +5192,7 @@ def reduce_C_function(C: array) -> float
51925192
linewidths = [mpl.rcParams['patch.linewidth']]
51935193

51945194
if xscale == 'log' or yscale == 'log':
5195-
polygons = np.expand_dims(polygon, 0) + np.expand_dims(offsets, 1)
5195+
polygons = np.expand_dims(polygon, 0)
51965196
if xscale == 'log':
51975197
polygons[:, :, 0] = 10.0 ** polygons[:, :, 0]
51985198
xmin = 10.0 ** xmin
@@ -5203,20 +5203,16 @@ def reduce_C_function(C: array) -> float
52035203
ymin = 10.0 ** ymin
52045204
ymax = 10.0 ** ymax
52055205
self.set_yscale(yscale)
5206-
collection = mcoll.PolyCollection(
5207-
polygons,
5208-
edgecolors=edgecolors,
5209-
linewidths=linewidths,
5210-
)
52115206
else:
5212-
collection = mcoll.PolyCollection(
5213-
[polygon],
5214-
edgecolors=edgecolors,
5215-
linewidths=linewidths,
5216-
offsets=offsets,
5217-
offset_transform=mtransforms.AffineDeltaTransform(
5218-
self.transData),
5219-
)
5207+
polygons = [polygon]
5208+
5209+
collection = mcoll.PolyCollection(
5210+
polygons,
5211+
edgecolors=edgecolors,
5212+
linewidths=linewidths,
5213+
offsets=offsets,
5214+
offset_transform=mtransforms.AffineDeltaTransform(self.transData)
5215+
)
52205216

52215217
# Set normalizer if bins is 'log'
52225218
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
@@ -1017,6 +1017,27 @@ def test_hexbin_log():
10171017
marginals=True, reduce_C_function=np.sum)
10181018
plt.colorbar(h)
10191019

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

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

0 commit comments

Comments
 (0)