Last modified: 12 December 2019

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).

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]

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.

matplotlib functions can be used to tweak the layout, sich as increasing the vertical space between the two plots (Figure 2):

sherpa> plt.subplots_adjust(hspace=0.5)

Increasing the vertical space between the plots

[This is very similar to the previous plot, except that now the plot label and title in the center of the plot do not overlap]
[Print media version: This is very similar to the previous plot, except that now the plot label and title in the center of the plot do not overlap]

Increasing the vertical space between the plots

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

sherpa> plot('fit', 1, 'fit', 2, 'delchi', 1, 'delchi', 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]

Plotting fit and residuals

The overlapping of labels is present, as well as the fact that the residual plots have been drawn with a logarithmic rather than linear scale (due to the use of set_ylog earlier). The Y axes of the plots are also not the same, which makes cross comparison difficult.

As with the previous example, matplotlib commands can be used to tweak the appearance of the plot (Figure 4):

sherpa> fig = plt.gcf()
sherpa> for i in [0, 1]:
   ...:    fig.axes[i].xaxis.label.set_visible(False)
   ...:    fig.axes[i].xaxis.set_ticklabels([])
   ...:    fig.axes[i].set_ylim(1e-4, 2e-1)
   ...:
sherpa> for i in [2, 3]:
   ...:    fig.axes[i].title.set_visible(False)
   ...:    fig.axes[i].set_yscale('linear')
   ...:    fig.axes[i].set_ylim(-4, 4)
   ...:
sherpa> for i in [1, 3]:
   ...:    fig.axes[i].yaxis.label.set_visible(False)
   ...:    fig.axes[i].yaxis.set_ticklabels([])
   ...:
sherpa> plt.subplots_adjust(hspace=0.05, wspace=0.05)

The final plot

[The horizontal and vertical space between the plots has been removed (except for a small amount to make the tick marks still visible), and the labels that were in this space have gone. The Y axes of the top row of plots is now the same, as is the bottom row (although each row has different ranges).]
[Print media version: The horizontal and vertical space between the plots has been removed (except for a small amount to make the tick marks still visible), and the labels that were in this space have gone. The Y axes of the top row of plots is now the same, as is the bottom row (although each row has different ranges).]

The final plot

The overlapping of labels is present, as well as the fact that the residual plots have been drawn with a logarithmic rather than linear scale (due to the use of set_ylog earlier). The Y axes of the plots are also not the same, which makes cross comparison difficult.