Last modified: 18 December 2023

Including extra data in plots with matplotlib


Once a plot has been created then Matplotlib commands can be used to augment the display. For example, adding labels to identify the different components:

sherpa> plot_fit(ylog=False)
sherpa> plot_resid(overplot=True, marker='s', markersize=3, alpha=0.5)
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)

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