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

Appearance settings

Commit ac58f1f

Browse filesBrowse files
authored
Merge pull request #11234 from ImportanceOfBeingErnest/fig-add-artist
ENG: Add Figure.add_artist method
2 parents b8838c3 + b6326cc commit ac58f1f
Copy full SHA for ac58f1f

File tree

Expand file treeCollapse file tree

3 files changed

+81
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+81
-1
lines changed

‎doc/users/whats_new.rst

Copy file name to clipboardExpand all lines: doc/users/whats_new.rst
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ 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+
193194
New convenience methods for GridSpec
194195
------------------------------------
195196

@@ -210,6 +211,23 @@ now call `.Figure.add_gridspec` and for the latter `.SubplotSpec.subgridspec`.
210211
fig.add_subplot(gssub[0, i])
211212
212213
214+
Figure has an `~.figure.Figure.add_artist` method
215+
-------------------------------------------------
216+
217+
A method `~.figure.Figure.add_artist` has been added to the
218+
:class:`~.figure.Figure` class, which allows artists to be added directly
219+
to a figure. E.g.
220+
221+
::
222+
223+
circ = plt.Circle((.7, .5), .05)
224+
fig.add_artist(circ)
225+
226+
In case the added artist has no transform set previously, it will be set to
227+
the figure transform (``fig.transFigure``).
228+
This new method may be useful for adding artists to figures without axes or to
229+
easily position static elements in figure coordinates.
230+
213231

214232

215233

‎lib/matplotlib/figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/figure.py
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,42 @@ def fixlist(args):
10481048
key = fixlist(args), fixitems(kwargs.items())
10491049
return key
10501050

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

‎lib/matplotlib/tests/test_figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_figure.py
+27-1Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import platform
44

55
from matplotlib import rcParams
6-
from matplotlib.testing.decorators import image_comparison
6+
from matplotlib.testing.decorators import image_comparison, check_figures_equal
77
from matplotlib.axes import Axes
88
from matplotlib.ticker import AutoMinorLocator, FixedFormatter
99
import matplotlib.pyplot as plt
@@ -383,6 +383,32 @@ def test_warn_cl_plus_tl():
383383
assert not(fig.get_constrained_layout())
384384

385385

386+
@check_figures_equal(extensions=["png", "pdf", "svg"])
387+
def test_add_artist(fig_test, fig_ref):
388+
fig_test.set_dpi(100)
389+
fig_ref.set_dpi(100)
390+
391+
ax = fig_test.subplots()
392+
l1 = plt.Line2D([.2, .7], [.7, .7])
393+
l2 = plt.Line2D([.2, .7], [.8, .8])
394+
r1 = plt.Circle((20, 20), 100, transform=None)
395+
r2 = plt.Circle((.7, .5), .05)
396+
r3 = plt.Circle((4.5, .8), .55, transform=fig_test.dpi_scale_trans,
397+
facecolor='crimson')
398+
for a in [l1, l2, r1, r2, r3]:
399+
fig_test.add_artist(a)
400+
l2.remove()
401+
402+
ax2 = fig_ref.subplots()
403+
l1 = plt.Line2D([.2, .7], [.7, .7], transform=fig_ref.transFigure)
404+
r1 = plt.Circle((20, 20), 100, transform=None, clip_on=False, zorder=20)
405+
r2 = plt.Circle((.7, .5), .05, transform=fig_ref.transFigure)
406+
r3 = plt.Circle((4.5, .8), .55, transform=fig_ref.dpi_scale_trans,
407+
facecolor='crimson', clip_on=False, zorder=20)
408+
for a in [l1, r1, r2, r3]:
409+
ax2.add_artist(a)
410+
411+
386412
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires Python 3.6+")
387413
@pytest.mark.parametrize("fmt", ["png", "pdf", "ps", "eps", "svg"])
388414
def test_fspath(fmt, tmpdir):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.