|
| 1 | +""" |
| 2 | +Helper module for the *bbox_inches* parameter in `.Figure.savefig`. |
| 3 | +""" |
| 4 | + |
| 5 | +frommatplotlib.transformsimportBbox,TransformedBbox,Affine2D |
| 6 | + |
| 7 | + |
| 8 | +defadjust_bbox(fig,bbox_inches,fixed_dpi=None): |
| 9 | +""" |
| 10 | + Temporarily adjust the figure so that only the specified area |
| 11 | + (bbox_inches) is saved. |
| 12 | +
|
| 13 | + It modifies fig.bbox, fig.bbox_inches, |
| 14 | + fig.transFigure._boxout, and fig.patch. While the figure size |
| 15 | + changes, the scale of the original figure is conserved. A |
| 16 | + function which restores the original values are returned. |
| 17 | + """ |
| 18 | +origBbox=fig.bbox |
| 19 | +origBboxInches=fig.bbox_inches |
| 20 | +orig_tight_layout=fig.get_tight_layout() |
| 21 | +_boxout=fig.transFigure._boxout |
| 22 | + |
| 23 | +fig.set_tight_layout(False) |
| 24 | + |
| 25 | +old_aspect= [] |
| 26 | +locator_list= [] |
| 27 | +sentinel=object() |
| 28 | +foraxinfig.axes: |
| 29 | +locator_list.append(ax.get_axes_locator()) |
| 30 | +current_pos=ax.get_position(original=False).frozen() |
| 31 | +ax.set_axes_locator(lambdaa,r,_pos=current_pos:_pos) |
| 32 | +# override the method that enforces the aspect ratio on the Axes |
| 33 | +if'apply_aspect'inax.__dict__: |
| 34 | +old_aspect.append(ax.apply_aspect) |
| 35 | +else: |
| 36 | +old_aspect.append(sentinel) |
| 37 | +ax.apply_aspect=lambdapos=None:None |
| 38 | + |
| 39 | +defrestore_bbox(): |
| 40 | +forax,loc,aspectinzip(fig.axes,locator_list,old_aspect): |
| 41 | +ax.set_axes_locator(loc) |
| 42 | +ifaspectissentinel: |
| 43 | +# delete our no-op function which un-hides the original method |
| 44 | +delax.apply_aspect |
| 45 | +else: |
| 46 | +ax.apply_aspect=aspect |
| 47 | + |
| 48 | +fig.bbox=origBbox |
| 49 | +fig.bbox_inches=origBboxInches |
| 50 | +fig.set_tight_layout(orig_tight_layout) |
| 51 | +fig.transFigure._boxout=_boxout |
| 52 | +fig.transFigure.invalidate() |
| 53 | +fig.patch.set_bounds(0,0,1,1) |
| 54 | + |
| 55 | +iffixed_dpiisNone: |
| 56 | +fixed_dpi=fig.dpi |
| 57 | +tr=Affine2D().scale(fixed_dpi) |
| 58 | +dpi_scale=fixed_dpi/fig.dpi |
| 59 | + |
| 60 | +fig.bbox_inches=Bbox.from_bounds(0,0,*bbox_inches.size) |
| 61 | +x0,y0=tr.transform(bbox_inches.p0) |
| 62 | +w1,h1=fig.bbox.size*dpi_scale |
| 63 | +fig.transFigure._boxout=Bbox.from_bounds(-x0,-y0,w1,h1) |
| 64 | +fig.transFigure.invalidate() |
| 65 | + |
| 66 | +fig.bbox=TransformedBbox(fig.bbox_inches,tr) |
| 67 | + |
| 68 | +fig.patch.set_bounds(x0/w1,y0/h1, |
| 69 | +fig.bbox.width/w1,fig.bbox.height/h1) |
| 70 | + |
| 71 | +returnrestore_bbox |
| 72 | + |
| 73 | + |
| 74 | +defprocess_figure_for_rasterizing(fig,bbox_inches_restore,fixed_dpi=None): |
| 75 | +""" |
| 76 | + A function that needs to be called when figure dpi changes during the |
| 77 | + drawing (e.g., rasterizing). It recovers the bbox and re-adjust it with |
| 78 | + the new dpi. |
| 79 | + """ |
| 80 | + |
| 81 | +bbox_inches,restore_bbox=bbox_inches_restore |
| 82 | +restore_bbox() |
| 83 | +r=adjust_bbox(fig,bbox_inches,fixed_dpi) |
| 84 | + |
| 85 | +returnbbox_inches,r |