From 537a3d51bbe558b8c66d2be3c5a8ca89a5d0a123 Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Sun, 2 Jun 2019 17:04:53 -1000 Subject: [PATCH] Backport PR #14397: Correctly set clip_path on pcolorfast return artist. --- lib/matplotlib/axes/_axes.py | 3 +++ lib/matplotlib/tests/test_bbox_tight.py | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 5d7924cbd887..baf5da20dcb2 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -6395,6 +6395,9 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None, ret.set_clim(vmin, vmax) else: ret.autoscale_None() + if ret.get_clip_path() is None: + # image does not already have clipping set, clip to axes patch + ret.set_clip_path(self.patch) ret.sticky_edges.x[:] = [xl, xr] ret.sticky_edges.y[:] = [yb, yt] diff --git a/lib/matplotlib/tests/test_bbox_tight.py b/lib/matplotlib/tests/test_bbox_tight.py index 8c99c4572f76..6ce57bf40c75 100644 --- a/lib/matplotlib/tests/test_bbox_tight.py +++ b/lib/matplotlib/tests/test_bbox_tight.py @@ -90,9 +90,21 @@ def test_bbox_inches_tight_raster(): def test_only_on_non_finite_bbox(): - fig, ax = plt.subplots() ax.annotate("", xy=(0, float('nan'))) ax.set_axis_off() # we only need to test that it does not error out on save fig.savefig(BytesIO(), bbox_inches='tight', format='png') + + +def test_tight_pcolorfast(): + fig, ax = plt.subplots() + ax.pcolorfast(np.arange(4).reshape((2, 2))) + ax.set(ylim=(0, .1)) + buf = BytesIO() + fig.savefig(buf, bbox_inches="tight") + buf.seek(0) + height, width, _ = plt.imread(buf).shape + # Previously, the bbox would include the area of the image clipped out by + # the axes, resulting in a very tall image given the y limits of (0, 0.1). + assert width > height