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

Skip to content

Commit dc63ec7

Browse files
authored
Merge pull request #29340 from rcomer/inset_axes-renderer
FIX: pass renderer through adjust_bbox
2 parents 469e03b + 0908973 commit dc63ec7

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

lib/matplotlib/_tight_bbox.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from matplotlib.transforms import Bbox, TransformedBbox, Affine2D
66

77

8-
def adjust_bbox(fig, bbox_inches, fixed_dpi=None):
8+
def adjust_bbox(fig, bbox_inches, renderer, fixed_dpi=None):
99
"""
1010
Temporarily adjust the figure so that only the specified area
1111
(bbox_inches) is saved.
@@ -25,7 +25,7 @@ def adjust_bbox(fig, bbox_inches, fixed_dpi=None):
2525
for ax in fig.axes:
2626
locator = ax.get_axes_locator()
2727
if locator is not None:
28-
ax.apply_aspect(locator(ax, None))
28+
ax.apply_aspect(locator(ax, renderer))
2929
locator_list.append(locator)
3030
current_pos = ax.get_position(original=False).frozen()
3131
ax.set_axes_locator(lambda a, r, _pos=current_pos: _pos)
@@ -70,7 +70,7 @@ def restore_bbox():
7070
return restore_bbox
7171

7272

73-
def process_figure_for_rasterizing(fig, bbox_inches_restore, fixed_dpi=None):
73+
def process_figure_for_rasterizing(fig, bbox_inches_restore, renderer, fixed_dpi=None):
7474
"""
7575
A function that needs to be called when figure dpi changes during the
7676
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):
7979

8080
bbox_inches, restore_bbox = bbox_inches_restore
8181
restore_bbox()
82-
r = adjust_bbox(fig, bbox_inches, fixed_dpi)
82+
r = adjust_bbox(fig, bbox_inches, renderer, fixed_dpi)
8383

8484
return bbox_inches, r

lib/matplotlib/backend_bases.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,9 @@ def print_figure(
21532153
# so that we can inject the orientation
21542154
with getattr(renderer, "_draw_disabled", nullcontext)():
21552155
self.figure.draw(renderer)
2156+
else:
2157+
renderer = None
2158+
21562159
if bbox_inches:
21572160
if bbox_inches == "tight":
21582161
bbox_inches = self.figure.get_tightbbox(
@@ -2169,7 +2172,7 @@ def print_figure(
21692172

21702173
# call adjust_bbox to save only the given area
21712174
restore_bbox = _tight_bbox.adjust_bbox(
2172-
self.figure, bbox_inches, self.figure.canvas.fixed_dpi)
2175+
self.figure, bbox_inches, renderer, self.figure.canvas.fixed_dpi)
21732176

21742177
_bbox_inches_restore = (bbox_inches, restore_bbox)
21752178
else:

lib/matplotlib/backends/backend_mixed.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,17 @@ def start_rasterizing(self):
7575
"""
7676
# change the dpi of the figure temporarily.
7777
self.figure.dpi = self.dpi
78-
if self._bbox_inches_restore: # when tight bbox is used
79-
r = process_figure_for_rasterizing(self.figure,
80-
self._bbox_inches_restore)
81-
self._bbox_inches_restore = r
8278

8379
self._raster_renderer = self._raster_renderer_class(
8480
self._width*self.dpi, self._height*self.dpi, self.dpi)
8581
self._renderer = self._raster_renderer
8682

83+
if self._bbox_inches_restore: # when tight bbox is used
84+
r = process_figure_for_rasterizing(self.figure,
85+
self._bbox_inches_restore,
86+
self._raster_renderer)
87+
self._bbox_inches_restore = r
88+
8789
def stop_rasterizing(self):
8890
"""
8991
Exit "raster" mode. All of the drawing that was done since
@@ -115,5 +117,6 @@ def stop_rasterizing(self):
115117
if self._bbox_inches_restore: # when tight bbox is used
116118
r = process_figure_for_rasterizing(self.figure,
117119
self._bbox_inches_restore,
120+
self._vector_renderer,
118121
self._figdpi)
119122
self._bbox_inches_restore = r

lib/matplotlib/tests/test_bbox_tight.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import matplotlib.path as mpath
99
import matplotlib.patches as mpatches
1010
from matplotlib.ticker import FuncFormatter
11+
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
1112

1213

1314
@image_comparison(['bbox_inches_tight'], remove_text=True,
@@ -173,3 +174,18 @@ def test_bbox_inches_fixed_aspect():
173174
ax.plot([0, 1])
174175
ax.set_xlim(0, 1)
175176
ax.set_aspect('equal')
177+
178+
179+
@image_comparison(['bbox_inches_inset_rasterized'], extensions=['pdf'],
180+
remove_text=True, savefig_kwarg={'bbox_inches': 'tight'},
181+
style='mpl20')
182+
def test_bbox_inches_inset_rasterized():
183+
fig, ax = plt.subplots()
184+
185+
arr = np.arange(100).reshape(10, 10)
186+
im = ax.imshow(arr)
187+
inset = inset_axes(
188+
ax, width='10%', height='30%', loc='upper left',
189+
bbox_to_anchor=(0.045, 0., 1, 1), bbox_transform=ax.transAxes)
190+
191+
fig.colorbar(im, cax=inset, orientation='horizontal')

0 commit comments

Comments
 (0)