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

Skip to content

Commit ab8d23f

Browse files
committed
Document, test, and simplify impl. of auto_adjustable_area.
Document behavior of auto_adjustable_area, and slightly modernize the example. Simplify its implementation: `Padded` is just size addition and `GetExtentHelper` and `SizeFromFunc` can reasonably be fused into a single class; none of them are used anywhere else, so just deprecate them as public APIs. Add a test.
1 parent 6fe94c3 commit ab8d23f

File tree

5 files changed

+77
-18
lines changed

5 files changed

+77
-18
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``axes_size`` internal helpers
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The following APIs are deprecated: ``axes_size.Padded`` (use ``size + pad``
5+
instead), ``axes_size.SizeFromFunc``, ``axes_size.GetExtentHelper``.

examples/axes_grid1/make_room_for_ylabel_using_axesgrid.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@
1111
from mpl_toolkits.axes_grid1.axes_divider import make_axes_area_auto_adjustable
1212

1313

14-
plt.figure()
15-
ax = plt.axes([0, 0, 1, 1])
14+
fig = plt.figure()
15+
ax = fig.add_axes([0, 0, 1, 1])
1616

1717
ax.set_yticks([0.5], labels=["very long label"])
1818

1919
make_axes_area_auto_adjustable(ax)
2020

2121
###############################################################################
2222

23-
24-
plt.figure()
25-
ax1 = plt.axes([0, 0, 1, 0.5])
26-
ax2 = plt.axes([0, 0.5, 1, 0.5])
23+
fig = plt.figure()
24+
ax1 = fig.add_axes([0, 0, 1, 0.5])
25+
ax2 = fig.add_axes([0, 0.5, 1, 0.5])
2726

2827
ax1.set_yticks([0.5], labels=["very long label"])
2928
ax1.set_ylabel("Y label")
@@ -37,7 +36,7 @@
3736

3837

3938
fig = plt.figure()
40-
ax1 = plt.axes([0, 0, 1, 1])
39+
ax1 = fig.add_axes([0, 0, 1, 1])
4140
divider = make_axes_locatable(ax1)
4241

4342
ax2 = divider.append_axes("right", "100%", pad=0.3, sharey=ax1)

lib/mpl_toolkits/axes_grid1/axes_divider.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,23 @@ def append_size(self, position, size):
265265
position=position)
266266

267267
def add_auto_adjustable_area(self, use_axes, pad=0.1, adjust_dirs=None):
268+
"""
269+
Add auto-adjustable padding around axes to take their decorations
270+
(title, labels, ticks, ticklabels) into account during layout.
271+
272+
Parameters
273+
----------
274+
use_axes : `~.axes.Axes` or list of `~.axes.Axes`
275+
The axes whose decorations are taken into account.
276+
pad : float, optional
277+
Additional padding in inches.
278+
adjust_dirs : list of {"left", "right", "bottom", "top"}, optional
279+
The sides in which padding is added; defaults to all four sides.
280+
"""
268281
if adjust_dirs is None:
269282
adjust_dirs = ["left", "right", "bottom", "top"]
270-
from .axes_size import Padded, SizeFromFunc, GetExtentHelper
271283
for d in adjust_dirs:
272-
helper = GetExtentHelper(use_axes, d)
273-
size = SizeFromFunc(helper)
274-
padded_size = Padded(size, pad) # pad in inch
275-
self.append_size(d, padded_size)
284+
self.append_size(d, Size._AxesDecorationsSize(use_axes, d) + pad)
276285

277286

278287
class AxesLocator:
@@ -693,15 +702,20 @@ def make_axes_locatable(axes):
693702
return divider
694703

695704

696-
def make_axes_area_auto_adjustable(ax,
697-
use_axes=None, pad=0.1,
698-
adjust_dirs=None):
705+
def make_axes_area_auto_adjustable(
706+
ax, use_axes=None, pad=0.1, adjust_dirs=None):
707+
"""
708+
Add auto-adjustable padding around *ax* to take its decorations (title,
709+
labels, ticks, ticklabels) into account during layout, using
710+
`Divider.add_auto_adjustable_area`.
711+
712+
By default, the decorations of *ax* are taken into account, but the
713+
decorations of other artists can be used instead using *use_axes*.
714+
"""
699715
if adjust_dirs is None:
700716
adjust_dirs = ["left", "right", "bottom", "top"]
701717
divider = make_axes_locatable(ax)
702-
703718
if use_axes is None:
704719
use_axes = ax
705-
706720
divider.add_auto_adjustable_area(use_axes=use_axes, pad=pad,
707721
adjust_dirs=adjust_dirs)

