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

Skip to content

Commit 084a94f

Browse files
committed
Merge pull request #1636 from mdboom/hexbin-log
hexbin log scale is broken in matplotlib 1.2.0
2 parents 78fa072 + 2f1fda6 commit 084a94f

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

lib/matplotlib/axes.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5443,7 +5443,7 @@ def errorbar(self, x, y, yerr=None, xerr=None,
54435443
lines_kw['transform'] = kwargs['transform']
54445444
if 'zorder' in kwargs:
54455445
lines_kw['zorder'] = kwargs['zorder']
5446-
5446+
54475447
# arrays fine here, they are booleans and hence not units
54485448
if not iterable(lolims):
54495449
lolims = np.asarray([lolims]*len(x), bool)
@@ -6432,32 +6432,39 @@ def hexbin(self, x, y, C = None, gridsize = 100, bins = None,
64326432
offsets = offsets[good_idxs,:]
64336433
accum = accum[good_idxs]
64346434

6435-
if xscale=='log':
6436-
offsets[:,0] = 10**(offsets[:,0])
6437-
xmin = 10**xmin
6438-
xmax = 10**xmax
6439-
self.set_xscale('log')
6440-
if yscale=='log':
6441-
offsets[:,1] = 10**(offsets[:,1])
6442-
ymin = 10**ymin
6443-
ymax = 10**ymax
6444-
self.set_yscale('log')
6445-
64466435
polygon = np.zeros((6, 2), float)
64476436
polygon[:,0] = sx * np.array([ 0.5, 0.5, 0.0, -0.5, -0.5, 0.0])
64486437
polygon[:,1] = sy * np.array([-0.5, 0.5, 1.0, 0.5, -0.5, -1.0]) / 3.0
64496438

64506439
if edgecolors=='none':
64516440
edgecolors = 'face'
64526441

6453-
collection = mcoll.PolyCollection(
6454-
[polygon],
6455-
edgecolors = edgecolors,
6456-
linewidths = linewidths,
6457-
offsets = offsets,
6458-
transOffset = mtransforms.IdentityTransform(),
6459-
offset_position = "data"
6460-
)
6442+
if xscale == 'log' or yscale == 'log':
6443+
polygons = np.expand_dims(polygon, 0) + np.expand_dims(offsets, 1)
6444+
if xscale == 'log':
6445+
polygons[:, :, 0] = 10.0 ** polygons[:, :, 0]
6446+
xmin = 10.0 ** xmin
6447+
xmax = 10.0 ** xmax
6448+
self.set_xscale(xscale)
6449+
if yscale == 'log':
6450+
polygons[:, :, 1] = 10.0 ** polygons[:, :, 1]
6451+
ymin = 10.0 ** ymin
6452+
ymax = 10.0 ** ymax
6453+
self.set_yscale(yscale)
6454+
collection = mcoll.PolyCollection(
6455+
polygons,
6456+
edgecolors=edgecolors,
6457+
linewidths=linewidths,
6458+
)
6459+
else:
6460+
collection = mcoll.PolyCollection(
6461+
[polygon],
6462+
edgecolors=edgecolors,
6463+
linewidths=linewidths,
6464+
offsets=offsets,
6465+
transOffset=mtransforms.IdentityTransform(),
6466+
offset_position="data"
6467+
)
64616468

64626469
if isinstance(norm, mcolors.LogNorm):
64636470
if (accum==0).any():

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,21 @@ def test_hexbin_extent():
419419

420420
ax.hexbin(x, y, extent=[.1, .3, .6, .7])
421421

422+
@image_comparison(baseline_images=['hexbin_log'],
423+
remove_text=True,
424+
extensions=['png'])
425+
def test_hexbin_log():
426+
# Issue #1636
427+
fig = plt.figure()
428+
429+
np.random.seed(0)
430+
n = 100000
431+
x = np.random.standard_normal(n)
432+
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
433+
y = np.power(2, y * 0.5)
434+
ax = fig.add_subplot(111)
435+
ax.hexbin(x, y, yscale='log')
436+
422437
@cleanup
423438
def test_inverted_limits():
424439
# Test gh:1553

0 commit comments

Comments
 (0)