Skip to content

Commit 3edb171

Browse files
committed
Set norm to log if bins=='log' in hexbin
1 parent 11dcf76 commit 3edb171

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4439,9 +4439,23 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
44394439
offset_position="data"
44404440
)
44414441

4442+
# Check for valid norm
4443+
if norm is not None and not isinstance(norm, mcolors.Normalize):
4444+
msg = "'norm' must be an instance of 'mcolors.Normalize'"
4445+
raise ValueError(msg)
4446+
4447+
# Set normalizer if bins is 'log'
4448+
if bins == 'log':
4449+
if norm is not None:
4450+
warnings.warn("Only one of 'bins' and 'norm' arguments can be "
4451+
"supplied, ignoring bins={}".format(bins))
4452+
else:
4453+
norm = mcolors.LogNorm()
4454+
bins = None
4455+
44424456
if isinstance(norm, mcolors.LogNorm):
44434457
if (accum == 0).any():
4444-
# make sure we have not zeros
4458+
# make sure we have no zeros
44454459
accum += 1
44464460

44474461
# autoscale the norm with curren accum values if it hasn't
@@ -4450,20 +4464,14 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
44504464
if norm.vmin is None and norm.vmax is None:
44514465
norm.autoscale(accum)
44524466

4453-
# Transform accum if needed
4454-
if bins == 'log':
4455-
accum = np.log10(accum + 1)
4456-
elif bins is not None:
4467+
if bins is not None:
44574468
if not iterable(bins):
44584469
minimum, maximum = min(accum), max(accum)
44594470
bins -= 1 # one less edge than bins
44604471
bins = minimum + (maximum - minimum) * np.arange(bins) / bins
44614472
bins = np.sort(bins)
44624473
accum = bins.searchsorted(accum)
44634474

4464-
if norm is not None and not isinstance(norm, mcolors.Normalize):
4465-
msg = "'norm' must be an instance of 'mcolors.Normalize'"
4466-
raise ValueError(msg)
44674475
collection.set_array(accum)
44684476
collection.set_cmap(cmap)
44694477
collection.set_norm(norm)

lib/matplotlib/tests/test_axes.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -826,19 +826,18 @@ def __init__(self, x, y):
826826

827827

828828
@image_comparison(baseline_images=['hexbin_log'],
829-
remove_text=True,
830-
extensions=['png'])
829+
extensions=['png'], style='mpl20')
831830
def test_hexbin_log():
832-
# Issue #1636
833-
fig = plt.figure()
834-
835-
np.random.seed(0)
831+
# Issue #1636 (and also test log scaled colorbar)
832+
np.random.seed(19680801)
836833
n = 100000
837834
x = np.random.standard_normal(n)
838835
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
839836
y = np.power(2, y * 0.5)
840-
ax = fig.add_subplot(111)
841-
ax.hexbin(x, y, yscale='log')
837+
838+
fig, ax = plt.subplots()
839+
h = ax.hexbin(x, y, yscale='log', bins='log')
840+
plt.colorbar(h)
842841

843842

844843
def test_inverted_limits():

0 commit comments

Comments
 (0)