From 8011ba21683987a70dff597d819733785771aa8c Mon Sep 17 00:00:00 2001 From: Benjamin Root Date: Mon, 5 Nov 2018 12:36:56 -0500 Subject: [PATCH] Backport PR #12693: Workaround Text3D breaking tight_layout() --- lib/mpl_toolkits/mplot3d/art3d.py | 5 +++++ lib/mpl_toolkits/tests/test_mplot3d.py | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/mpl_toolkits/mplot3d/art3d.py b/lib/mpl_toolkits/mplot3d/art3d.py index 862d04788bc3..7ce3925836a7 100644 --- a/lib/mpl_toolkits/mplot3d/art3d.py +++ b/lib/mpl_toolkits/mplot3d/art3d.py @@ -118,6 +118,11 @@ def draw(self, renderer): mtext.Text.draw(self, renderer) self.stale = False + def get_tightbbox(self, renderer): + # Overwriting the 2d Text behavior which is not valid for 3d. + # For now, just return None to exclude from layout calculation. + return None + def text_2d_to_3d(obj, z=0, zdir='z'): """Convert a Text to a Text3D object.""" diff --git a/lib/mpl_toolkits/tests/test_mplot3d.py b/lib/mpl_toolkits/tests/test_mplot3d.py index 8d2815408dc3..bc9c43ec7b12 100644 --- a/lib/mpl_toolkits/tests/test_mplot3d.py +++ b/lib/mpl_toolkits/tests/test_mplot3d.py @@ -2,7 +2,7 @@ from mpl_toolkits.mplot3d import Axes3D, axes3d, proj3d, art3d from matplotlib import cm -from matplotlib.testing.decorators import image_comparison +from matplotlib.testing.decorators import image_comparison, check_figures_equal from matplotlib.collections import LineCollection from matplotlib.patches import Circle import matplotlib.pyplot as plt @@ -155,6 +155,19 @@ def f(t): ax.set_zlim3d(-1, 1) +@check_figures_equal(extensions=['png']) +def test_tight_layout_text(fig_test, fig_ref): + # text is currently ignored in tight layout. So the order of text() and + # tight_layout() calls should not influence the result. + ax1 = fig_test.gca(projection='3d') + ax1.text(.5, .5, .5, s='some string') + fig_test.tight_layout() + + ax2 = fig_ref.gca(projection='3d') + fig_ref.tight_layout() + ax2.text(.5, .5, .5, s='some string') + + @image_comparison(baseline_images=['scatter3d'], remove_text=True) def test_scatter3d(): fig = plt.figure()