Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Axes.inset_axes: enable Axes subclass creation#22608

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
timhoffm merged 1 commit intomatplotlib:mainfromrcomer:inset-add_axes
Apr 7, 2022

Conversation

rcomer
Copy link
Member

@rcomerrcomer commentedMar 5, 2022
edited
Loading

PR Summary

Closes#22630

ModifyAxes.inset_axes so it can accept thepolar,projection andaxes_class keywords, and therefore return subclasses ofAxes. My target use-case is with Cartopy, so we can now adaptthis gallery example to do something like

importcartopy.crsasccrsimportmatplotlib.pyplotaspltimportnumpyasnpdefsample_data(shape=(73,145)):"""Return ``lons``, ``lats`` and ``data`` of some fake data."""nlats,nlons=shapelats=np.linspace(-np.pi/2,np.pi/2,nlats)lons=np.linspace(0,2*np.pi,nlons)lons,lats=np.meshgrid(lons,lats)wave=0.75* (np.sin(2*lats)**8)*np.cos(4*lons)mean=0.5*np.cos(2*lats)* ((np.sin(2*lats))**2+2)lats=np.rad2deg(lats)lons=np.rad2deg(lons)data=wave+meanreturnlons,lats,datalons,lats,data=sample_data()ax=plt.subplot(111,projection=ccrs.PlateCarree())ax.contourf(lons,lats,data)ax.coastlines()axins=ax.inset_axes([0.5,0.03,0.47,0.47],projection=ccrs.PlateCarree())axins.contourf(lons,lats,data)axins.set_xlim(-10,5)axins.set_ylim(50,60)axins.coastlines()box,lines=ax.indicate_inset_zoom(axins,edgecolor="black")plt.show()

cartopy_inset

PR Checklist

Tests and Styling

  • Has pytest style unit tests (andpytest passes).I'm unsure what extra tests should be added for this. Advice welcome!
  • IsFlake 8 compliant (installflake8-docstrings and runflake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry indoc/users/next_whats_new/ (follow instructions in README.rst there).
  • [N/A] API changes documented indoc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs shouldbuild without error).

@rcomerrcomer changed the titleUse fig.add_axes within Axes.inset_axesUse Figure.add_axes within Axes.inset_axesMar 5, 2022
@rcomer
Copy link
MemberAuthor

Looks like I have brokenone test consistently across platforms. I will have to come back to this another day.

@jklymak
Copy link
Member

Inset axes are explicitly children of their parent axes so please don't also add the figure as a parent. I'm not quite sure what the best way to get a projection into an inset_axes is...

@greglucas
Copy link
Contributor

This seems like a useful thing to have available in some form. Might be worth having a look at#22060 and whetherhttps://github.com/matplotlib/matplotview would work with GeoAxes too...

Ahh, the axes being a child of the figure or not would actually explain why mpl_toolkit inset_axes warn withtight_layout() (because they are added!), but standard inset_axes don't. (xref:#22611)

parent_axes.figure.add_axes(inset_axes)

The easiest way might be to allowaxes_class to be passed in to theinset_axes() method, similar to mpl_toolkits and figure's add_axes methods.

axes_class : `matplotlib.axes.Axes` type, default: `.HostAxes`
The type of the newly created inset axes.
axes_kwargs : dict, optional
Keyword arguments to pass to the constructor of the inset axes.
Valid arguments include:

However, there might be some more surgery needed outside of AxesGrid to support the projection or other keyword args.

axes_class : subclass type of `~.axes.Axes`, optional
The `.axes.Axes` subclass that is instantiated. This parameter
is incompatible with *projection* and *polar*. See
:ref:`axisartist_users-guide-index` for examples.

@rcomer
Copy link
MemberAuthor

Thanks for the feedback. I did have a hunch that it couldn't really be this simple!

Inset axes are explicitly children of their parent axes so please don't also add the figure as a parent.

I'm afraid I don't have the background knowledge to understand this. Is there somewhere I can read up on the matplotlib parent/child relationships and why they matter? I guess the comment abouttight_layout gives a clue.

matplotlibview

Looks useful - thanks! I will have a play with that at some point.

The easiest way might be to allow axes_class to be passed in to the inset_axes() method, similar to mpl_toolkits and figure's add_axes methods.

I'm not so keen on that API as it seems inconsistent with how the user is usually encouraged to create their Axes instances. But I guess it was done in those other functions for a reason.

Anyway it seems that whatever the answer is, it's not this. So for now I will just close this.

@rcomerrcomer closed thisMar 6, 2022
@jklymak
Copy link
Member

