10000 Document, test, and simplify impl. of auto_adjustable_area. · matplotlib/matplotlib@363c7e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 363c7e5

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 a08e334 commit 363c7e5

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,8 +11,8 @@
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])
1818
ax.set_yticklabels(["very long label"])
@@ -21,10 +21,9 @@
2121

2222
###############################################################################
2323

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

2928
ax1.set_yticks([0.5])
3029
ax1.set_yticklabels(["very long label"])
@@ -39,7 +38,7 @@
3938

4039

4140
fig = plt.figure()
42-
ax1 = plt.axes([0, 0, 1, 1])
41+
ax1 = fig.add_axes([0, 0, 1, 1])
4342
divider = make_axes_locatable(ax1)
4443

4544
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
@@ -262,14 +262,23 @@ def append_size(self, position, size):
262262
position=position)
263263

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

274283

275284
class AxesLocator:
@@ -719,15 +728,20 @@ def make_axes_locatable(axes):
719728
return divider
720729

721730

722-
def make_axes_area_auto_adjustable(ax,
723-
use_axes=None, pad=0.1,
724-
adjust_dirs=None):
731+
def make_axes_area_auto_adjustable(
732+
ax, use_axes=None, pad=0.1, adjust_dirs=None):
733+
"""
734+
Add auto-adjustable padding around *ax* to take its decorations (title,
735+
labels, ticks, ticklabels) into account during layout, using
736+
`Divider.add_auto_adjustable_area`.
737+
738+
By default, the decorations of *ax* are taken into account, but the
739+
decorations of other artists can be used instead using *use_axes*.
740+
"""
725741
if adjust_dirs is None:
726742
adjust_dirs = ["left", "right", "bottom", "top"]
727743
divider = make_axes_locatable(ax)
728-
729744
if use_axes is None:
730745
use_axes = ax
731-
732746
divider.add_auto_adjustable_area(use_axes=use_axes, pad=pad,
733747
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")
254< F438 /code>257
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
@@ -14,7 +14,8 @@
1414
axes_size as Size, host_subplot, make_axes_locatable, AxesGrid, ImageGrid)
1515
from mpl_toolkits.axes_grid1.anchored_artists import (
1616
AnchoredSizeBar, AnchoredDirectionArrows)
17-
from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider
17+
from mpl_toolkits.axes_grid1.axes_divider import (
18+
HBoxDivider, make_axes_area_auto_adjustable)
1819
from mpl_toolkits.axes_grid1.inset_locator import (
1920
zoomed_inset_axes, mark_inset, inset_axes, BboxConnectorPatch)
2021
import mpl_toolkits.axes_grid1.mpl_axes
@@ -470,3 +471,16 @@ def test_axes_class_tuple():
470471
fig = plt.figure()
471472
axes_class = (mpl_toolkits.axes_grid1.mpl_axes.Axes, {})
472473
gr = AxesGrid(fig, 111, nrows_ncols=(1, 1), axes_class=axes_class)
474+
475+
476+
def test_auto_adjustable():
477+
fig = plt.figure()
478+
ax = fig.add_axes([0, 0, 1, 1])
479+
pad = 0.1
480+
make_axes_area_auto_adjustable(ax, pad=pad)
481+
fig.canvas.draw()
482+
tbb = ax.get_tightbbox(fig._cachedRenderer)
483+
assert tbb.x0 == pytest.approx(pad * fig.dpi)
484+
assert tbb.x1 == pytest.approx(fig.bbox.width - pad * fig.dpi)
485+
assert tbb.y0 == pytest.approx(pad * fig.dpi)
486+
assert tbb.y1 == pytest.approx(fig.bbox.height - pad * fig.dpi)

0 commit comments

Comments
 (0)
0