From d47bb3ae7db1526ff4b5c76494b58b35a372d7f1 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 15 Apr 2021 07:24:13 -0700 Subject: [PATCH 1/2] ENH: add user-facing no-output draw --- lib/matplotlib/figure.py | 9 ++++++++- lib/matplotlib/tests/test_constrainedlayout.py | 5 ++--- lib/matplotlib/tests/test_figure.py | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 6691e91713a7..0c960efe3196 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -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 @@ -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. diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index 67474628e7bf..8783be7f0628 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -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(): @@ -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) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 635037c43442..8d589fc5890c 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -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 From aed10f34d4e22805d0181aa82c7d0ced2cb069cf Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 15 Apr 2021 07:40:01 -0700 Subject: [PATCH 2/2] Add doc and fix flk8 --- doc/users/next_whats_new/fig_draw_no_output.rst | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 doc/users/next_whats_new/fig_draw_no_output.rst diff --git a/doc/users/next_whats_new/fig_draw_no_output.rst b/doc/users/next_whats_new/fig_draw_no_output.rst new file mode 100644 index 000000000000..5948d75a2f70 --- /dev/null +++ b/doc/users/next_whats_new/fig_draw_no_output.rst @@ -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. \ No newline at end of file