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

Skip to content

Commit db96106

Browse files
authored
Merge pull request #22134 from oscargus/deprecate_tight
Deprecated `tight_bbox` and `tight_layout` modules
2 parents 5de27aa + 2d8bd62 commit db96106

File tree

13 files changed

+469
-454
lines changed

13 files changed

+469
-454
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Modules ``tight_bbox`` and ``tight_layout`` deprecated
2+
------------------------------------------------------
3+
4+
The modules ``matplotlib.tight_bbox`` and ``matplotlib.tight_layout`` are
5+
considered internal and public access is deprecated.

doc/api/prev_api_changes/api_changes_1.4.x.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ original location:
149149
``drawRect`` from ``FigureCanvasQTAgg``; they were always an
150150
implementation detail of the (preserved) ``drawRectangle()`` function.
151151

152-
* The function signatures of `.tight_bbox.adjust_bbox` and
153-
`.tight_bbox.process_figure_for_rasterizing` have been changed. A new
154-
*fixed_dpi* parameter allows for overriding the ``figure.dpi`` setting
152+
* The function signatures of ``matplotlib.tight_bbox.adjust_bbox`` and
153+
``matplotlib.tight_bbox.process_figure_for_rasterizing`` have been changed.
154+
A new *fixed_dpi* parameter allows for overriding the ``figure.dpi`` setting
155155
instead of trying to deduce the intended behaviour from the file format.
156156

157157
* Added support for horizontal/vertical axes padding to

doc/api/prev_api_changes/api_changes_2.2.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ If `.MovieWriterRegistry` can't find the requested `.MovieWriter`, a
159159
more helpful `RuntimeError` message is now raised instead of the
160160
previously raised `KeyError`.
161161

162-
`~.tight_layout.auto_adjust_subplotpars` now raises `ValueError`
162+
``matplotlib.tight_layout.auto_adjust_subplotpars`` now raises `ValueError`
163163
instead of `RuntimeError` when sizes of input lists don't match
164164

165165

doc/api/prev_api_changes/api_changes_3.0.1.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
API Changes for 3.0.1
22
=====================
33

4-
`.tight_layout.auto_adjust_subplotpars` can return ``None`` now if the new
5-
subplotparams will collapse axes to zero width or height. This prevents
6-
``tight_layout`` from being executed. Similarly
7-
`.tight_layout.get_tight_layout_figure` will return None.
4+
``matplotlib.tight_layout.auto_adjust_subplotpars`` can return ``None`` now if
5+
the new subplotparams will collapse axes to zero width or height.
6+
This prevents ``tight_layout`` from being executed. Similarly
7+
``matplotlib.tight_layout.get_tight_layout_figure`` will return None.
88

99
To improve import (startup) time, private modules are now imported lazily.
1010
These modules are no longer available at these locations:

lib/matplotlib/_tight_bbox.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
Helper module for the *bbox_inches* parameter in `.Figure.savefig`.
3+
"""
4+
5+
from matplotlib.transforms import Bbox, TransformedBbox, Affine2D
6+
7+
8+
def adjust_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+
for ax in fig.axes:
29+
locator_list.append(ax.get_axes_locator())
30+
current_pos = ax.get_position(original=False).frozen()
31+
ax.set_axes_locator(lambda a, r, _pos=current_pos: _pos)
32+
# override the method that enforces the aspect ratio on the Axes
33+
if 'apply_aspect' in ax.__dict__:
34+
old_aspect.append(ax.apply_aspect)
35+
else:
36+
old_aspect.append(sentinel)
37+
ax.apply_aspect = lambda pos=None: None
38+
39+
def restore_bbox():
40+
for ax, loc, aspect in zip(fig.axes, locator_list, old_aspect):
41+
ax.set_axes_locator(loc)
42+
if aspect is sentinel:
43+
# delete our no-op function which un-hides the original method
44+
del ax.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+
if fixed_dpi is None:
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+
return restore_bbox
72+
73+
74+
def process_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+
return bbox_inches, r

0 commit comments

Comments
 (0)