Skip to content

Set norm to log if bins=='log' in hexbin #9223

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
Jul 12, 2018
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
6 changes: 6 additions & 0 deletions doc/api/api_changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ Deprecations
`.Legend.draggable()` is drepecated in favor of `.Legend.set_draggable()`.
``Legend.draggable`` may be reintroduced as a property in future releases.

Colorbar for log-scaled hexbin
------------------------------

When using `hexbin` and plotting with a logarithmic color scale, the colorbar
ticks are now correctly log scaled. Previously the tick values were linear
scaled log(number of counts).

API Changes in 2.2.0
====================
Expand Down
24 changes: 16 additions & 8 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4326,9 +4326,23 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
offset_position="data"
)

# Check for valid norm
if norm is not None and not isinstance(norm, mcolors.Normalize):
msg = "'norm' must be an instance of 'mcolors.Normalize'"
raise ValueError(msg)

# Set normalizer if bins is 'log'
if bins == 'log':
if norm is not None:
warnings.warn("Only one of 'bins' and 'norm' arguments can be "
"supplied, ignoring bins={}".format(bins))
else:
norm = mcolors.LogNorm()
bins = None

if isinstance(norm, mcolors.LogNorm):
if (accum == 0).any():
# make sure we have not zeros
# make sure we have no zeros
accum += 1

# autoscale the norm with curren accum values if it hasn't
Expand All @@ -4337,20 +4351,14 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
if norm.vmin is None and norm.vmax is None:
norm.autoscale(accum)

# Transform accum if needed
if bins == 'log':
accum = np.log10(accum + 1)
elif bins is not None:
if bins is not None:
if not iterable(bins):
minimum, maximum = min(accum), max(accum)
bins -= 1 # one less edge than bins
bins = minimum + (maximum - minimum) * np.arange(bins) / bins
bins = np.sort(bins)
accum = bins.searchsorted(accum)

if norm is not None and not isinstance(norm, mcolors.Normalize):
raise ValueError(
"'norm' must be an instance of 'mcolors.Normalize'")
collection.set_array(accum)
collection.set_cmap(cmap)
collection.set_norm(norm)
Expand Down
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/hexbin_log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,18 +858,18 @@ def __init__(self, x, y):


@image_comparison(baseline_images=['hexbin_log'],
remove_text=True,
extensions=['png'])
extensions=['png'], style='mpl20')
def test_hexbin_log():
# Issue #1636
np.random.seed(0)
# Issue #1636 (and also test log scaled colorbar)
np.random.seed(19680801)
n = 100000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
y = np.power(2, y * 0.5)

fig, ax = plt.subplots()
ax.hexbin(x, y, yscale='log')
h = ax.hexbin(x, y, yscale='log', bins='log')
plt.colorbar(h)


def test_inverted_limits():
Expand Down