@rcomer, maybe convert this to an issue? I think it is reasonable to ask that inset_axes accepts projections, I'm just not sure off the top of my head how to do it (I have limited Matplotlib (and internet) bandwidth at the moment.

rcomer reacted with thumbs up emoji

@rcomer
Copy link
MemberAuthor

maybe convert this to an issue?

Issue now at#22630. Thanks!

@rcomerrcomer reopened thisMar 18, 2022
@rcomerrcomer marked this pull request as draftMarch 18, 2022 16:12
@rcomer
Copy link
MemberAuthor

The important functionality seems to be contained withinFigure._process_projection_requirements, so I tried using that directly. The Cartopy example still works, so I just wanted to see how it would fly with the CI.

@rcomerrcomer closed thisMar 25, 2022
@jklymak
Copy link
Member

@rcomer, did this not work? It looks like something similarshould work, and it is certainly reasonable functionality...

rcomer reacted with thumbs up emoji

@rcomer
Copy link
MemberAuthor

@jklymak yes the CI all seemed fine and it works for my Cartopy example. However at#22630@mylasem said they were working on it, so I thought I would leave it to them for now. I'd be happy to pick this up again at some point if@mylasem's version doesn't land for whatever reason.

@jklymak
Copy link
Member

In general, Matplotlib does not have a "reserve an issue" policy, and this PR predates@mylasem 's comment. I think they should work within the context of this PR if at all possible.

@rcomerrcomer reopened thisMar 31, 2022
@rcomerrcomer changed the titleUse Figure.add_axes within Axes.inset_axesAxes.inset_axes: enable Axes subclass creationMar 31, 2022
@rcomer
Copy link
MemberAuthor

OK, since@mylasem has not commented further and I had some time, I have:

  • Added some basic tests to verify that the expected type is returned for our three methods of specifying it.
  • Updated theAxes.inset_axes docstring with the keyword descriptions - this was a straight copy/paste from theFigure.add_axes docstring, and then added the word "inset".
  • Updated the what's new entry (and the PR title and OP) to reflect that we're not going viaFigure.add_axes.

@@ -6614,6 +6614,30 @@ def test_zoom_inset():
axin1.get_position().get_points(), xx, rtol=1e-4)


def test_inset_polar():
from matplotlib.projections.polar import PolarAxes
Copy link
MemberAuthor

@rcomerrcomerMar 31, 2022
edited
Loading

Choose a reason for hiding this comment

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

I imported the class within the test as I noticed another test in this module does that. If that's not necessary, then I think we could parametrize these three tests into one.

Copy link
Member

Choose a reason for hiding this comment

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

I do not see any reason to do this, I think pulling all of the imports to the top is OK.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I moved the imports to the top, but in the end couldn't convince myself it was worth parametrizing the two small tests that are still the same: since they're so small there isn'tmuch repetition going on and I suspect readability is better as they are than with a parametrized version.

@rcomerrcomer marked this pull request as ready for reviewMarch 31, 2022 09:33
@tacaswelltacaswell added this to thev3.6.0 milestoneApr 1, 2022
@rcomerrcomer closed thisApr 4, 2022
@rcomerrcomer reopened thisApr 4, 2022
Copy link
Member

@tacaswelltacaswell left a comment

Choose a reason for hiding this comment

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

I am 👍 with this going in as-is.

Moving to the mpl20 style would be good as would refactoring the test, but neither is worth holding up the PR over.

@rcomer
Copy link
MemberAuthor

Thanks@tacaswell, I am happy to follow those up tomorrow when I am back at my keyboard. Should I also squash the commits down to one?

@tacaswell
Copy link
Member

Should I also squash the commits down to one?

Yes please (and if you replace the image CI without squashing CI will fail).

rcomer reacted with thumbs up emoji

@timhoffmtimhoffm merged commit9caa261 intomatplotlib:mainApr 7, 2022
@rcomer
Copy link
MemberAuthor

Thanks All for your guidance!

@rcomerrcomer deleted the inset-add_axes branchApril 7, 2022 15:14
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@tacaswelltacaswelltacaswell approved these changes

@QuLogicQuLogicQuLogic left review comments

@jklymakjklymakjklymak left review comments

@timhoffmtimhoffmtimhoffm approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
v3.6.0
Development

Successfully merging this pull request may close these issues.

[ENH]: enable passing of projection keyword to Axes.inset_axes
6 participants
@rcomer@jklymak@greglucas@tacaswell@QuLogic@timhoffm

[8]ページ先頭

©2009-2025 Movatter.jp