Last modified: 18 December 2023

How can I set the axis scaling of existing plots to linear or logarithmic? [Updated]


The earlier version of this entry shows how to change plots that you are about to create. This entry shows how you can change an existing plot when using Matplotlib.

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 Y and then X axes to a logarithmic scale:

sherpa> plt.yscale('log')
sherpa> plt.xscale('log')

If you have a multi-panel plot - e.g. plot_fit_ratio - then you can use the axes attribute of the figure to chose which plot to change. The following example changes the top plot to have a logarithmically-scaled Y axis

sherpa> fig = plt.gcf()
sherpa> plt.sca(fig.axes[0])
sherpa> plt.yscale('log')

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

sherpa> ax1, ax2 = fig.axes
sherpa> ax1.set_yscale('log')