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

Skip to content

Commit 792ba77

Browse files
authored
Merge pull request #18772 from brunobeltran/annotation-visibility
FIX: clipped text should not contribute to tightbbox
2 parents 8f7c391 + 0d1f343 commit 792ba77

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Annotations with ``annotation_clip`` no longer affect ``tight_layout``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Previously, `.text.Annotation.get_tightbbox` always returned the full
4+
`.text.Annotation.get_window_extent` of the object, independent of the value
5+
of ``annotation_clip``. `.text.Annotation.get_tightbbox` now correctly takes
6+
this extra clipping box into account, meaning that `~.text.Annotation`\s that
7+
are not drawn because of ``annotation_clip`` will not count towards the axes
8+
bounding box calculations, such as those done by `~.pyplot.tight_layout`.
9+
10+
This is now consistent with the API described in `~.artist.Artist`, which
11+
specifies that ``get_window_extent`` should return the full extents and
12+
``get_tightbbox`` should "account for any clipping".

lib/matplotlib/tests/test_tightlayout.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,16 @@ def test_badsubplotgrid():
288288

289289

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

297-
ax.annotate('BIG LONG STRING', xy=(1.25, 2), xytext=(10.5, 1.75),)
299+
ax.annotate('BIG LONG STRING', xy=(1.25, 2), xytext=(10.5, 1.75),
300+
annotation_clip=False)
298301
p1 = ax.get_position()
299302
with pytest.warns(UserWarning):
300303
plt.tight_layout()

lib/matplotlib/text.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1958,7 +1958,7 @@ def get_window_extent(self, renderer=None):
19581958
"""
19591959
# This block is the same as in Text.get_window_extent, but we need to
19601960
# set the renderer before calling update_positions().
1961-
if not self.get_visible():
1961+
if not self.get_visible() or not self._check_xy(renderer):
19621962
return Bbox.unit()
19631963
if renderer is not None:
19641964
self._renderer = renderer
@@ -1977,5 +1977,11 @@ def get_window_extent(self, renderer=None):
19771977

19781978
return Bbox.union(bboxes)
19791979

1980+
def get_tightbbox(self, renderer):
1981+
# docstring inherited
1982+
if not self._check_xy(renderer):
1983+
return Bbox.null()
1984+
return super().get_tightbbox(renderer)
1985+
19801986

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

0 commit comments

Comments
 (0)