Skip to content

FIX: pass renderer through adjust_bbox #29340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/matplotlib/_tight_bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
5 changes: 4 additions & 1 deletion lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Copy link
Member Author

@rcomer rcomer Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing the renderer here does not affect the target issue but I assume that passing it when we have it is more efficient than potentially calling _get_renderer again.


_bbox_inches_restore = (bbox_inches, restore_bbox)
else:
Expand Down
11 changes: 7 additions & 4 deletions lib/matplotlib/backends/backend_mixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Binary file not shown.
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_bbox_tight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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')
Loading