1111from matplotlib .transforms import Bbox , TransformedBbox , Affine2D
1212
1313
14- def adjust_bbox (fig , format , bbox_inches ):
14+ def adjust_bbox (fig , bbox_inches , fixed_dpi = None ):
1515 """
1616 Temporarily adjust the figure so that only the specified area
1717 (bbox_inches) is saved.
@@ -50,74 +50,31 @@ def restore_bbox():
5050 fig .transFigure .invalidate ()
5151 fig .patch .set_bounds (0 , 0 , 1 , 1 )
5252
53- adjust_bbox_handler = _adjust_bbox_handler_d .get (format )
54- if adjust_bbox_handler is not None :
55- adjust_bbox_handler (fig , bbox_inches )
56- return restore_bbox
53+ if fixed_dpi is not None :
54+ tr = Affine2D ().scale (fixed_dpi )
55+ dpi_scale = fixed_dpi / fig .dpi
5756 else :
58- warnings .warn ("bbox_inches option for %s backend is not "
59- "implemented yet." % (format ))
60- return None
61-
62-
63- def adjust_bbox_png (fig , bbox_inches ):
64- """
65- adjust_bbox for png (Agg) format
66- """
67-
68- tr = fig .dpi_scale_trans
69-
70- _bbox = TransformedBbox (bbox_inches ,
71- tr )
72- x0 , y0 = _bbox .x0 , _bbox .y0
73- fig .bbox_inches = Bbox .from_bounds (0 , 0 ,
74- bbox_inches .width ,
75- bbox_inches .height )
76-
77- x0 , y0 = _bbox .x0 , _bbox .y0
78- w1 , h1 = fig .bbox .width , fig .bbox .height
79- fig .transFigure ._boxout = Bbox .from_bounds (- x0 , - y0 ,
80- w1 , h1 )
81- fig .transFigure .invalidate ()
82-
83- fig .bbox = TransformedBbox (fig .bbox_inches , tr )
84-
85- fig .patch .set_bounds (x0 / w1 , y0 / h1 ,
86- fig .bbox .width / w1 , fig .bbox .height / h1 )
87-
88-
89- def adjust_bbox_pdf (fig , bbox_inches ):
90- """
91- adjust_bbox for pdf & eps format
92- """
93-
94- if fig ._cachedRenderer .__class__ .__name__ == "RendererPgf" :
9557 tr = Affine2D ().scale (fig .dpi )
96- f = 1.
97- else :
98- tr = Affine2D ().scale (72 )
99- f = 72. / fig .dpi
58+ dpi_scale = 1.
10059
10160 _bbox = TransformedBbox (bbox_inches , tr )
10261
10362 fig .bbox_inches = Bbox .from_bounds (0 , 0 ,
104- bbox_inches .width ,
105- bbox_inches .height )
63+ bbox_inches .width , bbox_inches .height )
10664 x0 , y0 = _bbox .x0 , _bbox .y0
107- w1 , h1 = fig .bbox .width * f , fig .bbox .height * f
108- fig .transFigure ._boxout = Bbox .from_bounds (- x0 , - y0 ,
109- w1 , h1 )
65+ w1 , h1 = fig .bbox .width * dpi_scale , fig .bbox .height * dpi_scale
66+ fig .transFigure ._boxout = Bbox .from_bounds (- x0 , - y0 , w1 , h1 )
11067 fig .transFigure .invalidate ()
11168
11269 fig .bbox = TransformedBbox (fig .bbox_inches , tr )
11370
11471 fig .patch .set_bounds (x0 / w1 , y0 / h1 ,
11572 fig .bbox .width / w1 , fig .bbox .height / h1 )
11673
74+ return restore_bbox
11775
118- def process_figure_for_rasterizing (figure ,
119- bbox_inches_restore , mode ):
12076
77+ def process_figure_for_rasterizing (fig , bbox_inches_restore , fixed_dpi = None ):
12178 """
12279 This need to be called when figure dpi changes during the drawing
12380 (e.g., rasterizing). It recovers the bbox and re-adjust it with
@@ -126,14 +83,6 @@ def process_figure_for_rasterizing(figure,
12683
12784 bbox_inches , restore_bbox = bbox_inches_restore
12885 restore_bbox ()
129- r = adjust_bbox (figure , mode ,
130- bbox_inches )
86+ r = adjust_bbox (figure , bbox_inches , fixed_dpi )
13187
13288 return bbox_inches , r
133-
134-
135- _adjust_bbox_handler_d = {}
136- for format in ["png" , "raw" , "rgba" , "jpg" , "jpeg" , "tiff" ]:
137- _adjust_bbox_handler_d [format ] = adjust_bbox_png
138- for format in ["pgf" , "pdf" , "eps" , "svg" , "svgz" ]:
139- _adjust_bbox_handler_d [format ] = adjust_bbox_pdf
0 commit comments