Subtract the background estimate from a data set.

The subtract function performs a channel-by-channel subtraction of the background estimate from the data. After this command, anything that uses the data set - such as a plot, fit, or error analysis - will use the subtracted data. Models should be re-fit if subtract is called.


id (int or str, optional) – The identifier for the data set to use. If not given then the default identifier is used, as returned by get_default_id.


sherpa.utils.err.ArgumentErr – If the data set does not contain a PHA data set.

See also


Fit one or more data sets.


Undo any background subtraction for the data set.


Unlike X-Spec, Sherpa does not automatically subtract the background estimate from the data.

Background subtraction can only be performed when data and background are of the same length. If the data and background are ungrouped, both must have same number of channels. If they are grouped, data and background can start with different numbers of channels, but must have the same number of groups after grouping.

The equation for the subtraction is:

src_counts - bg_counts * (src_exposure * src_backscal)
                          (bg_exposure * bg_backscal)

where src_exposure and bg_exposure are the source and background exposure times, and src_backscal and bg_backscal are the source and background backscales. The backscale, read from the BACKSCAL header keyword of the PHA file, is the ratio of data extraction area to total detector area.

The subtracted field of a dataset is set to True when the background is subtracted.


Background subtract the default data set.

>>> subtract()
>>> get_data().subtracted

Remove the background from the data set labelled ‘src’:

>>> subtract('src')
>>> get_data('src').subtracted

Overplot the background-subtracted data on the original data for the default data set:

>>> plot_data()
>>> subtract()
>>> plot_data(overplot=True)