Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit48bff43

Browse files
committed
FIX/API:fig.canvas.draw always updates internal state
Previously the non-interactive backends, other than Agg, did notdefine `draw` methods and fell back to the base no-op version.However, we have been documenting that the correct way to update thevarious internal state we keep (run tight/constrained layouts, autolimits, text size/position, ...), this is now a bug due to oursuggested usage drifting.closes#18407
1 parentb52aad8 commit48bff43

File tree

6 files changed

+62
-6
lines changed

6 files changed

+62
-6
lines changed

‎lib/matplotlib/backend_bases.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,12 @@ def _draw(renderer): raise Done(renderer)
15871587
figure.canvas=orig_canvas
15881588

15891589

1590+
def_no_output_draw(figure):
1591+
renderer=_get_renderer(figure)
1592+
withrenderer._draw_disabled():
1593+
figure.draw(renderer)
1594+
1595+
15901596
def_is_non_interactive_terminal_ipython(ip):
15911597
"""
15921598
Return whether we are in a a terminal IPython, but non interactive.
@@ -1624,7 +1630,9 @@ def _check_savefig_extra_args(func=None, extra_kwargs=()):
16241630
@functools.wraps(func)
16251631
defwrapper(*args,**kwargs):
16261632
name='savefig'# Reasonable default guess.
1627-
public_api=re.compile(r'^savefig|print_[A-Za-z0-9]+$')
1633+
public_api=re.compile(
1634+
r'^savefig|print_[A-Za-z0-9]+|_no_output_draw$'
1635+
)
16281636
seen_print_figure=False
16291637
forframe,lineintraceback.walk_stack(None):
16301638
ifframeisNone:
@@ -1635,8 +1643,9 @@ def wrapper(*args, **kwargs):
16351643
frame.f_globals.get('__name__','')):
16361644
ifpublic_api.match(frame.f_code.co_name):
16371645
name=frame.f_code.co_name
1638-
ifname=='print_figure':
1646+
ifnamein ('print_figure','_no_output_draw'):
16391647
seen_print_figure=True
1648+
16401649
else:
16411650
break
16421651

‎lib/matplotlib/backends/backend_pdf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
frommatplotlib._pylab_helpersimportGcf
2929
frommatplotlib.backend_basesimport (
3030
_Backend,_check_savefig_extra_args,FigureCanvasBase,FigureManagerBase,
31-
GraphicsContextBase,RendererBase)
31+
GraphicsContextBase,RendererBase,_no_output_draw)
3232
frommatplotlib.backends.backend_mixedimportMixedModeRenderer
3333
frommatplotlib.figureimportFigure
3434
frommatplotlib.font_managerimportfindfont,get_font
@@ -2727,6 +2727,9 @@ def print_pdf(self, filename, *,
27272727
else:# we opened the file above; now finish it off
27282728
file.close()
27292729

2730+
defdraw(self,*args,**kwargs):
2731+
_no_output_draw(self.figure)
2732+
returnsuper().draw(*args,**kwargs)
27302733

27312734
FigureManagerPdf=FigureManagerBase
27322735

‎lib/matplotlib/backends/backend_pgf.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
frommatplotlibimport_api,cbook,font_managerasfm
2020
frommatplotlib.backend_basesimport (
2121
_Backend,_check_savefig_extra_args,FigureCanvasBase,FigureManagerBase,
22-
GraphicsContextBase,RendererBase)
22+
GraphicsContextBase,RendererBase,_no_output_draw
23+
)
2324
frommatplotlib.backends.backend_mixedimportMixedModeRenderer
2425
frommatplotlib.backends.backend_pdfimport (
2526
_create_pdf_info_dict,_datetime_to_pdf)
@@ -900,6 +901,10 @@ def print_png(self, fname_or_fh, *args, **kwargs):
900901
defget_renderer(self):
901902
returnRendererPgf(self.figure,None)
902903

904+
defdraw(self,*args,**kwargs):
905+
_no_output_draw(self.figure)
906+
returnsuper().draw()
907+
903908

904909
FigureManagerPgf=FigureManagerBase
905910

‎lib/matplotlib/backends/backend_ps.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
frommatplotlib.afmimportAFM
2424
frommatplotlib.backend_basesimport (
2525
_Backend,_check_savefig_extra_args,FigureCanvasBase,FigureManagerBase,
26-
GraphicsContextBase,RendererBase)
26+
GraphicsContextBase,RendererBase,_no_output_draw)
2727
frommatplotlib.cbookimportis_writable_file_like,file_requires_unicode
2828
frommatplotlib.font_managerimportget_font
2929
frommatplotlib.ft2fontimportLOAD_NO_HINTING,LOAD_NO_SCALE
@@ -1126,6 +1126,9 @@ def _print_figure_tex(
11261126

11271127
_move_path_to_path_or_stream(tmpfile,outfile)
11281128

1129+
defdraw(self,*args,**kwargs):
1130+
_no_output_draw(self.figure)
1131+
returnsuper().draw()
11291132

11301133
defconvert_psfrags(tmpfile,psfrags,font_preamble,custom_preamble,
11311134
paper_width,paper_height,orientation):

‎lib/matplotlib/backends/backend_svg.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
frommatplotlibimport_api,cbook
1818
frommatplotlib.backend_basesimport (
1919
_Backend,_check_savefig_extra_args,FigureCanvasBase,FigureManagerBase,
20-
RendererBase)
20+
RendererBase,_no_output_draw)
2121
frommatplotlib.backends.backend_mixedimportMixedModeRenderer
2222
frommatplotlib.colorsimportrgb2hex
2323
frommatplotlib.datesimportUTC
@@ -1360,6 +1360,9 @@ def _print_svg(self, filename, fh, *, dpi=72, bbox_inches_restore=None,
13601360
defget_default_filetype(self):
13611361
return'svg'
13621362

1363+
defdraw(self,*args,**kwargs):
1364+
_no_output_draw(self.figure)
1365+
returnsuper().draw()
13631366

13641367
FigureManagerSVG=FigureManagerBase
13651368

‎lib/matplotlib/tests/test_backend_bases.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,36 @@ def test_toolbar_zoompan():
180180
assertax.get_navigate_mode()=="ZOOM"
181181
ax.figure.canvas.manager.toolmanager.trigger_tool('pan')
182182
assertax.get_navigate_mode()=="PAN"
183+
184+
185+
@pytest.mark.parametrize("backend", ['svg','pgf','ps','pdf'])
186+
deftest_draw(backend):
187+
frommatplotlib.figureimportFigure
188+
frommatplotlib.backends.backend_aggimportFigureCanvas
189+
test_backend=pytest.importorskip(
190+
f'matplotlib.backends.backend_{backend}'
191+
)
192+
TestCanvas=test_backend.FigureCanvas
193+
fig_test=Figure(constrained_layout=True)
194+
TestCanvas(fig_test)
195+
axes_test=fig_test.subplots(2,2)
196+
197+
# defaults to FigureCanvasBase
198+
fig_agg=Figure(constrained_layout=True)
199+
# put a backends.backend_agg.FigureCanvas on it
200+
FigureCanvas(fig_agg)
201+
axes_agg=fig_agg.subplots(2,2)
202+
203+
init_pos= [ax.get_position()foraxinaxes_test.ravel()]
204+
205+
fig_test.canvas.draw()
206+
fig_agg.canvas.draw()
207+
208+
layed_out_pos_test= [ax.get_position()foraxinaxes_test.ravel()]
209+
layed_out_pos_agg= [ax.get_position()foraxinaxes_agg.ravel()]
210+
211+
forinit,placedinzip(init_pos,layed_out_pos_test):
212+
assertnotnp.allclose(init,placed,atol=0.005)
213+
214+
forref,testinzip(layed_out_pos_agg,layed_out_pos_test):
215+
np.testing.assert_allclose(ref,test,atol=0.005)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp