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

Commit5d208e9

Browse files
Add fig.add_artist method
1 parent721fc04 commit5d208e9

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

‎doc/users/whats_new.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,22 @@ specify a number that is close (i.e. ``ax.title.set_position(0.5, 1.01)``)
190190
and the title will not be moved via this algorithm.
191191

192192

193+
Figure has an `~.figure.Figure.add_artist` method
194+
-------------------------------------------------
193195

196+
A method `~.figure.Figure.add_artist` has been added to the
197+
:class:`~.figure.Figure` class, which allows artists to be added directly
198+
to a figure. E.g.
199+
200+
::
201+
202+
circ = plt.Circle((.7, .5), .05)
203+
fig.add_artist(circ)
204+
205+
In case the added artist has no transform set previously, it will be set to
206+
the figure transform (``fig.transFigure``).
207+
This new method may be useful for adding artists to figures without axes or to
208+
easily position static elements in figure coordinates.
194209

195210

196211

‎lib/matplotlib/figure.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,42 @@ def fixlist(args):
10451045
key=fixlist(args),fixitems(kwargs.items())
10461046
returnkey
10471047

1048+
defadd_artist(self,artist,clip=False):
1049+
"""
1050+
Add any :class:`~matplotlib.artist.Artist` to the figure.
1051+
1052+
Usually artists are added to axes objects using
1053+
:meth:`matplotlib.axes.Axes.add_artist`, but use this method in the
1054+
rare cases that adding directly to the figure is necessary.
1055+
1056+
Parameters
1057+
----------
1058+
artist : `~matplotlib.artist.Artist`
1059+
The artist to add to the figure. If the added artist has no
1060+
transform previously set, its transform will be set to
1061+
``figure.transFigure``.
1062+
clip : bool, optional, default ``False``
1063+
An optional parameter ``clip`` determines whether the added artist
1064+
should be clipped by the figure patch. Default is *False*,
1065+
i.e. no clipping.
1066+
1067+
Returns
1068+
-------
1069+
artist : The added `~matplotlib.artist.Artist`
1070+
"""
1071+
artist.set_figure(self)
1072+
self.artists.append(artist)
1073+
artist._remove_method=self.artists.remove
1074+
1075+
ifnotartist.is_transform_set():
1076+
artist.set_transform(self.transFigure)
1077+
1078+
ifclip:
1079+
artist.set_clip_path(self.patch)
1080+
1081+
self.stale=True
1082+
returnartist
1083+
10481084
@docstring.dedent_interpd
10491085
defadd_axes(self,*args,**kwargs):
10501086
"""

‎lib/matplotlib/tests/test_figure.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
importsys
22
importwarnings
3+
importio
34

45
frommatplotlibimportrcParams
56
frommatplotlib.testing.decoratorsimportimage_comparison
@@ -381,6 +382,34 @@ def test_warn_cl_plus_tl():
381382
assertnot(fig.get_constrained_layout())
382383

383384

385+
deftest_add_artist():
386+
fig,ax=plt.subplots(dpi=100)
387+
l1=plt.Line2D([.2,.7], [.7,.7])
388+
l2=plt.Line2D([.2,.7], [.8,.8])
389+
r1=plt.Circle((20,20),100,transform=None)
390+
r2=plt.Circle((.7,.5),.05)
391+
r3=plt.Circle((4.5,.8),.55,transform=fig.dpi_scale_trans,
392+
facecolor='crimson')
393+
forain [l1,l2,r1,r2,r3]:
394+
fig.add_artist(a)
395+
l2.remove()
396+
buf1=io.BytesIO()
397+
fig.savefig(buf1)
398+
399+
fig2,ax2=plt.subplots(dpi=100)
400+
l1=plt.Line2D([.2,.7], [.7,.7],transform=fig2.transFigure)
401+
r1=plt.Circle((20,20),100,transform=None,clip_on=False,zorder=20)
402+
r2=plt.Circle((.7,.5),.05,transform=fig2.transFigure)
403+
r3=plt.Circle((4.5,.8),.55,transform=fig.dpi_scale_trans,
404+
facecolor='crimson',clip_on=False,zorder=20)
405+
forain [l1,r1,r2,r3]:
406+
ax2.add_artist(a)
407+
buf2=io.BytesIO()
408+
fig2.savefig(buf2)
409+
410+
assertbuf1.getvalue()==buf2.getvalue()
411+
412+
384413
@pytest.mark.skipif(sys.version_info< (3,6),reason="requires Python 3.6+")
385414
@pytest.mark.parametrize("fmt", ["png","pdf","ps","eps","svg"])
386415
deftest_fspath(fmt,tmpdir):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp