| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 250c33e commit b6326cc
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -190,6 +190,7 @@ specify a number that is close (i.e. ``ax.title.set_position(0.5, 1.01)``) | |||
| 190 | 190 | and the title will not be moved via this algorithm. | |
| 191 | 191 | ||
| 192 | 192 | ||
| 193 | + | ||
| 193 | 194 | New convenience methods for GridSpec | |
| 194 | 195 | ------------------------------------ | |
| 195 | 196 | ||
@@ -210,6 +211,23 @@ now call `.Figure.add_gridspec` and for the latter `.SubplotSpec.subgridspec`. | |||
| 210 | 211 | fig.add_subplot(gssub[0, i]) | |
| 211 | 212 | ||
| 212 | 213 | ||
| 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 | + | ||
| 213 | 231 | ||
| 214 | 232 | ||
| 215 | 233 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1048,6 +1048,42 @@ def fixlist(args): | |||
| 1048 | 1048 | key = fixlist(args), fixitems(kwargs.items()) | |
| 1049 | 1049 | return key | |
| 1050 | 1050 | ||
| 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 | + | ||
| 1051 | 1087 | @docstring.dedent_interpd | |
| 1052 | 1088 | def add_axes(self, *args, **kwargs): | |
| 1053 | 1089 | """ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ | |||
| 2 | 2 | import warnings | |
| 3 | 3 | ||
| 4 | 4 | from matplotlib import rcParams | |
| 5 | - from matplotlib.testing.decorators import image_comparison | ||
| 5 | + from matplotlib.testing.decorators import image_comparison, check_figures_equal | ||
| 6 | 6 | from matplotlib.axes import Axes | |
| 7 | 7 | from matplotlib.ticker import AutoMinorLocator, FixedFormatter | |
| 8 | 8 | import matplotlib.pyplot as plt | |
@@ -381,6 +381,32 @@ def test_warn_cl_plus_tl(): | |||
| 381 | 381 | assert not(fig.get_constrained_layout()) | |
| 382 | 382 | ||
| 383 | 383 | ||
| 384 | + @check_figures_equal(extensions=["png", "pdf", "svg"]) | ||
| 385 | + def test_add_artist(fig_test, fig_ref): | ||
| 386 | + fig_test.set_dpi(100) | ||
| 387 | + fig_ref.set_dpi(100) | ||
| 388 | + | ||
| 389 | + ax = fig_test.subplots() | ||
| 390 | + l1 = plt.Line2D([.2, .7], [.7, .7]) | ||
| 391 | + l2 = plt.Line2D([.2, .7], [.8, .8]) | ||
| 392 | + r1 = plt.Circle((20, 20), 100, transform=None) | ||
| 393 | + r2 = plt.Circle((.7, .5), .05) | ||
| 394 | + r3 = plt.Circle((4.5, .8), .55, transform=fig_test.dpi_scale_trans, | ||
| 395 | + facecolor='crimson') | ||
| 396 | + for a in [l1, l2, r1, r2, r3]: | ||
| 397 | + fig_test.add_artist(a) | ||
| 398 | + l2.remove() | ||
| 399 | + | ||
| 400 | + ax2 = fig_ref.subplots() | ||
| 401 | + l1 = plt.Line2D([.2, .7], [.7, .7], transform=fig_ref.transFigure) | ||
| 402 | + r1 = plt.Circle((20, 20), 100, transform=None, clip_on=False, zorder=20) | ||
| 403 | + r2 = plt.Circle((.7, .5), .05, transform=fig_ref.transFigure) | ||
| 404 | + r3 = plt.Circle((4.5, .8), .55, transform=fig_ref.dpi_scale_trans, | ||
| 405 | + facecolor='crimson', clip_on=False, zorder=20) | ||
| 406 | + for a in [l1, r1, r2, r3]: | ||
| 407 | + ax2.add_artist(a) | ||
| 408 | + | ||
| 409 | + | ||
| 384 | 410 | @pytest.mark.skipif(sys.version_info < (3, 6), reason="requires Python 3.6+") | |
| 385 | 411 | @pytest.mark.parametrize("fmt", ["png", "pdf", "ps", "eps", "svg"]) | |
| 386 | 412 | def test_fspath(fmt, tmpdir): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments