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

Skip to content

Commit 403ccf7

Browse files
authored
Merge pull request #20931 from tacaswell/api_rename_draw_no_output
API: rename draw_no_output to draw_without_rendering
2 parents 3fc0448 + f90819a commit 403ccf7

15 files changed

+52
-50
lines changed

doc/users/next_whats_new/fig_draw_no_output.rst

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Figure now has ``draw_without_rendering`` method
2+
------------------------------------------------
3+
4+
Some aspects of a figure are only determined at draw-time, such as the exact
5+
position of text artists or deferred computation like automatic data limits.
6+
If you need these values, you can use ``figure.canvas.draw()`` to force a full
7+
draw. However, this has side effects, sometimes requires an open file, and is
8+
doing more work than is needed.
9+
10+
The new `.Figure.draw_without_rendering` method runs all the updates that
11+
``draw()`` does, but skips rendering the figure. It's thus more efficient if you
12+
need the updated values to configure further aspects of the figure.

lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ def _draw(renderer): raise Done(renderer)
15701570
def _no_output_draw(figure):
15711571
# _no_output_draw was promoted to the figure level, but
15721572
# keep this here in case someone was calling it...
1573-
figure.draw_no_output()
1573+
figure.draw_without_rendering()
15741574

15751575

15761576
def _is_non_interactive_terminal_ipython(ip):

