Skip to content

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

Merged
merged 1 commit into from
Jul 19, 2018

Conversation

ImportanceOfBeingErnest
Copy link
Member

@ImportanceOfBeingErnest ImportanceOfBeingErnest commented May 11, 2018

PR Summary

As quickly discussed on gitter, an add_artist() of Figure may be useful in some cases.
This PR would add such method.

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)

Copy link
Member

@jklymak jklymak left a 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!

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``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

figure.transFigure?

Copy link
Member

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.

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
Copy link
Member

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.

@ImportanceOfBeingErnest ImportanceOfBeingErnest force-pushed the fig-add-artist branch 2 times, most recently from 9969fc4 to f1046f6 Compare May 12, 2018 00:02
@dstansby dstansby added this to the v3.0 milestone May 12, 2018

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
Copy link
Member

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``).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Broken sentence

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shorter: „Determines whether ...“

@ImportanceOfBeingErnest
Copy link
Member Author

It doesn't seem like the failing test is due to anything commited here.

@jklymak
Copy link
Member

jklymak commented May 14, 2018

You might need to rebase....? Looks like #11204 monkeyed w/ boilerplate... ping @anntzer

@ImportanceOfBeingErnest
Copy link
Member Author

Rebasing didn't change anything.

@tacaswell
Copy link
Member

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....

@tacaswell
Copy link
Member

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.

Copy link
Member

@efiring efiring left a 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.

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``
Copy link
Member

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.

Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"an" -> "a"

Copy link
Member

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
Copy link
Member

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
Copy link
Member

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

----------
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
Copy link
Member

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)
Copy link
Member

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).

Copy link
Member

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.

@ImportanceOfBeingErnest
Copy link
Member Author

ImportanceOfBeingErnest commented May 16, 2018

The difference between clip=False and clip=True can be observed when saving the figure with the bbox_inches="tight" option.

Default (i.e. clip=False)

import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (3,2)
plt.rcParams["figure.dpi"] = 72

fig, ax = plt.subplots()
c = plt.Circle((0,0),0.2)
fig.add_artist(c)
fig.savefig("test1.png", facecolor="azure", bbox_inches="tight")

image

With clip=True:

image

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. @efiring and @QuLogic, could you explain a bit more in detail what you have in mind concerning the clip option?

@efiring efiring dismissed their stale review May 16, 2018 19:00

My potential objections have been shown to be invalid.

@jklymak jklymak added status: needs rebase Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. labels Jul 5, 2018
@jklymak
Copy link
Member

jklymak commented Jul 5, 2018

This seems done, and can/should go in for 3.0. Needs a rebase and another approver...

@ImportanceOfBeingErnest ImportanceOfBeingErnest force-pushed the fig-add-artist branch 2 times, most recently from 1158162 to 6024bf3 Compare July 5, 2018 19:08
@ImportanceOfBeingErnest
Copy link
Member Author

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?

@jklymak jklymak requested a review from QuLogic July 13, 2018 22:54
@ImportanceOfBeingErnest ImportanceOfBeingErnest force-pushed the fig-add-artist branch 2 times, most recently from e60a0f3 to 0ade33e Compare July 13, 2018 23:23
@ImportanceOfBeingErnest
Copy link
Member Author

I needed to rebase yet again; now there is an error from circle/ci saying document not included in any doctree.

@jklymak
Copy link
Member

jklymak commented Jul 14, 2018

Yeah, unfortunately while the release candidate for 3.0 is cut, the whats-new should be moved to the actual whats-new.rst.

@ImportanceOfBeingErnest
Copy link
Member Author

Now I get Analysis Failed (could not build the merge commit), but I looking at the log, I don't see what's failing (which is probably due to me not knowing what to look for in this long log file).

@@ -381,6 +382,34 @@ def test_warn_cl_plus_tl():
assert not(fig.get_constrained_layout())


def test_add_artist():
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Member

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...

Copy link
Member Author

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.

Copy link
Member

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 😉

@tacaswell tacaswell merged commit ac58f1f into matplotlib:master Jul 19, 2018
@ImportanceOfBeingErnest ImportanceOfBeingErnest deleted the fig-add-artist branch February 17, 2019 23:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants