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

Skip to content

Commit f29d2ae

Browse files
committed
FIX: return the actual ax.get_window_extent
1 parent 531bd02 commit f29d2ae

File tree

16 files changed

+644
-374
lines changed

16 files changed

+644
-374
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
`.spines.get_window_extent` now takes into account ticks that are on the
14+
spine.

lib/matplotlib/axes/_base.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -564,18 +564,17 @@ def __setstate__(self, state):
564564

565565
def get_window_extent(self, *args, **kwargs):
566566
"""
567-
get the axes bounding box in display space; *args* and
568-
*kwargs* are empty
569-
"""
570-
bbox = self.bbox
571-
x_pad = 0
572-
if self.axison and self.xaxis.get_visible():
573-
x_pad = self.xaxis.get_tick_padding()
574-
y_pad = 0
575-
if self.axison and self.yaxis.get_visible():
576-
y_pad = self.yaxis.get_tick_padding()
577-
return mtransforms.Bbox([[bbox.x0 - x_pad, bbox.y0 - y_pad],
578-
[bbox.x1 + x_pad, bbox.y1 + y_pad]])
567+
Return the axes bounding box in display space;
568+
*args* and *kwargs* are empty
569+
570+
See Also
571+
--------
572+
.matplotlib.axes.Axes.get_tightbbox
573+
.matplotlib.axis.Axis.get_tightbbox
574+
.spines.get_window_extent
575+
576+
"""
577+
return self.bbox
579578

580579
def _init_axis(self):
581580
"move this out of __init__ because non-separable axes don't use it"
@@ -4322,13 +4321,14 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
43224321
else:
43234322
self.apply_aspect()
43244323

4325-
bb_xaxis = self.xaxis.get_tightbbox(renderer)
4326-
if bb_xaxis:
4327-
bb.append(bb_xaxis)
4324+
if self.axison:
4325+
bb_xaxis = self.xaxis.get_tightbbox(renderer)
4326+
if bb_xaxis:
4327+
bb.append(bb_xaxis)
43284328

4329-
bb_yaxis = self.yaxis.get_tightbbox(renderer)
4330-
if bb_yaxis:
4331-
bb.append(bb_yaxis)
4329+
bb_yaxis = self.yaxis.get_tightbbox(renderer)
4330+
if bb_yaxis:
4331+
bb.append(bb_yaxis)
43324332

43334333
self._update_title_position(renderer)
43344334
if self.title.get_visible():

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
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)