Last modified: 15 December 2025

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


Sherpa provides some support for plotting multiple datasets together, as discussed here. It is also possible to add data using the plotting backend, such as with the Matplotlib examples.

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> set_xlog()
sherpa> set_ylog()

and that fits have been made to both data sets.

[New] New in CIAO 4.18 is the ability to plot multiple datasets with a single plot_xxx call. This allows you to say:

sherpa> plot_data([1, 2], alpha=0.6)

which displays 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.

Figure 1: 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.]

Figure 1: Overplotting different data sets

The two data set are drawn partially transparent; the alpha option goes from 0 (transparent) to 1 (fully opaque).

If you want to add different data - e.g. overlay the residual data on a fit plot - or want to use different plot options for the different datasets then you should add overplot=False to the plot_xxx call, or calls. As an example, 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', markerfacecolor='white', markersize=3, linestyle='none', alpha=0.5)

Figure 2: 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).]

Figure 2: 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.

You can overlay fits, with the choice of colors depending on the plot backend. So either:

sherpa> plot_fit([1, 2], alpha=0.5)

or

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

will create Figure 3:

Figure 3: Overplotting fits

[The data and model from the two fits are drawn on the 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 on the same graph. Fortunately as the spectral shape is different it is not too hard to separate the two.]

Figure 3: Overplotting fits

Plot options can be applied to all datasets, or a list can be used to set values for each dataset. So

sherpa> plot_data([1, 2], color="black", alpha=0.5)

draws both data sets in a gray-ish color:

Figure 4: Using the same color for each dataset

[The two plots are displayed in gray (well, a partially-opaque black).]
[Print media version: The two plots are displayed in gray (well, a partially-opaque black).]

Figure 4: Using the same color for each dataset

and this has different colors and opacities, but the same line style:

sherpa> plot_data([1, 2], color=["orange", "pink"], alpha=[0.5, 1], linestyle="solid", xlog=False, ylog=False)

Figure 5: Using different colors

[The two plots are displayed in orange or pink, both connected with solid lines.]
[Print media version: The two plots are displayed in orange or pink, both connected with solid lines.]

Figure 5: Using different colors

The plots which display multiple components - so plot_fit and variants - deal with values per "plot", so in the following the green color is applied to both the data and fit for the first dataset and brown for the second:

sherpa> plot_fit([1, 2], color=["green", "brown"])

Figure 6: Using different colors for fit-style plots

[The data and fit for the first dataset is in green, and that for the second dataset is in green.]
[Print media version: The data and fit for the first dataset is in green, and that for the second dataset is in green.]

Figure 6: Using different colors for fit-style plots