Skip to content

Commit 593576e

Browse files
meeseeksmachinetimhoffm
authored andcommitted
Backport PR #12693: Workaround Text3D breaking tight_layout() (#12750)
1 parent 6f8463a commit 593576e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ def draw(self, renderer):
118118
mtext.Text.draw(self, renderer)
119119
self.stale = False
120120

121+
def get_tightbbox(self, renderer):
122+
# Overwriting the 2d Text behavior which is not valid for 3d.
123+
# For now, just return None to exclude from layout calculation.
124+
return None
125+
121126

122127
def text_2d_to_3d(obj, z=0, zdir='z'):
123128
"""Convert a Text to a Text3D object."""

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from mpl_toolkits.mplot3d import Axes3D, axes3d, proj3d, art3d
44
from matplotlib import cm
5-
from matplotlib.testing.decorators import image_comparison
5+
from matplotlib.testing.decorators import image_comparison, check_figures_equal
66
from matplotlib.collections import LineCollection
77
from matplotlib.patches import Circle
88
import matplotlib.pyplot as plt
@@ -155,6 +155,19 @@ def f(t):
155155
ax.set_zlim3d(-1, 1)
156156

157157

158+
@check_figures_equal(extensions=['png'])
159+
def test_tight_layout_text(fig_test, fig_ref):
160+
# text is currently ignored in tight layout. So the order of text() and
161+
# tight_layout() calls should not influence the result.
162+
ax1 = fig_test.gca(projection='3d')
163+
ax1.text(.5, .5, .5, s='some string')
164+
fig_test.tight_layout()
165+
166+
ax2 = fig_ref.gca(projection='3d')
167+
fig_ref.tight_layout()
168+
ax2.text(.5, .5, .5, s='some string')
169+
170+
158171
@image_comparison(baseline_images=['scatter3d'], remove_text=True)
159172
def test_scatter3d():
160173
fig = plt.figure()

0 commit comments

Comments
 (0)