lib/matplotlib/backends/backend_pdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2793,7 +2793,7 @@ def print_pdf(self, filename, *,
27932793
file.close()
27942794

27952795
def draw(self):
2796-
self.figure.draw_no_output()
2796+
self.figure.draw_without_rendering()
27972797
return super().draw()
27982798

27992799

lib/matplotlib/backends/backend_pgf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ def get_renderer(self):
883883
return RendererPgf(self.figure, None)
884884

885885
def draw(self):
886-
self.figure.draw_no_output()
886+
self.figure.draw_without_rendering()
887887
return super().draw()
888888

889889

lib/matplotlib/backends/backend_ps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ def _print_figure_tex(
11191119
_move_path_to_path_or_stream(tmpfile, outfile)
11201120

11211121
def draw(self):
1122-
self.figure.draw_no_output()
1122+
self.figure.draw_without_rendering()
11231123
return super().draw()
11241124

11251125

lib/matplotlib/backends/backend_svg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ def get_default_filetype(self):
13431343
return 'svg'
13441344

13451345
def draw(self):
1346-
self.figure.draw_no_output()
1346+
self.figure.draw_without_rendering()
13471347
return super().draw()
13481348

13491349

lib/matplotlib/figure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2808,7 +2808,7 @@ def draw(self, renderer):
28082808

28092809
self.canvas.draw_event(renderer)
28102810

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

lib/matplotlib/tests/test_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4793,7 +4793,7 @@ def test_reset_ticks(fig_test, fig_ref):
47934793
labelsize=14, labelcolor='C1', labelrotation=45,
47944794
grid_color='C2', grid_alpha=0.8, grid_linewidth=3,
47954795
grid_linestyle='--')
4796-
fig.draw_no_output()
4796+
fig.draw_without_rendering()
47974797

47984798
# After we've changed any setting on ticks, reset_ticks will mean
47994799
# re-creating them from scratch. This *should* appear the same as not

lib/matplotlib/tests/test_colorbar.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ def test_mappable_2d_alpha():
613613
# the original alpha array
614614
assert cb.alpha is None
615615
assert pc.get_alpha() is x
616-
fig.draw_no_output()
616+
fig.draw_without_rendering()
617617

618618

619619
def test_colorbar_label():
@@ -766,7 +766,7 @@ def test_inset_colorbar_layout():
766766
cax = ax.inset_axes([1.02, 0.1, 0.03, 0.8])
767767
cb = fig.colorbar(pc, cax=cax)
768768

769-
fig.draw_no_output()
769+
fig.draw_without_rendering()
770770
# make sure this is in the figure. In the colorbar swapping
771771
# it was being dropped from the list of children...
772772
np.testing.assert_allclose(cb.ax.get_position().bounds,
@@ -806,7 +806,7 @@ def test_aspects():
806806
pc = ax[mm, nn].pcolormesh(np.arange(100).reshape(10, 10))
807807
cb[nn][mm] = fig.colorbar(pc, ax=ax[mm, nn], orientation=orient,
808808
aspect=aspect, extend=extend)
809-
fig.draw_no_output()
809+
fig.draw_without_rendering()
810810
# check the extends are right ratio:
811811
np.testing.assert_almost_equal(cb[0][1].ax.get_position().height,
812812
cb[0][0].ax.get_position().height * 0.9,

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def test_constrained_layout7():
128128
for gs in gsl:
129129
fig.add_subplot(gs)
130130
# need to trigger a draw to get warning
131-
fig.draw_no_output()
131+
fig.draw_without_rendering()
132132

133133

134134
@image_comparison(['constrained_layout8.png'])
@@ -309,7 +309,7 @@ def test_constrained_layout18():
309309
ax2 = ax.twinx()
310310
example_plot(ax)
311311
example_plot(ax2, fontsize=24)
312-
fig.draw_no_output()
312+
fig.draw_without_rendering()
313313
assert all(ax.get_position().extents == ax2.get_position().extents)
314314

315315

@@ -321,7 +321,7 @@ def test_constrained_layout19():
321321
example_plot(ax2, fontsize=24)
322322
ax2.set_title('')
323323
ax.set_title('')
324-
fig.draw_no_output()
324+
fig.draw_without_rendering()
325325
assert all(ax.get_position().extents == ax2.get_position().extents)
326326

327327

@@ -341,11 +341,11 @@ def test_constrained_layout21():
341341
fig, ax = plt.subplots(constrained_layout=True)
342342

343343
fig.suptitle("Suptitle0")
344-
fig.draw_no_output()
344+
fig.draw_without_rendering()
345345
extents0 = np.copy(ax.get_position().extents)
346346

347347
fig.suptitle("Suptitle1")
348-
fig.draw_no_output()
348+
fig.draw_without_rendering()
349349
extents1 = np.copy(ax.get_position().extents)
350350

351351
np.testing.assert_allclose(extents0, extents1)
@@ -355,11 +355,11 @@ def test_constrained_layout22():
355355
"""#11035: suptitle should not be include in CL if manually positioned"""
356356
fig, ax = plt.subplots(constrained_layout=True)
357357

358-
fig.draw_no_output()
358+
fig.draw_without_rendering()
359359
extents0 = np.copy(ax.get_position().extents)
360360

361361
fig.suptitle("Suptitle", y=0.5)
362-
fig.draw_no_output()
362+
fig.draw_without_rendering()
363363
extents1 = np.copy(ax.get_position().extents)
364364

365365
np.testing.assert_allclose(extents0, extents1)
@@ -407,7 +407,7 @@ def test_hidden_axes():
407407
# (as does a gridspec slot that is empty)
408408
fig, axs = plt.subplots(2, 2, constrained_layout=True)
409409
axs[0, 1].set_visible(False)
410-
fig.draw_no_output()
410+
fig.draw_without_rendering()
411411
extents1 = np.copy(axs[0, 0].get_position().extents)
412412

413413
np.testing.assert_allclose(
@@ -433,7 +433,7 @@ def test_colorbar_align():
433433
fig.set_constrained_layout_pads(w_pad=4 / 72, h_pad=4 / 72, hspace=0.1,
434434
wspace=0.1)
435435

436-
fig.draw_no_output()
436+
fig.draw_without_rendering()
437437
if location in ['left', 'right']:
438438
np.testing.assert_allclose(cbs[0].ax.get_position().x0,
439439
cbs[2].ax.get_position().x0)
@@ -475,15 +475,15 @@ def test_colorbars_no_overlapH():
475475
def test_manually_set_position():
476476
fig, axs = plt.subplots(1, 2, constrained_layout=True)
477477
axs[0].set_position([0.2, 0.2, 0.3, 0.3])
478-
fig.draw_no_output()
478+
fig.draw_without_rendering()
479479
pp = axs[0].get_position()
480480
np.testing.assert_allclose(pp, [[0.2, 0.2], [0.5, 0.5]])
481481

482482
fig, axs = plt.subplots(1, 2, constrained_layout=True)
483483
axs[0].set_position([0.2, 0.2, 0.3, 0.3])
484484
pc = axs[0].pcolormesh(np.random.rand(20, 20))
485485
fig.colorbar(pc, ax=axs[0])
486-
fig.draw_no_output()
486+
fig.draw_without_rendering()
487487
pp = axs[0].get_position()
488488
np.testing.assert_allclose(pp, [[0.2, 0.2], [0.44, 0.5]])
489489

@@ -528,7 +528,7 @@ def test_align_labels():
528528

529529
fig.align_ylabels(axs=(ax3, ax1, ax2))
530530

531-
fig.draw_no_output()
531+
fig.draw_without_rendering()
532532
after_align = [ax1.yaxis.label.get_window_extent(),
533533
ax2.yaxis.label.get_window_extent(),
534534
ax3.yaxis.label.get_window_extent()]
@@ -541,22 +541,22 @@ def test_align_labels():
541541

542542
def test_suplabels():
543543
fig, ax = plt.subplots(constrained_layout=True)
544-
fig.draw_no_output()
544+
fig.draw_without_rendering()
545545
pos0 = ax.get_tightbbox(fig.canvas.get_renderer())
546546
fig.supxlabel('Boo')
547547
fig.supylabel('Booy')
548-
fig.draw_no_output()
548+
fig.draw_without_rendering()
549549
pos = ax.get_tightbbox(fig.canvas.get_renderer())
550550
assert pos.y0 > pos0.y0 + 10.0
551551
assert pos.x0 > pos0.x0 + 10.0
552552

553553
fig, ax = plt.subplots(constrained_layout=True)
554-
fig.draw_no_output()
554+
fig.draw_without_rendering()
555555
pos0 = ax.get_tightbbox(fig.canvas.get_renderer())
556556
# check that specifying x (y) doesn't ruin the layout
557557
fig.supxlabel('Boo', x=0.5)
558558
fig.supylabel('Boo', y=0.5)
559-
fig.draw_no_output()
559+
fig.draw_without_rendering()
560560
pos = ax.get_tightbbox(fig.canvas.get_renderer())
561561
assert pos.y0 > pos0.y0 + 10.0
562562
assert pos.x0 > pos0.x0 + 10.0

lib/matplotlib/tests/test_dates.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_date_empty():
7676
# http://sourceforge.net/tracker/?func=detail&aid=2850075&group_id=80706&atid=560720
7777
fig, ax = plt.subplots()
7878
ax.xaxis_date()
79-
fig.draw_no_output()
79+
fig.draw_without_rendering()
8080
np.testing.assert_allclose(ax.get_xlim(),
8181
[mdates.date2num(np.datetime64('2000-01-01')),
8282
mdates.date2num(np.datetime64('2010-01-01'))])
@@ -85,7 +85,7 @@ def test_date_empty():
8585
mdates.set_epoch('0000-12-31')
8686
fig, ax = plt.subplots()
8787
ax.xaxis_date()
88-
fig.draw_no_output()
88+
fig.draw_without_rendering()
8989
np.testing.assert_allclose(ax.get_xlim(),
9090
[mdates.date2num(np.datetime64('2000-01-01')),
9191
mdates.date2num(np.datetime64('2010-01-01'))])

lib/matplotlib/tests/test_figure.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,11 @@ def test_autofmt_xdate(which):
412412
@mpl.style.context('default')
413413
def test_change_dpi():
414414
fig = plt.figure(figsize=(4, 4))
415-
fig.draw_no_output()
415+
fig.draw_without_rendering()
416416
assert fig.canvas.renderer.height == 400
417417
assert fig.canvas.renderer.width == 400
418418
fig.dpi = 50
419-
fig.draw_no_output()
419+
fig.draw_without_rendering()
420420
assert fig.canvas.renderer.height == 200
421421
assert fig.canvas.renderer.width == 200
422422

@@ -1082,10 +1082,10 @@ def test_subfigure_ticks():
10821082
ax3 = subfig_bl.add_subplot(gs[0, 3:14], sharey=ax1)
10831083

10841084
fig.set_dpi(120)
1085-
fig.draw_no_output()
1085+
fig.draw_without_rendering()
10861086
ticks120 = ax2.get_xticks()
10871087
fig.set_dpi(300)
1088-
fig.draw_no_output()
1088+
fig.draw_without_rendering()
10891089
ticks300 = ax2.get_xticks()
10901090
np.testing.assert_allclose(ticks120, ticks300)
10911091

lib/matplotlib/tests/test_mathtext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def test_default_math_fontfamily():
402402
prop2 = text2.get_fontproperties()
403403
assert prop2.get_math_fontfamily() == 'cm'
404404

405-
fig.draw_no_output()
405+
fig.draw_without_rendering()
406406

407407

408408
def test_argument_order():
@@ -427,7 +427,7 @@ def test_argument_order():
427427
prop4 = text4.get_fontproperties()
428428
assert prop4.get_math_fontfamily() == 'dejavusans'
429429

430-
fig.draw_no_output()
430+
fig.draw_without_rendering()
431431

432432

433433
def test_mathtext_cmr10_minus_sign():

lib/matplotlib/tests/test_units.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,38 +226,38 @@ def test_empty_default_limits(quantity_converter):
226226
munits.registry[Quantity] = quantity_converter
227227
fig, ax1 = plt.subplots()
228228
ax1.xaxis.update_units(Quantity([10], "miles"))
229-
fig.draw_no_output()
229+
fig.draw_without_rendering()
230230
assert ax1.get_xlim() == (0, 100)
231231
ax1.yaxis.update_units(Quantity([10], "miles"))
232-
fig.draw_no_output()
232+
fig.draw_without_rendering()
233233
assert ax1.get_ylim() == (0, 100)
234234

235235
fig, ax = plt.subplots()
236236
ax.axhline(30)
237237
ax.plot(Quantity(np.arange(0, 3), "miles"),
238238
Quantity(np.arange(0, 6, 2), "feet"))
239-
fig.draw_no_output()
239+
fig.draw_without_rendering()
240240
assert ax.get_xlim() == (0, 2)
241241
assert ax.get_ylim() == (0, 30)
242242

243243
fig, ax = plt.subplots()
244244
ax.axvline(30)
245245
ax.plot(Quantity(np.arange(0, 3), "miles"),
246246
Quantity(np.arange(0, 6, 2), "feet"))
247-
fig.draw_no_output()
247+
fig.draw_without_rendering()
248248
assert ax.get_xlim() == (0, 30)
249249
assert ax.get_ylim() == (0, 4)
250250

251251
fig, ax = plt.subplots()
252252
ax.xaxis.update_units(Quantity([10], "miles"))
253253
ax.axhline(30)
254-
fig.draw_no_output()
254+
fig.draw_without_rendering()
255255
assert ax.get_xlim() == (0, 100)
256256
assert ax.get_ylim() == (28.5, 31.5)
257257

258258
fig, ax = plt.subplots()
259259
ax.yaxis.update_units(Quantity([10], "miles"))
260260
ax.axvline(30)
261-
fig.draw_no_output()
261+
fig.draw_without_rendering()
262262
assert ax.get_ylim() == (0, 100)
263263
assert ax.get_xlim() == (28.5, 31.5)

0 commit comments

Comments
 (0)