Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Allow user to specify an axis instead of a figure #88

Open
wants to merge 5 commits into
from

Conversation

Projects
None yet
3 participants
Member

keflavich commented Feb 24, 2013

I found myself wanting to use matplotlib/pylab's built-in subplot routine and then have aplpy plot on one of those axes, so I tried implementing that. Unfortunately, aplpy requires a ParasiteAxes/HostAxes instance. I tried creating one (see commented out code), but right now this just raises an exception if the wrong type of axis is specified.

This may need to be rebased against the latest astropy refactor...

Contributor

wkerzendorf commented Apr 25, 2013

+1 to this. I often wanted to do the same.

Member

keflavich commented Apr 25, 2013

Thanks for bringing it back to my attention. I rebased, we'll see if the tests pass now.

Owner

astrofrog commented Apr 25, 2013

Just to be clear, is there any advantage to doing this rather than simply specifying subplot=[0.1, 0.1, 0.9, 0.9] or subplot=(2,2,1) when calling FITSFigure?

Member

keflavich commented Apr 25, 2013

If you have an existing figure axis "in-hand", you don't need to overwrite it. I find it useful when I want the matplotlib defaults for the figure window, or when I'm working interactively or in scripts and don't know the figure number but have direct access to the axis variable.

There's probably little advantage over the subplot= keyword, but it does give you access to matplotlib's other axis creation methods. You could do F = aplpy.FITSFigure('file.fits', axis=pl.subplot(111)) or something like axis=pl.subplot2grid((2,2),(0, 0)).

Owner

astrofrog commented Apr 27, 2013

I guess the issue is that this requires a very specialized kind of axes as you point out (the parasite/host axes) so it's not clear to me that one could use e.g. pl.subplot2grid((2,2),(0, 0)) - although one option is simply to check the extent of the axes, make the one the user gave invisible, and create a new one with the correct subplot parameters (since all axes can be specified with the [x, y, dx, dy] syntax. What do you think?

Member

keflavich commented Apr 27, 2013

Oh, yes, good point, I'd forgotten about that. Really, I think it ought to be possible to generate a ParasiteAxis instance from a regular axis instance - but your workaround sounds pretty solid for a start. Maybe I should raise that matplotlib issue further upstream, but in the meantime I'll implement the workaround you suggest.

@astrofrog astrofrog and 1 other commented on an outdated diff May 14, 2013

aplpy/aplpy.py
@@ -278,6 +280,18 @@ def __init__(self, data, hdu=0, figure=None, subplot=(1, 1, 1),
self._ax1 = mpltk.HostAxes(self._figure, subplot, adjustable='datalim')
elif type(subplot) == tuple and len(subplot) == 3:
self._ax1 = mpltk.SubplotHost(self._figure, *subplot)
+ elif axis is not None:
+ if hasattr(axis,'toggle_axisline'):
+ self._ax1 = axis
+ else:
+ # Hack-ish; there must be a better way
+ lower_corner = axis.get_position()[0,:]
+ extent = axis.get_position()[1:] - lower_corner
+ cornerpars = np.concatenate([lower_corner,extent])
+ self._figure = axis.get_figure()
+ self._ax1 = mpltk.HostAxis(self._figure, cornerpars)
@astrofrog

astrofrog May 14, 2013

Owner

Is there a reason this is HostAxis instead of HostAxes?

@keflavich

keflavich May 14, 2013

Member

no, typo

@astrofrog astrofrog and 1 other commented on an outdated diff May 14, 2013

aplpy/aplpy.py
@@ -278,6 +280,18 @@ def __init__(self, data, hdu=0, figure=None, subplot=(1, 1, 1),
self._ax1 = mpltk.HostAxes(self._figure, subplot, adjustable='datalim')
elif type(subplot) == tuple and len(subplot) == 3:
self._ax1 = mpltk.SubplotHost(self._figure, *subplot)
+ elif axis is not None:
+ if hasattr(axis,'toggle_axisline'):
@astrofrog

astrofrog May 14, 2013

Owner

Would it not make more sense to check if the axis is an instance of HostAxes?

@keflavich

keflavich May 14, 2013

Member

No, I don't think so - any type of axis that has the toggle_axisline feature will work (though perhaps there are other features to check for?). I'd rather check for features than restrict to particular parent classes, as I'd hope that some time in the future other mpl axis classes will have these features. That said, maybe for now it would be better to restrict to classes.

Owner

astrofrog commented May 14, 2013

Could you add simple tests for the different cases to make sure it doesn't crash?

Member

keflavich commented May 14, 2013

On my to-do list. I may need a reminder to add the tests.

keflavich added some commits Feb 24, 2013

Member

keflavich commented Jan 27, 2017

I added a small test. I'm not sure it's enough... but it's something.

@astrofrog astrofrog added the Feature label Jul 29, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment