Skip to content

BF: text not drawn shouldn't count for tightbbox #18772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/api/next_api_changes/behavior/18772-BGB.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Annotations with ``annotation_clip`` no longer affect ``tight_layout``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Previously, `.text.Annotation.get_tightbbox` always returned the full
`.text.Annotation.get_window_extent` of the object, independent of the value
of ``annotation_clip``. `.text.Annotation.get_tightbbox` now correctly takes
this extra clipping box into account, meaning that `~.text.Annotation`\s that
are not drawn because of ``annotation_clip`` will not count towards the axes
bounding box calculations, such as those done by `~.pyplot.tight_layout`.

This is now consistent with the API described in `~.artist.Artist`, which
specifies that ``get_window_extent`` should return the full extents and
``get_tightbbox`` should "account for any clipping".
9 changes: 6 additions & 3 deletions lib/matplotlib/tests/test_tightlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,16 @@ def test_badsubplotgrid():


def test_collapsed():
# test that if a call to tight_layout will collapses the axes that
# it does not get applied:
# test that if the amount of space required to make all the axes
# decorations fit would mean that the actual Axes would end up with size
# zero (i.e. margins add up to more than the available width) that a call
# to tight_layout will not get applied:
fig, ax = plt.subplots(tight_layout=True)
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])

ax.annotate('BIG LONG STRING', xy=(1.25, 2), xytext=(10.5, 1.75),)
ax.annotate('BIG LONG STRING', xy=(1.25, 2), xytext=(10.5, 1.75),
annotation_clip=False)
Comment on lines +299 to +300
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was actually "buggy" as originally written. Because of the value of xy, with annotation_clip=None, this text never got drawn, and as such should never have contributed to the tight_layout in the first place. The code fixed this, causing this test to fail (since it no longer tested what it was intended to).

Adding annotation_clip=False fixes the test to check for what it originally intended (by actually drawing the text way off screen).

p1 = ax.get_position()
with pytest.warns(UserWarning):
plt.tight_layout()
Expand Down
8 changes: 7 additions & 1 deletion lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1958,7 +1958,7 @@ def get_window_extent(self, renderer=None):
"""
# This block is the same as in Text.get_window_extent, but we need to
# set the renderer before calling update_positions().
if not self.get_visible():
if not self.get_visible() or not self._check_xy(renderer):
return Bbox.unit()
if renderer is not None:
self._renderer = renderer
Expand All @@ -1977,5 +1977,11 @@ def get_window_extent(self, renderer=None):

return Bbox.union(bboxes)

def get_tightbbox(self, renderer):
# docstring inherited
if not self._check_xy(renderer):
return Bbox.null()
return super().get_tightbbox(renderer)


docstring.interpd.update(Annotation=Annotation.__init__.__doc__)