@@ -35,6 +35,11 @@ def _l(a, r, pos=pos):
35
35
ax .set_axes_locator (_l )
36
36
ax .set_aspect ("auto" )
37
37
38
+ legend_anchors = {}
39
+ for l in fig .legends : # store legend anchors for the restore step.
40
+ anch = l .get_bbox_to_anchor ()
41
+ legend_anchors [l ] = anch .frozen ()
42
+
38
43
def restore_bbox ():
39
44
for ax , asp , loc in zip (fig .axes , asp_list , locator_list ):
40
45
ax .set_aspect (asp )
@@ -45,6 +50,9 @@ def restore_bbox():
45
50
fig .set_tight_layout (orig_tight_layout )
46
51
fig .transFigure ._boxout = _boxout
47
52
fig .transFigure .invalidate ()
53
+ for l in fig .legends : # re-set the anchor for legends...
54
+ anch = legend_anchors [l ].transformed (fig .transFigure .inverted ())
55
+ l .set_bbox_to_anchor (anch )
48
56
fig .patch .set_bounds (0 , 0 , 1 , 1 )
49
57
50
58
if fixed_dpi is not None :
@@ -59,6 +67,11 @@ def restore_bbox():
59
67
fig .bbox_inches = Bbox .from_bounds (0 , 0 ,
60
68
bbox_inches .width , bbox_inches .height )
61
69
x0 , y0 = _bbox .x0 , _bbox .y0
70
+
71
+ for l in fig .legends : # offset the anchors...
72
+ anchor = legend_anchors [l ].translated (- x0 , - y0 ) # in pixels
73
+ l .set_bbox_to_anchor (anchor .transformed (fig .transFigure .inverted ()))
74
+
62
75
w1 , h1 = fig .bbox .width * dpi_scale , fig .bbox .height * dpi_scale
63
76
fig .transFigure ._boxout = Bbox .from_bounds (- x0 , - y0 , w1 , h1 )
64
77
fig .transFigure .invalidate ()
0 commit comments