Skip to content

ENH: add user-facing no-output draw #19967

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

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 8 additions & 0 deletions doc/users/next_whats_new/fig_draw_no_output.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Figure now has draw_no_output method
------------------------------------

Rarely, the user will want to trigger a draw without making output to
either the screen or a file. This is useful for determining the final
position of artists on the figure that require a draw like text.
This could be accomplished via ``fig.canvas.draw()`` but that is
not user-facing, so a new method on `.Figure.draw_no_output` is provided.
9 changes: 8 additions & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from matplotlib.artist import (
Artist, allow_rasterization, _finalize_rasterization)
from matplotlib.backend_bases import (
FigureCanvasBase, NonGuiException, MouseButton)
FigureCanvasBase, NonGuiException, MouseButton, _no_output_draw)
import matplotlib._api as _api
import matplotlib.cbook as cbook
import matplotlib.colorbar as cbar
Expand Down Expand Up @@ -2739,6 +2739,13 @@ def draw(self, renderer):

self.canvas.draw_event(renderer)

def draw_no_output(self):
"""
Draw the figure with no output. Useful to get the final size of
artists that require a draw before their size is known (e.g. text).
"""
_no_output_draw(self)

def draw_artist(self, a):
"""
Draw `.Artist` *a* only.
Expand Down
5 changes: 2 additions & 3 deletions lib/matplotlib/tests/test_constrainedlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ def test_constrained_layout7():
for gs in gsl:
fig.add_subplot(gs)
# need to trigger a draw to get warning
fig.draw(fig.canvas.get_renderer())

fig.draw_no_output()

@image_comparison(['constrained_layout8.png'])
def test_constrained_layout8():
Expand Down Expand Up @@ -327,7 +326,7 @@ def test_constrained_layout18():
ax2 = ax.twinx()
example_plot(ax)
example_plot(ax2, fontsize=24)
fig.canvas.draw()
fig.draw_no_output()
assert all(ax.get_position().extents == ax2.get_position().extents)


Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,11 @@ def test_autofmt_xdate(which):
@pytest.mark.style('default')
def test_change_dpi():
fig = plt.figure(figsize=(4, 4))
fig.canvas.draw()
fig.draw_no_output()
assert fig.canvas.renderer.height == 400
assert fig.canvas.renderer.width == 400
fig.dpi = 50
fig.canvas.draw()
fig.draw_no_output()
assert fig.canvas.renderer.height == 200
assert fig.canvas.renderer.width == 200

Expand Down