Skip to content

Fix: [Bug]: Setting norm by string doesn't work for hexbin #28105 #28106

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 2 commits into from
Apr 29, 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
10 changes: 5 additions & 5 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5228,11 +5228,6 @@ def reduce_C_function(C: array) -> float
vmin = vmax = None
bins = None

# autoscale the norm with current accum values if it hasn't been set
if norm is not None:
if norm.vmin is None and norm.vmax is None:
norm.autoscale(accum)

if bins is not None:
if not np.iterable(bins):
minimum, maximum = min(accum), max(accum)
Expand All @@ -5248,6 +5243,11 @@ def reduce_C_function(C: array) -> float
collection._internal_update(kwargs)
collection._scale_norm(norm, vmin, vmax)

# autoscale the norm with current accum values if it hasn't been set
if norm is not None:
if collection.norm.vmin is None and collection.norm.vmax is None:
collection.norm.autoscale()

corners = ((xmin, ymin), (xmax, ymax))
self.update_datalim(corners)
self._request_autoscale_view(tight=True)
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,15 @@ def test_hexbin_bad_extents():
ax.hexbin(x, y, extent=(0, 1, 1, 0))


def test_hexbin_string_norm():
fig, ax = plt.subplots()
hex = ax.hexbin(np.random.rand(10), np.random.rand(10), norm="log", vmin=2, vmax=5)
assert isinstance(hex, matplotlib.collections.PolyCollection)
assert isinstance(hex.norm, matplotlib.colors.LogNorm)
assert hex.norm.vmin == 2
assert hex.norm.vmax == 5


@image_comparison(['hexbin_empty.png'], remove_text=True)
def test_hexbin_empty():
# From #3886: creating hexbin from empty dataset raises ValueError
Expand Down
Loading