Skip to content

Set polygon offsets for log scaled hexbin #27818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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'):
Expand Down
21 changes: 21 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down