lib/mpl_toolkits/axes_grid1/axes_size.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ def get_size(self, renderer):
203203
return rel_size, abs_size
204204

205205

206+
@_api.deprecated("3.5", alternative="size + pad")
206207
class Padded(_Base):
207208
"""
208209
Return a instance where the absolute part of *size* is
@@ -237,6 +238,7 @@ def from_any(size, fraction_ref=None):
237238
raise ValueError("Unknown format")
238239

239240

241+
@_api.deprecated("3.5")
240242
class SizeFromFunc(_Base):
241243
def __init__(self, func):
242244
self._func = func
@@ -251,6 +253,7 @@ def get_size(self, renderer):
251253
return rel_size, abs_size
252254

253255

256+
@_api.deprecated("3.5")
254257
class GetExtentHelper:
255258
_get_func_map = {
256259
"left": lambda self, axes_bbox: axes_bbox.xmin - self.xmin,
@@ -270,3 +273,27 @@ def __call__(self, renderer):
270273
ax.bbox)
271274
for ax in self._ax_list]
272275
return max(vl)
276+
277+
278+
class _AxesDecorationsSize(_Base):
279+
_get_size_map = {
280+
"left": lambda tight_bb, axes_bb: axes_bb.xmin - tight_bb.xmin,
281+
"right": lambda tight_bb, axes_bb: tight_bb.xmax - axes_bb.xmax,
282+
"bottom": lambda tight_bb, axes_bb: axes_bb.ymin - tight_bb.ymin,
283+
"top": lambda tight_bb, axes_bb: tight_bb.ymax - axes_bb.ymax,
284+
}
285+
286+
def __init__(self, ax, direction):
287+
self._get_size = _api.check_getitem(
288+
self._get_size_map, direction=direction)
289+
self._ax_list = [ax] if isinstance(ax, Axes) else ax
290+
291+
def get_size(self, renderer):
292+
sz = max([
293+
self._get_size(ax.get_tightbbox(renderer, call_axes_locator=False),
294+
ax.bbox)
295+
for ax in self._ax_list])
296+
dpi = renderer.points_to_pixels(72)
297+
abs_size = sz / dpi
298+
rel_size = 0
299+
return rel_size, abs_size

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
Grid, AxesGrid, ImageGrid)
1717
from mpl_toolkits.axes_grid1.anchored_artists import (
1818
AnchoredSizeBar, AnchoredDirectionArrows)
19-
from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider
19+
from mpl_toolkits.axes_grid1.axes_divider import (
20+
HBoxDivider, make_axes_area_auto_adjustable)
2021
from mpl_toolkits.axes_grid1.inset_locator import (
2122
zoomed_inset_axes, mark_inset, inset_axes, BboxConnectorPatch)
2223
import mpl_toolkits.axes_grid1.mpl_axes
@@ -510,3 +511,16 @@ def test_mark_inset_unstales_viewlim(fig_test, fig_ref):
510511
mark_inset(full, inset, 1, 4)
511512
# Manually unstale the full's viewLim.
512513
fig_ref.canvas.draw()
514+
515+
516+
def test_auto_adjustable():
517+
fig = plt.figure()
518+
ax = fig.add_axes([0, 0, 1, 1])
519+
pad = 0.1
520+
make_axes_area_auto_adjustable(ax, pad=pad)
521+
fig.canvas.draw()
522+
tbb = ax.get_tightbbox(fig._cachedRenderer)
523+
assert tbb.x0 == pytest.approx(pad * fig.dpi)
524+
assert tbb.x1 == pytest.approx(fig.bbox.width - pad * fig.dpi)
525+
assert tbb.y0 == pytest.approx(pad * fig.dpi)
526+
assert tbb.y1 == pytest.approx(fig.bbox.height - pad * fig.dpi)

0 commit comments

Comments
 (0)