Last modified: 22 December 2024

Changing the limits of a plot


Matplotlib commands can be used to change existing plots. For example - assuming that import matplotlib.pyplot as plt has already been called - the following will change the x and Y limits of a plot:

sherpa> plot_source(ylog=True)
sherpa> plt.xlim(0, 2)
(0.0, 2.0)
sherpa> plt.ylim(1e-5, 1e-3)
(1e-05, 0.001)

If you have a multi-panel plot - e.g. plot_fit_ratio - then the X axes are linked, so changing the axis in one plot will change both plots:

sherpa> plot_fit_ratio()
sherpa> plt.xlim(0, 2)
(0.0, 2.0)

If you want to change the Y axis of one (or both) of the plots, or have used the plot function to create multiple plots, then can use the axes attribute of the figure to chose which plot to change. The following example changes the top plot to use a Y range or 0 to 0.02 and the bottom plot to a Y range of -2 to 2:

sherpa> plot("data", "resid")	     
sherpa> fig = plt.gcf()
sherpa> plt.sca(fig.axes[0])
sherpa> plt.ylim(0, 0.02)
(0.0, 0.02)
sherpa> plt.sca(fig.axes[1])
sherpa> plt.ylim(-2, 2)
(-2.0, 2.0)

An alternative version would be to use the axis objects directly - as shown below - which uses the set_ylim method of the axis to change the scaling:

sherpa> ax1, ax2 = fig.axes
sherpa> ax1.set_ylim(0, 0.02)
(0.0, 0.02)
sherpa> ax2.set_ylim(-2, 2)
(-2.0, 2.0)

There is a set_xlim version for the X axis.