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

Skip to content

Commit c6bfb7c

Browse files
committed
ENH: add user-facing no-output draw
1 parent 8dd2838 commit c6bfb7c

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Figure now has draw_no_output method
2+
------------------------------------
3+
4+
Rarely, the user will want to trigger a draw without making output to
5+
either the screen or a file. This is useful for determining the final
6+
position of artists on the figure that require a draw like text.
7+
This could be accomplished via ``fig.canvas.draw()`` but that is
8+
not user-facing, so a new method on `.Figure.draw_no_output` is provided.

lib/matplotlib/figure.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from matplotlib.artist import (
2727
Artist, allow_rasterization, _finalize_rasterization)
2828
from matplotlib.backend_bases import (
29-
FigureCanvasBase, NonGuiException, MouseButton)
29+
FigureCanvasBase, NonGuiException, MouseButton, _no_output_draw)
3030
import matplotlib._api as _api
3131
import matplotlib.cbook as cbook
3232
import matplotlib.colorbar as cbar
@@ -2739,6 +2739,13 @@ def draw(self, renderer):
27392739

27402740
self.canvas.draw_event(renderer)
27412741

2742+
def draw_no_output(self):
2743+
"""
2744+
Draw the figure with no output. Useful to get the final size of
2745+
artists that require a draw before their size is known (e.g. text).
2746+
"""
2747+
_no_output_draw(self)
2748+
27422749
def draw_artist(self, a):
27432750
"""
27442751
Draw `.Artist` *a* only.

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def test_constrained_layout7():
134134
for gs in gsl:
135135
fig.add_subplot(gs)
136136
# need to trigger a draw to get warning
137-
fig.draw(fig.canvas.get_renderer())
137+
fig.draw_no_output()
138138

139139

140140
@image_comparison(['constrained_layout8.png'])
@@ -327,7 +327,7 @@ def test_constrained_layout18():
327327
ax2 = ax.twinx()
328328
example_plot(ax)
329329
example_plot(ax2, fontsize=24)
330-
fig.canvas.draw()
330+
fig.draw_no_output()
331331
assert all(ax.get_position().extents == ax2.get_position().extents)
332332

333333

@@ -339,7 +339,7 @@ def test_constrained_layout19():
339339
example_plot(ax2, fontsize=24)
340340
ax2.set_title('')
341341
ax.set_title('')
342-
fig.canvas.draw()
342+
fig.draw_no_output()
343343
assert all(ax.get_position().extents == ax2.get_position().extents)
344344

345345

@@ -359,11 +359,11 @@ def test_constrained_layout21():
359359
fig, ax = plt.subplots(constrained_layout=True)
360360

361361
fig.suptitle("Suptitle0")
362-
fig.canvas.draw()
362+
fig.draw_no_output()
363363
extents0 = np.copy(ax.get_position().extents)
364364

365365
fig.suptitle("Suptitle1")
366-
fig.canvas.draw()
366+
fig.draw_no_output()
367367
extents1 = np.copy(ax.get_position().extents)
368368

369369
np.testing.assert_allclose(extents0, extents1)
@@ -373,11 +373,11 @@ def test_constrained_layout22():
373373
"""#11035: suptitle should not be include in CL if manually positioned"""
374374
fig, ax = plt.subplots(constrained_layout=True)
375375

376-
fig.canvas.draw()
376+
fig.draw_no_output()
377377
extents0 = np.copy(ax.get_position().extents)
378378

379379
fig.suptitle("Suptitle", y=0.5)
380-
fig.canvas.draw()
380+
fig.draw_no_output()
381381
extents1 = np.copy(ax.get_position().extents)
382382

383383
np.testing.assert_allclose(extents0, extents1)
@@ -425,7 +425,7 @@ def test_hidden_axes():
425425
# (as does a gridspec slot that is empty)
426426
fig, axs = plt.subplots(2, 2, constrained_layout=True)
427427
axs[0, 1].set_visible(False)
428-
fig.canvas.draw()
428+
fig.draw_no_output()
429429
extents1 = np.copy(axs[0, 0].get_position().extents)
430430

431431
np.testing.assert_allclose(
@@ -451,7 +451,7 @@ def test_colorbar_align():
451451
fig.set_constrained_layout_pads(w_pad=4 / 72, h_pad=4 / 72, hspace=0.1,
452452
wspace=0.1)
453453

454-
fig.canvas.draw()
454+
fig.draw_no_output()
455455
if location in ['left', 'right']:
456456
np.testing.assert_allclose(cbs[0].ax.get_position().x0,
457457
cbs[2].ax.get_position().x0)
@@ -493,15 +493,15 @@ def test_colorbars_no_overlapH():
493493
def test_manually_set_position():
494494
fig, axs = plt.subplots(1, 2, constrained_layout=True)
495495
axs[0].set_position([0.2, 0.2, 0.3, 0.3])
496-
fig.canvas.draw()
496+
fig.draw_no_output()
497497
pp = axs[0].get_position()
498498
np.testing.assert_allclose(pp, [[0.2, 0.2], [0.5, 0.5]])
499499

500500
fig, axs = plt.subplots(1, 2, constrained_layout=True)
501501
axs[0].set_position([0.2, 0.2, 0.3, 0.3])
502502
pc = axs[0].pcolormesh(np.random.rand(20, 20))
503503
fig.colorbar(pc, ax=axs[0])
504-
fig.canvas.draw()
504+
fig.draw_no_output()
505505
pp = axs[0].get_position()
506506
np.testing.assert_allclose(pp, [[0.2, 0.2], [0.44, 0.5]])
507507

@@ -546,7 +546,7 @@ def test_align_labels():
546546

547547
fig.align_ylabels(axs=(ax3, ax1, ax2))
548548

549-
fig.canvas.draw()
549+
fig.draw_no_output()
550550
after_align = [ax1.yaxis.label.get_window_extent(),
551551
ax2.yaxis.label.get_window_extent(),
552552
ax3.yaxis.label.get_window_extent()]

lib/matplotlib/tests/test_figure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,11 @@ def test_autofmt_xdate(which):
415415
@pytest.mark.style('default')
416416
def test_change_dpi():
417417
fig = plt.figure(figsize=(4, 4))
418-
fig.canvas.draw()
418+
fig.draw_no_output()
419419
assert fig.canvas.renderer.height == 400
420420
assert fig.canvas.renderer.width == 400
421421
fig.dpi = 50
422-
fig.canvas.draw()
422+
fig.draw_no_output()
423423
assert fig.canvas.renderer.height == 200
424424
assert fig.canvas.renderer.width == 200
425425

0 commit comments

Comments
 (0)