Skip to content
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

How to easily set the location and size of a sub_axes? #8986

Closed
gepcel opened this issue Aug 4, 2017 · 8 comments
Closed

How to easily set the location and size of a sub_axes? #8986

gepcel opened this issue Aug 4, 2017 · 8 comments

Comments

@gepcel
Copy link
Contributor

gepcel commented Aug 4, 2017

The problem comes from me drawwing a map via cartopy. I want to add a little sub_axes on the lower_right corner of the main axes. But it seems hard to set the location and size of the sub_axes.

The first thing I tried was fig.add_axes(). But it doesn't accept transform args, while the document says it should. Without the transform args, the location (left, bottom) and size (width, height) wasn't on the transform of ax.

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)
ax2 = fig.add_axes([0.8, 0, 0.2, 0.2], transform=ax.transAxes, projection='rectilinear') 

The second I tried was inset_axes, which was very easy to use. But it doesn't accept projection args.

from mpl_toolkits.axes_grid.inset_locator import inset_axes
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)

# The following line doesn't work
ax2 = inset_axes(ax, width='20%', height='20%', axes_kwargs={'projection': 'rectilinear'})
# Doesn't work neither:
ax2 = inset_axes(ax, width='20%', height='20%', projection='rectilinear')

I may get what I want by ax.get_position(), the set the position according it.

@jklymak
Copy link
Member

jklymak commented Aug 4, 2017

fig, ax = plt.subplots(1, 1)
box = matplotlib.transforms.Bbox.from_bounds(0.8, 0, 0.2, 0.2)
ax2 = fig.add_axes(fig.transFigure.inverted().transform_bbox( ax.transAxes.transform_bbox(box)))

but it is a bit roundabout...

@gepcel
Copy link
Contributor Author

gepcel commented Aug 5, 2017

This works without a projection. But with a projection is doesn't. Like following:

import cartopy.crs as ccrs
fig, ax = plt.subplots(1, 1, subplot_kw={'projection': ccrs.PlateCarree()})
box = mpl.transforms.Bbox.from_bounds(0.8, 0.8, 0.2, 0.2)
ax2 = fig.add_axes(fig.transFigure.inverted().transform_bbox(ax.transAxes.transform_bbox(box)), projection=ccrs.PlateCarree())

I got this:
image

I tried to give a miminal example, sorry for the cartopy import.

@jklymak
Copy link
Member

jklymak commented Aug 5, 2017 via email

@gepcel
Copy link
Contributor Author

gepcel commented Aug 5, 2017

@jklymak Thanks.
Is there a way I can anchor the top right point ([1.0, 1.0]) of ax2 at the top right corner of ax1 ([1.0, 1.0])?
Then the position or size of ax2 won't matter that much.

@jklymak
Copy link
Member

jklymak commented Aug 5, 2017 via email

@QuLogic
Copy link
Member

QuLogic commented Aug 6, 2017

The plot you obtained is "correct" in that those really are the limits of the axes space, but Cartopy has turned off the axis spines and ticks and is drawing its own boundary which is not necessarily "full" (this is how you can get complicated maps like Interrupted Goode Homolosine).

If you want to do something based on that boundary, you can do something like call ax.get_extent(), and calculate some relative position based on that. It will be in data space, so you would need to apply a transform into figure coords (possibly ax.transAxes, but I have not tried.)

@jklymak
Copy link
Member

jklymak commented Aug 6, 2017

One wonders if cartopy could return the extent of the spines somehow. I've been thinking about layout management and lining up spines is a pretty important part of layout.

@gepcel
Copy link
Contributor Author

gepcel commented Aug 7, 2017

I may have figured out an answer. For someone who may run into the same problem, here is a working solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants