diff --git a/lib/matplotlib/_tight_bbox.py b/lib/matplotlib/_tight_bbox.py index db72bbdff020..7cd28dd031e6 100644 --- a/lib/matplotlib/_tight_bbox.py +++ b/lib/matplotlib/_tight_bbox.py @@ -5,7 +5,7 @@ from matplotlib.transforms import Bbox, TransformedBbox, Affine2D -def adjust_bbox(fig, bbox_inches, fixed_dpi=None): +def adjust_bbox(fig, bbox_inches, renderer, fixed_dpi=None): """ Temporarily adjust the figure so that only the specified area (bbox_inches) is saved. @@ -25,7 +25,7 @@ def adjust_bbox(fig, bbox_inches, fixed_dpi=None): for ax in fig.axes: locator = ax.get_axes_locator() if locator is not None: - ax.apply_aspect(locator(ax, None)) + ax.apply_aspect(locator(ax, renderer)) locator_list.append(locator) current_pos = ax.get_position(original=False).frozen() ax.set_axes_locator(lambda a, r, _pos=current_pos: _pos) @@ -70,7 +70,7 @@ def restore_bbox(): return restore_bbox -def process_figure_for_rasterizing(fig, bbox_inches_restore, fixed_dpi=None): +def process_figure_for_rasterizing(fig, bbox_inches_restore, renderer, fixed_dpi=None): """ A function that needs to be called when figure dpi changes during the drawing (e.g., rasterizing). It recovers the bbox and re-adjust it with @@ -79,6 +79,6 @@ def process_figure_for_rasterizing(fig, bbox_inches_restore, fixed_dpi=None): bbox_inches, restore_bbox = bbox_inches_restore restore_bbox() - r = adjust_bbox(fig, bbox_inches, fixed_dpi) + r = adjust_bbox(fig, bbox_inches, renderer, fixed_dpi) return bbox_inches, r diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 9b9cfe3ccc76..0b1bc77bcab7 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2153,6 +2153,9 @@ def print_figure( # so that we can inject the orientation with getattr(renderer, "_draw_disabled", nullcontext)(): self.figure.draw(renderer) + else: + renderer = None + if bbox_inches: if bbox_inches == "tight": bbox_inches = self.figure.get_tightbbox( @@ -2169,7 +2172,7 @@ def print_figure( # call adjust_bbox to save only the given area restore_bbox = _tight_bbox.adjust_bbox( - self.figure, bbox_inches, self.figure.canvas.fixed_dpi) + self.figure, bbox_inches, renderer, self.figure.canvas.fixed_dpi) _bbox_inches_restore = (bbox_inches, restore_bbox) else: diff --git a/lib/matplotlib/backends/backend_mixed.py b/lib/matplotlib/backends/backend_mixed.py index 6571d1928c0d..ea8f50a39ad1 100644 --- a/lib/matplotlib/backends/backend_mixed.py +++ b/lib/matplotlib/backends/backend_mixed.py @@ -75,15 +75,17 @@ def start_rasterizing(self): """ # change the dpi of the figure temporarily. self.figure.dpi = self.dpi - if self._bbox_inches_restore: # when tight bbox is used - r = process_figure_for_rasterizing(self.figure, - self._bbox_inches_restore) - self._bbox_inches_restore = r self._raster_renderer = self._raster_renderer_class( self._width*self.dpi, self._height*self.dpi, self.dpi) self._renderer = self._raster_renderer + if self._bbox_inches_restore: # when tight bbox is used + r = process_figure_for_rasterizing(self.figure, + self._bbox_inches_restore, + self._raster_renderer) + self._bbox_inches_restore = r + def stop_rasterizing(self): """ Exit "raster" mode. All of the drawing that was done since @@ -115,5 +117,6 @@ def stop_rasterizing(self): if self._bbox_inches_restore: # when tight bbox is used r = process_figure_for_rasterizing(self.figure, self._bbox_inches_restore, + self._vector_renderer, self._figdpi) self._bbox_inches_restore = r diff --git a/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_inset_rasterized.pdf b/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_inset_rasterized.pdf new file mode 100644 index 000000000000..17a80ef876d1 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_inset_rasterized.pdf differ diff --git a/lib/matplotlib/tests/test_bbox_tight.py b/lib/matplotlib/tests/test_bbox_tight.py index 5f2b397b650d..089b3a63005b 100644 --- a/lib/matplotlib/tests/test_bbox_tight.py +++ b/lib/matplotlib/tests/test_bbox_tight.py @@ -8,6 +8,7 @@ import matplotlib.path as mpath import matplotlib.patches as mpatches from matplotlib.ticker import FuncFormatter +from mpl_toolkits.axes_grid1.inset_locator import inset_axes @image_comparison(['bbox_inches_tight'], remove_text=True, @@ -173,3 +174,18 @@ def test_bbox_inches_fixed_aspect(): ax.plot([0, 1]) ax.set_xlim(0, 1) ax.set_aspect('equal') + + +@image_comparison(['bbox_inches_inset_rasterized'], extensions=['pdf'], + remove_text=True, savefig_kwarg={'bbox_inches': 'tight'}, + style='mpl20') +def test_bbox_inches_inset_rasterized(): + fig, ax = plt.subplots() + + arr = np.arange(100).reshape(10, 10) + im = ax.imshow(arr) + inset = inset_axes( + ax, width='10%', height='30%', loc='upper left', + bbox_to_anchor=(0.045, 0., 1, 1), bbox_transform=ax.transAxes) + + fig.colorbar(im, cax=inset, orientation='horizontal')