Last modified: 18 December 2023

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


Within the Sherpa session, the following commands may be used to set the scale of plots to either linear or logarithmic. Note that the set_* commands allow you to specify that only certain types of plots be affected by the scaling setting, e.g., only data or model plots, as opposed to having all plots created in the session altered.

The Matplotlib version of this entry shows how to change an existing plot.

Setting the scaling when you create the plot

The plot and plot_xxx() calls accept the xlog and ylog parameters which control whether the axis is drawn with a logarathmic scale. For example:

sherpa> plot_data(xlog=True)

will create a "data" plot with its X axis drawn using a log scale, and

sherpa> plot_fit(xlog=True, ylog=True)

will make a "fit" plot with both axes drawn with a log scale. Note that the settings applies to both plots for composite plots, so a command like

sherpa> plot_fit_resid(ylog=True)

will use a logarithmic scale for both the fit and residual plots, which is not ideal (since the residual plot should include negative points).

Changing the Sherpa preferences

The following commands will change any new plots, but not existing ones.

When called with no arguments, these commands will set the scale of all plots created in the session; otherwise, they will alter only the specified plot type, e.g., 'set_xlog("data")' to set the X axis of only data plots to log scale. They accept the arguments used by the generic plot command, e.g., "data", "model", "source", "fit", "delchi", etc.

The data and model plot preferences can be changed directly (this is similar to using set_xlog and set_ylog).

The get_model_component_plot command must be used to set the axes of plots created with the plot_model_component command.

[New] New in CIAO 4.16 is the get_plot_prefs command which generalizes the get_data_plot_prefs and get_model_plot_prefs calls, allowing you to say

for plotname in ["data", "resid", "delchi"]:
    get_plot_prefs(plotname)["xerrorbars"] = False

Remembering the preference settings

To change the default preference settings for plot_data so that both the X and Y axes of a data plot will be drawn using a log scale each time the function is called in Sherpa, add the following get_data_plot_prefs() functions to your Sherpa customization file, located in $HOME/.ipython-ciao/profile_sherpa/startup/. For instance, if you create the following file $HOME/.ipython-ciao/profile_sherpa/startup/99-user.py:

print("# Setting data/model plots to log scale")
for ptype in ["data", "model"]:
    get_plot_prefs(ptype)["xlog"] = True
    get_plot_prefs(ptype)["ylog"] = True

Then you will see the screen message Setting data/model plots to log scale when you start Sherpa and the plot preferences will be set:

unix% sherpa
-----------------------------------------------------
Welcome to Sherpa: CXC's Modeling and Fitting Package
-----------------------------------------------------
Sherpa 4.16.0

Python 3.11.6 | packaged by conda-forge | (main, Oct  3 2023, 10:40:35) [GCC 12.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.17.2 -- An enhanced Interactive Python. Type '?' for help.

IPython profile: sherpa
Installed qt5 event loop hook.
Using matplotlib backend: QtAgg
# Setting data/model plots to log scale

sherpa In [1]: get_plot_prefs("data")['ylog']
Out[1]: True