[AstroPy] plot of data and residuals

Jonathan Slavin jslavin at cfa.harvard.edu
Tue Aug 10 15:11:48 EDT 2010


Thanks for all the suggestions regarding my question on plotting data
and a model with residuals.  Here is the simple function I wrote which
gets the jobs done for me.  It's not too flexible, but it's what I
wanted.

Jon


def plot_model(xdata,ydata,edata,ymod,dcolor='b',mcolor='g'):
    """
    Plot data and a model on one plot and the residuals on a plot stacked
    below.  Returns the figure object and an array of the axes objects for the
    main plot and the residuals plot.

    f,a = plot_model(xdata,ydata,edata,ymod,dcolor='b',mcolor='g')

    where xdata, ydata, edata are the x, y and error values for the data and
    ymod are the model values.  The keyword arguments dcolor (color of data
    points) and mcolor (color of model line) are accepted.
    """
    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.2])
    # I prefer not to have horizontal "caps" on my error bars
    ax1.errorbar(xdata,ydata,yerr=edata,marker='o',capsize=0,color=dcolor)
    ax1.plot(xdata,ymod,color=mcolor)
    xticklabels = ax1.get_xticklabels()
    plt.setp(xticklabels,visible=False)
    resid = ydata - ymod
    ax2.errorbar(xdata,resid,yerr=edata,marker='o',capsize=0,color=dcolor)
    ax2.axhline(0.,color='k')
    axes = np.array([ax1,ax2])
    return fig, axes




More information about the AstroPy mailing list