Skip to content

Commit f18bd14

Browse files
committed
ENH: implement get_tightbbox on Axis3D
1 parent 5451b25 commit f18bd14

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ def set_aspect(self, aspect, adjustable=None, anchor=None, share=False):
279279
ratio
280280
'auto' automatic; fill the position rectangle with data.
281281
This will let the ratio between the size of the
282-
(pseudo-) bounding box be free and will not adjust
283-
anything at draw time.
282+
(pseudo-) bounding box be free and will not
283+
adjust anything at draw time.
284284
======== =================================================
285285
286286
adjustable : None or {'box', 'datalim'}, optional
@@ -2861,6 +2861,27 @@ def permutation_matrices(n):
28612861

28622862
return polygons
28632863

2864+
def get_tightbbox(self, renderer, call_axes_locator=True,
2865+
bbox_extra_artists=None, *, for_layout_only=False):
2866+
ret = super().get_tightbbox(renderer,
2867+
call_axes_locator=call_axes_locator,
2868+
bbox_extra_artists=bbox_extra_artists,
2869+
for_layout_only=for_layout_only)
2870+
batch = [ret]
2871+
if self._axis3don:
2872+
for axis in self._get_axis_list():
2873+
if axis.get_visible():
2874+
try:
2875+
axis_bb = axis.get_tightbbox(
2876+
renderer,
2877+
for_layout_only=for_layout_only
2878+
)
2879+
except TypeError:
2880+
# in case downstream library has redefined axis:
2881+
axis_bb = axis.get_tightbbox(renderer)
2882+
if axis_bb:
2883+
batch.append(axis_bb)
2884+
return mtransforms.Bbox.union(batch)
28642885

28652886
docstring.interpd.update(Axes3D=artist.kwdoc(Axes3D))
28662887
docstring.dedent_interpd(Axes3D.__init__)

lib/mpl_toolkits/mplot3d/axis3d.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Parts rewritten by Reinier Heeres <reinier@heeres.eu>
44

55
import numpy as np
6-
6+
import matplotlib.transforms as mtransforms
77
from matplotlib import (
88
artist, lines as mlines, axis as maxis, patches as mpatches, rcParams)
99
from . import art3d, proj3d
@@ -398,12 +398,30 @@ def draw(self, renderer):
398398
renderer.close_group('axis3d')
399399
self.stale = False
400400

401-
# TODO: Get this to work properly when mplot3d supports
402-
# the transforms framework.
403-
def get_tightbbox(self, renderer):
404-
# Currently returns None so that Axis.get_tightbbox
405-
# doesn't return junk info.
406-
return None
401+
# TODO: Get this to work (more) properly when mplot3d supports the
402+
# transforms framework.
403+
def get_tightbbox(self, renderer, *, for_layout_only=False):
404+
# inherited docstring
405+
if not self.get_visible():
406+
return
407+
# We have to directly access the internal data structures
408+
# (and hope they are up to date) because at draw time we
409+
# shift the ticks and their labels around in (x, y) space
410+
# based on the projection, the current view port, and their
411+
# position in 3D space. If we extend the transforms framework
412+
# into 3D we would not need to do this different book keeping
413+
# than we do in the normal axis
414+
ticks = [*self.majorTicks, *self.minorTicks]
415+
bb_1, bb_2 = self._get_tick_bboxes(ticks, renderer)
416+
other = []
417+
418+
if self.line.get_visible():
419+
other.append(self.line.get_window_extent(renderer))
420+
if (self.label.get_visible() and not for_layout_only and
421+
self.label.get_text()):
422+
other.append(self.label.get_window_extent(renderer))
423+
424+
return mtransforms.Bbox.union([*bb_1, *bb_2, *other])
407425

408426
@property
409427
def d_interval(self):

0 commit comments

Comments
 (0)