8000 Merge pull request #20071 from anntzer/aaa · matplotlib/matplotlib@826e685 · GitHub
[go: up one dir, main page]

Skip to content

Commit 826e685

Browse files
authored
Merge pull request #20071 from anntzer/aaa
Document, test, and simplify impl. of auto_adjustable_area.
2 parents 9ab5aad + eb12b02 commit 826e685

File tree

5 files changed

+78
-17
lines changed

5 files changed

+78
-17
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: 3 additions & 6 deletions

lib/mpl_toolkits/axes_grid1/axes_divider.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"""
2-
===================================
3-
Make Room For Ylabel Using Axesgrid
4-
===================================
5-
2+
====================================
3+
Make room for ylabel using axes_grid
4+
====================================
65
"""
76

87
import matplotlib.pyplot as plt
@@ -20,7 +19,6 @@
2019

2120
###############################################################################
2221

23-
2422
fig = plt.figure()
2523
ax1 = fig.add_axes([0, 0, 1, 0.5])
2624
ax2 = fig.add_axes([0, 0.5, 1, 0.5])
@@ -35,7 +33,6 @@
3533

3634
###############################################################################
3735

38-
3936
fig = plt.figure()
4037
ax1 = fig.add_axes([0, 0, 1, 1])
4138
divider = make_axes_locatable(ax1)
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 *use_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 where 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, padding is determined from the decorations of *ax*.
713+
Pass *use_axes* to consider the decorations of other Axes instead.
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ def get_size(self, renderer):
204204
return rel_size, abs_size
205205

206206

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

240241

242+
@_api.deprecated("3.6")
241243
class SizeFromFunc(_Base):
242244
def __init__(self, func):
243245
self._func = func
@@ -252,6 +254,7 @@ def get_size(self, renderer):
252254
return rel_size, abs_size
253255

254256

257+
@_api.deprecated("3.6")
255258
class GetExtentHelper:
256259
_get_func_map = {
257260
"left": lambda self, axes_bbox: axes_bbox.xmin - self.xmin,
@@ -271,3 +274,31 @@ def __call__(self, renderer):
271274
ax.bbox)
272275
for ax in self._ax_list]
273276
return max(vl)
277+
278+
279+
class _AxesDecorationsSize(_Base):
280+
"""
281+
Fixed size, corresponding to the size of decorations on a given Axes side.
282+
"""
283+
284+
_get_size_map = {
285+
"left": lambda tight_bb, axes_bb: axes_bb.xmin - tight_bb.xmin,
286+
"right": lambda tight_bb, axes_bb: tight_bb.xmax - axes_bb.xmax,
287+
"bottom": lambda tight_bb, axes_bb: axes_bb.ymin - tight_bb.ymin,
288+
"top": lambda tight_bb, axes_bb: tight_bb.ymax - axes_bb.ymax,
289+
}
290+
291+
def __init__(self, ax, direction):
292+
self._get_size = _api.check_getitem(
293+
self._get_size_map, direction=direction)
294+
self._ax_list = [ax] if isinstance(ax, Axes) else ax
295+
296+
def get_size(self, renderer):
297+
sz = max([
298+
self._get_size(ax.get_tightbbox(renderer, call_axes_locator=False),
299+
ax.bbox)
300+
for ax in self._ax_list])
301+
dpi = renderer.points_to_pixels(72)
302+
abs_size = sz / dpi
303+
rel_size = 0
304+
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)
0