Skip to content

Hint at draw_without_rendering() in Text.get_window_extent #22122

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 1 commit into from
Jan 6, 2022
Merged
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
9 changes: 6 additions & 3 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,8 +882,9 @@ def get_window_extent(self, renderer=None, dpi=None):
A renderer is needed to compute the bounding box. If the artist
has already been drawn, the renderer is cached; thus, it is only
necessary to pass this argument when calling `get_window_extent`
before the first `draw`. In practice, it is usually easier to
trigger a draw first (e.g. by saving the figure).
before the first draw. In practice, it is usually easier to
Copy link
Member Author

Choose a reason for hiding this comment

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

The draw link would go to Text.draw which is more or less an internal API and not helpful here (at worst the user might try and call that). It's better to not link to anything here.

trigger a draw first, e.g. by calling
`~.Figure.draw_without_rendering` or ``plt.show()``.
Comment on lines +886 to +887
Copy link
Member Author

Choose a reason for hiding this comment

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

Saving is usually not a good way to trigger a draw in this case. You'll likely want to modify the figure somehow based on the extent.

Copy link
Member

Choose a reason for hiding this comment

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

I'm 50/50 about including plt.show() as

plt.ion()
for j in range(5):
    fig, ax = plt.subplots()
    plt.show()
    sleep(5)

will not spin the GUI event loop (and hence in some cases not get the actual render to happen) until after the loop is done.

Copy link
Member

Choose a reason for hiding this comment

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

OK, but 99.999% of cases, a draw will occur!


dpi : float, optional
The dpi value for computing the bbox, defaults to
Expand All @@ -904,7 +905,9 @@ def get_window_extent(self, renderer=None, dpi=None):
if self._renderer is None:
self._renderer = self.figure._cachedRenderer
if self._renderer is None:
raise RuntimeError('Cannot get window extent w/o renderer')
raise RuntimeError(
"Cannot get window extent of text w/o renderer. You likely "
"want to call 'figure.draw_without_rendering()' first.")

with cbook._setattr_cm(self.figure, dpi=dpi):
bbox, info, descent = self._get_layout(self._renderer)
Expand Down