[AstroPy] plot of data and residuals

Derek Homeier derek@astro.physik.uni-goettingen...
Tue Aug 10 10:00:15 CDT 2010


> The easiest way to do something like this is just to define your  
> axes size relative to the figure:
> import matplotlib as mpl
> mpl.use('Agg')
> import matplotlib.pyplot as plt
> fig = plt.figure()
> ax1 = fig.add_axes([0.1,0.35,0.8,0.50])
> ax2 = fig.add_axes([0.1,0.1,0.8,0.20])
> fig.savefig('data_residuals.png')
> The format of the axes specifications is [xmin, ymin, dx, dy]. You  
> can then use ax1 and ax2 to plot in the respective axes.

I found the new subplot utility provided with matplotlib 1.0.0 also  
gives a number of
useful hints to start with, e.g. it lets you do

fig, axarr = plt.subplots(2, sharex=True)
axarr[0].plot(x, y)
axarr[1].plot(x, sigy)

This still produces independent plot frames and x axis labels, so it's  
quite be what you (or I) might envisage yet, but it automatically  
keeps the
x ranges synchronised, and the source could  be a good place to start  
further refinements.

You could implement the same functionality in Tom's example by passing  
'sharex' keyword to add_axes as below. It's also possible to get the  
axes adjacent,
which lets you hide the x labels of ax1, but unfortunately you can't  
really get rid
of them (since the x axis is now shared, any tampering with the  
xtick[labels] will
affect ax2 in the same way):

fig = plt.figure()
ax1 = fig.add_axes([0.1,0.3,0.8,0.6])
ax2 = fig.add_axes([0.1,0.1,0.8,0.20], sharex=ax1)

Note that logarithmic axis mappings should be done individually, e.g.
axarr[0].semilogx(); axarr[1].semilogx()

otherwise the labels may get mangled.

Derek Homeier                         Institut für Astrophysik Göttingen
Georg-August-Universität              Phone:             +49 551 39-7980
Friedrich-Hund-Platz 1                Fax:               +49 551 39-5043
D-37077 Göttingen, Germany            Feet:                     E.04.104
Web:                   http://www.astro.physik.uni-goettingen.de/~derek/

More information about the AstroPy mailing list