10000 FIX: return the actual ax.get_window_extent · matplotlib/matplotlib@c7d2c1c · GitHub
[go: up one dir, main page]

Skip to content

Commit c7d2c1c

Browse files
committed
FIX: return the actual ax.get_window_extent
1 parent 977f415 commit c7d2c1c

File tree

16 files changed

+659
-374
lines changed

16 files changed

+659
-374
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
get_window_extents changes:
2+
---------------------------
3+
4+
`.matplotlib.axes.Axes.get_window_extent` used to return a bounding box
5+
that was slightly larger than the axes, presumably to take into account
6+
the ticks that may be on a spine. However, it was not scaling the tick sizes
7+
according to the dpi of the canvas, and it did not check if the ticks were
8+
visible, or on the spine.
9+
10+
Now `.matplotlib.axes.Axes.get_window_extent` just returns the axes extent
11+
with no padding for ticks.
12+
13+
This affects `.matplotlib.axes.Axes.get_tightbbox` in cases where there are
14+
outward ticks with no tick labels, and it also removes the (small) pad around
15+
axes in that case.
16+
17+
`.spines.get_window_extent` now takes into account ticks that are on the
18+
spine.

lib/matplotlib/axes/_base.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -576,18 +576,21 @@ def __setstate__(self, state):
576576

577577
def get_window_extent(self, *args, **kwargs):
578578
"""
579-
get the axes bounding box in display space; *args* and
580-
*kwargs* are empty
581-
"""
582-
bbox = self.bbox
583-
x_pad = 0
584-
if self.axison and self.xaxis.get_visible():
585-
x_pad = self.xaxis.get_tick_padding()
586-
y_pad = 0
587-
if self.axison and self.yaxis.get_visible():
588-
y_pad = self.yaxis.get_tick_padding()
589-
return mtransforms.Bbox([[bbox.x0 - x_pad, bbox.y0 - y_pad],
590-
[bbox.x1 + x_pad, bbox.y1 + y_pad]])
579+
Return the axes bounding box in display space; *args* and *kwargs*
580+
are empty.
581+
582+
This bounding box does not include the spines, ticks, ticklables,
583+
or other labels. For a bounding box including these elements use
584+
`~matplotlib.axes.Axes.get_tightbbox`.
585+
586+
See Also
587+
--------
588+
matplotlib.axes.Axes.get_tightbbox
589+
matplotlib.axis.Axis.get_tightbbox
590+
matplotlib.spines.get_window_extent
591+
592+
"""
593+
return self.bbox
591594

592595
def _init_axis(self):
593596
"move this out of __init__ because non-separable axes don't use it"
@@ -4290,6 +4293,13 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
42904293
-------
42914294
bbox : `.BboxBase`
42924295
bounding box in figure pixel coordinates.
4296+
4297+
See Also
4298+
--------
4299+
matplotlib.axis.Axes.get_window_extent
4300+
matplotlib.axis.Axis.get_tightbbox
4301+
matplotlib.spines.get_window_extent
4302+
42934303
"""
42944304

42954305
bb = []
@@ -4304,13 +4314,14 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
43044314
else:
43054315
self.apply_aspect()
43064316

4307-
bb_xaxis = self.xaxis.get_tightbbox(renderer)
4308-
if bb_xaxis:
4309-
bb.append(bb_xaxis)
4317+
if self.axison:
4318+
bb_xaxis = self.xaxis.get_tightbbox(renderer)
4319+
if bb_xaxis:
4320+
bb.append(bb_xaxis)
43104321

4311-
bb_yaxis = self.yaxis.get_tightbbox(renderer)
4312-
if bb_yaxis:
4313-
bb.append(bb_yaxis)
4322+
bb_yaxis = self.yaxis.get_tightbbox(renderer)
4323+
if bb_yaxis:
4324+
bb.append(bb_yaxis)
43144325

43154326
self._update_title_position(renderer)
43164327
bb.append(self.get_window_extent(renderer))

lib/matplotlib/spines.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,60 @@ def get_patch_transform(self):
145145
return super().get_patch_transform()
146146

147147
def get_window_extent(self, renderer=None):
148+
"""
149+
Return the window extent of the spines in display space, including
150+
padding for ticks (but not their labels)
151+
152+
See Also
153+
--------
154+
matplotlib.axes.Axes.get_tightbbox
155+
matplotlib.axes.Axes.get_window_extent
156+
157+
"""
148158
# make sure the location is updated so that transforms etc are
149159
# correct:
150160
self._adjust_location()
151-
return super().get_window_extent(renderer=renderer)
161+
bb = super().get_window_extent(renderer=renderer)
162+
bboxes = [bb]
163+
tickstocheck = [self.axis.majorTicks[0]]
164+
if len(self.axis.minorTicks) > 1:
165+
# only pad for minor ticks if there are more than one
166+
# of them. There is always one...
167+
tickstocheck.append(self.axis.minorTicks[1])
168+
for tick in tickstocheck:
169+
bb0 = bb.frozen()
170+
tickl = tick._size
171+
tickdir = tick._tickdir
172+
if tickdir == 'out':
173+
padout = 1
174+
padin = 0
175+
elif tickdir == 'in':
176+
padout = 0
177+
padin = 1
178+
else:
179+
padout = 0.5
180+
padin = 0.5
181+
padout = padout * tickl / 72 * self.figure.dpi
182+
padin = padin * tickl / 72 * self.figure.dpi
183+
184+
if tick.tick1line.get_visible():
185+
if self.spine_type in ['left']:
186+
bb0.x0 = bb0.x0 - padout
187+
bb0.x1 = bb0.x1 + padin
188+
elif self.spine_type in ['bottom']:
189+
bb0.y0 = bb0.y0 - padout
190+
bb0.y1 = bb0.y1 + padin
191+
192+
if tick.tick2line.get_visible():
193+
if self.spine_type in ['right']:
194+
bb0.x1 = bb0.x1 + padout
195+
bb0.x0 = bb0.x0 - padin
196+
elif self.spine_type in ['top']:
197+
bb0.y1 = bb0.y1 + padout
198+
bb0.y0 = bb0.y0 - padout
199+
bboxes.append(bb0)
200+
201+
return mtransforms.Bbox.union(bboxes)
152202

153203
def get_path(self):
154204
return self._path
Loading
Binary file not shown.
Loading
Loading
Loading
Loading
Loading
Loading
Binary file not shown.
Loading

0 commit comments

Comments
 (0)
0