Last modified: 12 December 2019

How can I include multiple datasets in the same plot? [Updated]


In the following we assume you have two data sets loaded with ids of 1 (i.e. the default) and 2, that you have done the following

sherpa> get_data_plot_prefs()["xlog"] = True
sherpa> get_data_plot_prefs()["ylog"] = True

and that fits have been made to both data sets.

If you wish to display multiple datasets in the same plot then you have to set the overplot argument to True; for example if you have two datasets with ids of 1 and 2 then:

sherpa> plot_data()
sherpa> plot_data(2, overplot=True)

will display the two data sets in the same plot, with the second dataset drawn in orange. Note that the plot title is not changed when extra datasets are added.

Overplotting different data sets

[The two data sets are shown on the same graph (of Counts/sec/kev versus Energy (keV)). The two data sets have very-different continuum shapes.]
[Print media version: The two data sets are shown on the same graph (of Counts/sec/kev versus Energy (keV)). The two data sets have very-different continuum shapes.]

Overplotting different data sets

You can overplot data from the same data set; for instance if you want the residuals shown on the same graph as the data you could say

sherpa> plot_fit(ylog=False)
sherpa> plot_resid(overplot=True, marker='s', markersize=3)
sherpa> ax = plt.hca()
sherpa> kwargs = {'horizontalalignment': 'right', 'transform': ax.transAxes}
sherpa> plt.text(0.9, 0.9, 'Data', color='blue', **kwargs)
sherpa> plt.text(0.9, 0.8, 'Residuals', color='green', **kwargs)
[NOTE]
New in CIAO 4.12

New in CIAO 4.12 is the ability to supply plot options in the plot_* call, which is used here to plot the residuals as small square markers.

Overplotting residuals

[The residuals are shown on the same plot as the fit (data and models).]
[Print media version: The residuals are shown on the same plot as the fit (data and models).]

Overplotting residuals

The residuals are drawn with a small square symbol (rather than the default which is a small circular symbol) to help distinguish them from the data.

The labels have been added using "plot normalized" coordinates - where 0,0 is the bottom-left of the plot and 1,1 the top-right - rather than data coordinates, which is why the transform argument was used in the plt.text call.

You can overlay fits, as the matplotlib backend will automatically cycle colors:

sherpa> plot_fit(1)
sherpa> plot_fit(2, overplot=True)

Overplotting fits

[The data and model from the two fits are drawn onthe same graph. Fortunately as the spectral shape is different it is not too hard to separate the two.]
[Print media version: The data and model from the two fits are drawn onthe same graph. Fortunately as the spectral shape is different it is not too hard to separate the two.]

Overplotting fits

You can even combine data with different axes; for example in the following we add a second Y axis to the top plot to show how the ARF varies with energy:

sherpa> plot_fit_ratio(1)
sherpa> plt.xlim(0.4, 8)
sherpa> fig = plt.gcf()
sherpa> plt.sca(fig.axes[0])
sherpa> ax2 = plt.twinx()
sherpa> plot_arf(1, overplot=True, color='gray', linestyle='dotted')

Overplotting the ARF

[The top plot fills about 2/3 of the vertical area and shows data and model (using the left Y axis) and ARF (using the right Y axis) as a function of energy. The bottom plot has the same X axis and shows the ratio of the data to the model, and fills the remaining 1/3 of the space.]
[Print media version: The top plot fills about 2/3 of the vertical area and shows data and model (using the left Y axis) and ARF (using the right Y axis) as a function of energy. The bottom plot has the same X axis and shows the ratio of the data to the model, and fills the remaining 1/3 of the space.]

Overplotting the ARF

The plot_fit_ratio() function is new in CIAO 4.12.