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

Skip to content

Commit 2e29d00

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 8a8dd90 commit 2e29d00

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.new_horizontal("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
@@ -266,14 +266,23 @@ def append_size(self, position, size):
266266
position=position)
267267

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

278287

279288
class AxesLocator:
@@ -733,15 +742,20 @@ def make_axes_locatable(axes):
733742
return divider
734743

735744

736-
def make_axes_area_auto_adjustable(ax,
737-
use_axes=None, pad=0.1,
738-
adjust_dirs=None):
745+
def make_axes_area_auto_adjustable(
746+
ax, use_axes=None, pad=0.1, adjust_dirs=None):
747+
"""
748+
Add auto-adjustable padding around *ax* to take its decorations (title,
749+
labels, ticks, ticklabels) into account during layout, using
750+
`Divider.add_auto_adjustable_area`.
751+
752+
By default, the decorations of *ax* are taken into account, but the
753+
decorations of other artists can be used instead using *use_axes*.
754+
"""
739755
if adjust_dirs is None:
740756
adjust_dirs = ["left", "right", "bottom", "top"]
741757
divider = make_axes_locatable(ax)
742-
743758
if use_axes is None:
744759
use_axes = ax
745-
746760
divider.add_auto_adjustable_area(use_axes=use_axes, pad=pad,
747761
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
@@ -494,3 +495,16 @@ def test_grid_axes_position(direction):
494495
assert loc[1]._nx > loc[0]._nx and loc[2]._ny < loc[0]._ny
495496
assert loc[0]._nx == loc[2]._nx and loc[0]._ny == loc[1]._ny
496497
assert loc[3]._nx == loc[1]._nx and loc[3]._ny == loc[2]._ny
498+
499+
500+
def test_auto_adjustable():
501+
fig = plt.figure()
502+
ax = fig.add_axes([0, 0, 1, 1])
503+
pad = 0.1
504+
make_axes_area_auto_adjustable(ax, pad=pad)
505+
fig.canvas.draw()
506+
tbb = ax.get_tightbbox(fig._cachedRenderer)
507+
assert tbb.x0 == pytest.approx(pad * fig.dpi)
508+
assert tbb.x1 == pytest.approx(fig.bbox.width - pad * fig.dpi)
509+
assert tbb.y0 == pytest.approx(pad * fig.dpi)
510+
assert tbb.y1 == pytest.approx(fig.bbox.height - pad * fig.dpi)

0 commit comments

Comments
 (0)