| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9ec4b95 commit 9969fc4
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,16 @@ | |||
| 1 | + Figure has an `~.figure.Figure.add_artist` method | ||
| 2 | + ------------------------------------------------- | ||
| 3 | + | ||
| 4 | + A method `~.figure.Figure.add_artist` has been added to the | ||
| 5 | + :class:`~.figure.Figure` class, which allows to add artists directly to a | ||
| 6 | + figure. E.g. | ||
| 7 | + | ||
| 8 | + :: | ||
| 9 | + | ||
| 10 | + circ = plt.Circle((.7, .5), .05) | ||
| 11 | + fig.add_artist(circ) | ||
| 12 | + | ||
| 13 | + In case the added artist has no transform set previously, it will assume | ||
| 14 | + set it the figure transform (``fig.transFigure``). | ||
| 15 | + This new mathod may be useful for adding artists to figures without axes or to | ||
| 16 | + easily position static elements in figure coordinates. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1038,6 +1038,44 @@ def fixlist(args): | |||
| 1038 | 1038 | key = fixlist(args), fixitems(kwargs.items()) | |
| 1039 | 1039 | return key | |
| 1040 | 1040 | ||
| 1041 | + | ||
| 1042 | + def add_artist(self, artist, clip=False): | ||
| 1043 | + """ | ||
| 1044 | + Add any :class:`~matplotlib.artist.Artist` to the figure. | ||
| 1045 | + | ||
| 1046 | + Usually artists are added to axes objects using | ||
| 1047 | + :meth:`matplotlib.axes.Axes.add_artist`, but use this method in the | ||
| 1048 | + rare cases that adding directly to the figure is necessary. | ||
| 1049 | + | ||
| 1050 | + Parameters | ||
| 1051 | + ---------- | ||
| 1052 | + artist : `~matplotlib.artist.Artist` | ||
| 1053 | + The artist to add to the figure. If the added artist has no | ||
| 1054 | + transform set to it previously, its transform will be set to | ||
| 1055 | + ``figure.transFigure``. | ||
| 1056 | + clip : bool, optional, default ``False`` | ||
| 1057 | + An optional parameter ``clip`` may be used to determine whether the | ||
| 1058 | + added artist should be clipped by the figure patch. Default is | ||
| 1059 | + *False*, i.e. no clipping. | ||
| 1060 | + | ||
| 1061 | + Returns | ||
| 1062 | + ------- | ||
| 1063 | + artist : The added `~matplotlib.artist.Artist` | ||
| 1064 | + """ | ||
| 1065 | + artist.set_figure(self) | ||
| 1066 | + self.artists.append(artist) | ||
| 1067 | + artist._remove_method = self.artists.remove | ||
| 1068 | + | ||
| 1069 | + if not artist.is_transform_set(): | ||
| 1070 | + artist.set_transform(self.transFigure) | ||
| 1071 | + | ||
| 1072 | + if clip: | ||
| 1073 | + artist.set_clip_path(self.patch) | ||
| 1074 | + | ||
| 1075 | + self.stale = True | ||
| 1076 | + return artist | ||
| 1077 | + | ||
| 1078 | + | ||
| 1041 | 1079 | def add_axes(self, *args, **kwargs): | |
| 1042 | 1080 | """ | |
| 1043 | 1081 | Add an axes to the figure. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | import sys | |
| 2 | 2 | import warnings | |
| 3 | + import io | ||
| 3 | 4 | ||
| 4 | 5 | from matplotlib import rcParams | |
| 5 | 6 | from matplotlib.testing.decorators import image_comparison | |
@@ -373,6 +374,34 @@ def test_figure_repr(): | |||
| 373 | 374 | assert repr(fig) == "<Figure size 100x200 with 0 Axes>" | |
| 374 | 375 | ||
| 375 | 376 | ||
| 377 | + def test_add_artist(): | ||
| 378 | + fig, ax = plt.subplots(dpi=100) | ||
| 379 | + l1 = plt.Line2D([.2, .7], [.7, .7]) | ||
| 380 | + l2 = plt.Line2D([.2, .7], [.8, .8]) | ||
| 381 | + r1 = plt.Circle((20, 20), 100, transform=None) | ||
| 382 | + r2 = plt.Circle((.7, .5), .05) | ||
| 383 | + r3 = plt.Circle((4.5, .8), .55, transform=fig.dpi_scale_trans, | ||
| 384 | + facecolor='crimson') | ||
| 385 | + for a in [l1, l2, r1, r2, r3]: | ||
| 386 | + fig.add_artist(a) | ||
| 387 | + l2.remove() | ||
| 388 | + buf1 = io.BytesIO() | ||
| 389 | + fig.savefig(buf1) | ||
| 390 | + | ||
| 391 | + fig2, ax2 = plt.subplots(dpi=100) | ||
| 392 | + l1 = plt.Line2D([.2, .7], [.7, .7], transform=fig2.transFigure) | ||
| 393 | + r1 = plt.Circle((20, 20), 100, transform=None, clip_on=False, zorder=20) | ||
| 394 | + r2 = plt.Circle((.7, .5), .05, transform=fig2.transFigure) | ||
| 395 | + r3 = plt.Circle((4.5, .8), .55, transform=fig.dpi_scale_trans, | ||
| 396 | + facecolor='crimson', clip_on=False, zorder=20) | ||
| 397 | + for a in [l1, r1, r2, r3]: | ||
| 398 | + ax2.add_artist(a) | ||
| 399 | + buf2 = io.BytesIO() | ||
| 400 | + fig2.savefig(buf2) | ||
| 401 | + | ||
| 402 | + assert buf1.getvalue() == buf2.getvalue() | ||
| 403 | + | ||
| 404 | + | ||
| 376 | 405 | @pytest.mark.skipif(sys.version_info < (3, 6), reason="requires Python 3.6+") | |
| 377 | 406 | @pytest.mark.parametrize("fmt", ["png", "pdf", "ps", "eps", "svg"]) | |
| 378 | 407 | def test_fspath(fmt, tmpdir): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments