diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index 19095e060d02..4d8bbb36d380 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -5443,7 +5443,7 @@ def errorbar(self, x, y, yerr=None, xerr=None, lines_kw['transform'] = kwargs['transform'] if 'zorder' in kwargs: lines_kw['zorder'] = kwargs['zorder'] - + # arrays fine here, they are booleans and hence not units if not iterable(lolims): lolims = np.asarray([lolims]*len(x), bool) @@ -6432,17 +6432,6 @@ def hexbin(self, x, y, C = None, gridsize = 100, bins = None, offsets = offsets[good_idxs,:] accum = accum[good_idxs] - if xscale=='log': - offsets[:,0] = 10**(offsets[:,0]) - xmin = 10**xmin - xmax = 10**xmax - self.set_xscale('log') - if yscale=='log': - offsets[:,1] = 10**(offsets[:,1]) - ymin = 10**ymin - ymax = 10**ymax - self.set_yscale('log') - polygon = np.zeros((6, 2), float) polygon[:,0] = sx * np.array([ 0.5, 0.5, 0.0, -0.5, -0.5, 0.0]) polygon[:,1] = sy * np.array([-0.5, 0.5, 1.0, 0.5, -0.5, -1.0]) / 3.0 @@ -6450,14 +6439,32 @@ def hexbin(self, x, y, C = None, gridsize = 100, bins = None, if edgecolors=='none': edgecolors = 'face' - collection = mcoll.PolyCollection( - [polygon], - edgecolors = edgecolors, - linewidths = linewidths, - offsets = offsets, - transOffset = mtransforms.IdentityTransform(), - offset_position = "data" - ) + if xscale == 'log' or yscale == 'log': + polygons = np.expand_dims(polygon, 0) + np.expand_dims(offsets, 1) + if xscale == 'log': + polygons[:, :, 0] = 10.0 ** polygons[:, :, 0] + xmin = 10.0 ** xmin + xmax = 10.0 ** xmax + self.set_xscale(xscale) + if yscale == 'log': + polygons[:, :, 1] = 10.0 ** polygons[:, :, 1] + 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, + transOffset=mtransforms.IdentityTransform(), + offset_position="data" + ) if isinstance(norm, mcolors.LogNorm): if (accum==0).any(): diff --git a/lib/matplotlib/tests/baseline_images/test_axes/hexbin_log.png b/lib/matplotlib/tests/baseline_images/test_axes/hexbin_log.png new file mode 100644 index 000000000000..8940524bbd5b Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/hexbin_log.png differ diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 82aac0d87058..f73a80fd39e3 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -419,6 +419,21 @@ def test_hexbin_extent(): ax.hexbin(x, y, extent=[.1, .3, .6, .7]) +@image_comparison(baseline_images=['hexbin_log'], + remove_text=True, + extensions=['png']) +def test_hexbin_log(): + # Issue #1636 + fig = plt.figure() + + np.random.seed(0) + 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) + ax = fig.add_subplot(111) + ax.hexbin(x, y, yscale='log') + @cleanup def test_inverted_limits(): # Test gh:1553