-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add fig.add_artist method #11234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add fig.add_artist method #11234
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great asside frm the small formatting issues! Thanks!
lib/matplotlib/figure.py
Outdated
Add any :class:`~matplotlib.artist.Artist` to the figure. | ||
|
||
If the added artist has no transform set to it previously, its | ||
transform will be set to ``transFigure``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
figure.transFigure
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think ideally this docstring would be numpydoc compliant. Most of figure.py
is not, but can't hurt to make this new one compliant.
lib/matplotlib/figure.py
Outdated
If the added artist has no transform set to it previously, its | ||
transform will be set to ``transFigure``. | ||
|
||
Use ``add_artist`` only in cases you really need to add an artist to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has a lot of "only" and "most often". Note a big reason to do this is if you don't want the artist in the tighbbox of the axes. Maybe:
Usually artists are added to axes objects using :meth:matplotlib.axes.Axes.add_artist
, but use this method in the rare cases that adding directly to the figure is necessary.
9969fc4
to
f1046f6
Compare
|
||
In case the added artist has no transform set previously, it will assume | ||
set it the figure transform (``fig.transFigure``). | ||
This new mathod may be useful for adding artists to figures without axes or to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
method
fig.add_artist(circ) | ||
|
||
In case the added artist has no transform set previously, it will assume | ||
set it the figure transform (``fig.transFigure``). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Broken sentence
lib/matplotlib/figure.py
Outdated
transform set to it previously, its transform will be set to | ||
``figure.transFigure``. | ||
clip : bool, optional, default ``False`` | ||
An optional parameter ``clip`` may be used to determine whether the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shorter: „Determines whether ...“
f1046f6
to
ec6df1d
Compare
It doesn't seem like the failing test is due to anything commited here. |
ec6df1d
to
2bdffec
Compare
Rebasing didn't change anything. |
try running boilerplate.py and commit the results. On the plus side, this means that test is working! On the down side it is a bit annoying.... |
I suspect what happened is that between when the tests on #11204 ran and it got merged something that changed pyplot got merged into master. |
2bdffec
to
1ab9b49
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the 'clip' kwarg can be removed.
lib/matplotlib/figure.py
Outdated
The artist to add to the figure. If the added artist has no | ||
transform set to it previously, its transform will be set to | ||
``figure.transFigure``. | ||
clip : bool, optional, default ``False`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this parameter for a Figure artist; I would not expect that anything could be drawn outside the Figure patch boundaries. I did a little test that seemed to confirm that even with the ps backend, so there is white space around the figure, a Figure.figimage is clipped, regardless of whether get_clip_on() returns True or False.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if you do savefig(bbox_inches='tight')
? If clipping is on, it'll clip the tight bbox (I think, or maybe it will when some of my PRs on this get merged). If clipping is off the figure will get larger...
@@ -0,0 +1,16 @@ | |||
Figure has an `~.figure.Figure.add_artist` method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"an" -> "a"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ~
cuts it to just add_artist
, so 'an' is correct here.
@@ -0,0 +1,16 @@ | |||
Figure has an `~.figure.Figure.add_artist` method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ~
cuts it to just add_artist
, so 'an' is correct here.
------------------------------------------------- | ||
|
||
A method `~.figure.Figure.add_artist` has been added to the | ||
:class:`~.figure.Figure` class, which allows to add artists directly to a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
allows to add artists -> allows artists to be added
lib/matplotlib/figure.py
Outdated
---------- | ||
artist : `~matplotlib.artist.Artist` | ||
The artist to add to the figure. If the added artist has no | ||
transform set to it previously, its transform will be set to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
set to it previously -> previously set
artist.set_transform(self.transFigure) | ||
|
||
if clip: | ||
artist.set_clip_path(self.patch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should always be passed, unless you can guarantee that the clip path was always False
, or it should default to None
if you really don't want to change the existing setting. That is, assuming it has an effect (re. @efiring's comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I think I misread this, and thought clip
was being passed down. Since it isn't, this doesn't really matter.
The difference between Default (i.e. clip=False)
With If people think this should be handled differently, I'm happy to change it. Unfortunately I'm not quite sure I understood the comments above. |
My potential objections have been shown to be invalid.
1ab9b49
to
00e0ac4
Compare
This seems done, and can/should go in for 3.0. Needs a rebase and another approver... |
1158162
to
6024bf3
Compare
I rebased. There was still a comment by @QuLogic, which I did not fully understand; so maybe he wants to take another look and either approve or clarify that issue? |
e60a0f3
to
0ade33e
Compare
I needed to rebase yet again; now there is an error from circle/ci saying |
Yeah, unfortunately while the release candidate for 3.0 is cut, the whats-new should be moved to the actual whats-new.rst. |
0ade33e
to
5d208e9
Compare
Now I get |
lib/matplotlib/tests/test_figure.py
Outdated
@@ -381,6 +382,34 @@ def test_warn_cl_plus_tl(): | |||
assert not(fig.get_constrained_layout()) | |||
|
|||
|
|||
def test_add_artist(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may want to (but don't have to) use #11408 for the test, which has the benefit of directly taking care of all three output formats and generating failure diffs when needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, did that. Locally, the svg test always skips though. Not sure about the reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You just don't have inkscape installed. I wouldn't worry about it...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have several inkscape versions installed, maybe that's the problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...sorry, shouldn't have assumed; rephrase - inkscape wasn't callable for some reason 😉
5d208e9
to
e4e5a0b
Compare
e4e5a0b
to
b6326cc
Compare
PR Summary
As quickly discussed on gitter, an
add_artist()
ofFigure
may be useful in some cases.This PR would add such method.
PR Checklist
, with examples if plot related