Last modified: 15 December 2025

How can I create multiple plots in the same window? [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> set_xlog()
sherpa> set_ylog()

and that fits have been made to both data sets.

If you wish to display multiple datasets in the same plot window then you can use the plot command; for example if you have two datasets with ids of 1 and 2 then:

sherpa> plot("data", 1, "data", 2)

will display each data set in a separate plot in the same window (Figure 1).

Figure 1: Plotting multiple data sets

[The two data sets are arranged vertically, showing counts/sec/kev (y axis) against energy (kev) for the x axis]
[Print media version: The two data sets are arranged vertically, showing counts/sec/kev (y axis) against energy (kev) for the x axis]

Figure 1: Plotting multiple data sets

The default layout of the plots is not guaranteed to be free of overlaps of plot elements, such as the X-axis label of the top plot and the title of the second plot.

Parameters can be applied to all plots, such as setting the X-axis to a linear scale:

sherpa> plot("data", 1, "data", 2, xlog=False)

You can display more than two plots, for instance (Figure 2):

sherpa> plot('fit', 1, 'fit', 2, 'delchi', 1, 'delchi', 2)

Figure 2: Plotting fit and residuals

[The plots are in a two-by-two grid with the data and "sigma residuals" for the first dataset in the first column and for the second dataset in the second column]
[Print media version: The plots are in a two-by-two grid with the data and "sigma residuals" for the first dataset in the first column and for the second dataset in the second column]

Figure 2: Plotting fit and residuals

The overlapping of labels is present and the Y axes of the plots are also not the same, which makes cross comparison difficult.

The spacing and layout of the plots can be adjusted using Matplotlib.

Plot options can be specified for all plots (when a scalar, such as a single color):

sherpa> plot('fit', 'fit', 2, color='black')

Figure 3: Changing plot properties

[The two fits are shown ni black - both the data and the model fit.]
[Print media version: The two fits are shown ni black - both the data and the model fit.]

Figure 3: Changing plot properties

Plot options can also per per-plot, and each plot can have a different number of datasets (this is new in CIAO 4.18 [New]). In this example. the first two plots are set to the data and models for datasets 1 and 2, and the last (bottom) plot is the residuals for both datasets. The colors of this last plot are set so that the residual plot color matches the fit results.

sherpa> col1 = 'gray'
sherpa> col2 = 'green'
sherpa> colors = [col1, col2, [col1, col2]]
sherpa> plot('fit', 'fit', 2, 'resid', [1, 2], color=colors, cols=1)

Figure 4: Multiple color settings

[The three plots are shown vertically aligned (so a single columm), with the top being the fit to dataset 1 (in gray), the middle plot is the fit to dataset 2 (in green), and the third contains the residuals for both datasets (again with gray for dataset 1 and green for dataset 2).]
[Print media version: The three plots are shown vertically aligned (so a single columm), with the top being the fit to dataset 1 (in gray), the middle plot is the fit to dataset 2 (in green), and the third contains the residuals for both datasets (again with gray for dataset 1 and green for dataset 2).]

Figure 4: Multiple color settings

If more than two plots are created then the default is to pick a multi-column display. This can be over-ridden by setting the cols or rows argument (or both). In this case setting cols=1 ensures the display uses a single column rather than a 2 by 2 grid such as used in Figure 2.

A version of this plot is also included in the using Matplotlib FAQ entry.

[New] New in CIAO 4.18 is the use of the ids argument to apply a set of identifiers to each plot type. For example, a fit and residual plot (Figure 5) can be generated with:

sherpa> plot('fit', 'resid', ids=[1, 2], alpha=0.5)

Figure 5: Using the ids argument

[There are two plots: on top are two "fit" plots (so data plus model), and the bottom shows two residual plots for the two datasets.]
[Print media version: There are two plots: on top are two "fit" plots (so data plus model), and the bottom shows two residual plots for the two datasets.]

Figure 5: Using the ids argument

A version of this plot is also included in the using Matplotlib FAQ entry.