This page was generated from the Jupyter notebook notebooks/ExamplePlots.

A sample of plots

The idea of this notebook is to show off a number of plot types, and act as a simple check of the plotting output. It requires matplotlib and does not attempt to describe the plots (see the help for the plot constructor for this!).

[1]:
import numpy as np

%matplotlib inline
[2]:
from sherpa import data
from sherpa.astro import data as astrodata

from sherpa import plot
from sherpa.astro import plot as astroplot

One dimensional data plots

[3]:
x1 = [100, 200, 600, 1200]
y1 = [2000, 2100, 1400, 3050]

d1 = data.Data1D('oned', x1, y1)

plot1 = plot.DataPlot()
plot1.prepare(d1)
plot1.plot()
_images/ExamplePlots_4_0.png

We can have some fun with the plot options (these are a mixture of generic options, such as xlog, and ones specific to the plotting backend - which here is matplotlib - such as marker).

[4]:
plot1.plot(xlog=True, linestyle='dotted', marker='*', markerfacecolor='orange', markersize=20, color='black')
_images/ExamplePlots_6_0.png

The plot object contains the preferences - here we look at the default plot settings. Note that some plot types have different - and even multiple - preference settings.

[5]:
plot.DataPlot.plot_prefs
[5]:
{'xlog': False,
 'ylog': False,
 'label': None,
 'xerrorbars': False,
 'yerrorbars': True,
 'color': None,
 'linestyle': 'None',
 'linewidth': None,
 'marker': '.',
 'alpha': None,
 'markerfacecolor': None,
 'markersize': None,
 'ecolor': None,
 'capsize': None}

Error bars - here on the dependent axis - can be displayed too:

[6]:
dy1 = [100, 50, 200, 300]
d2 = data.Data1D('errors', x1, y1, dy1)

plot2 = plot.DataPlot()
plot2.prepare(d2)

plot2.plot()
_images/ExamplePlots_10_0.png
[7]:
plot2.plot(capsize=4)
_images/ExamplePlots_11_0.png

Histogram-style data (with low and high edges) are handled similarly:

[8]:
xlo2 = [0.1, 0.2, 0.4, 0.8, 1.5]
xhi2 = [0.2, 0.4, 0.6, 1.1, 2.0]
y2 = [10, 12, 3, 0, 4]

data3 = data.Data1DInt('int1', xlo2, xhi2, y2)

plot3 = plot.DataHistogramPlot()
plot3.prepare(data3)
plot3.plot(xlog=True)
_images/ExamplePlots_13_0.png

If we want to see the data drawn “like a histogram” then we need to set the linestyle attribute:

[9]:
plot3.plot(xlog=True, linestyle='solid')
_images/ExamplePlots_15_0.png

The histogram-style plots are an example of a plot using a different name for the preference settings, in this case histo_prefs:

[10]:
plot.DataHistogramPlot.histo_prefs
[10]:
{'xlog': False,
 'ylog': False,
 'label': None,
 'xerrorbars': False,
 'yerrorbars': True,
 'color': None,
 'linestyle': 'None',
 'linewidth': None,
 'marker': '.',
 'alpha': None,
 'markerfacecolor': None,
 'markersize': None,
 'ecolor': None,
 'capsize': None}

Previously we explicitly set the error values, but we can also use one of the chi-square statistics to come up with error values. In this case it’s just the square-root of the data value (so, for \(x \sim 1\) bin, we have an error of 0):

[11]:
from sherpa.stats import Chi2DataVar

plot4 = plot.DataHistogramPlot()
plot4.prepare(data3, stat=Chi2DataVar())
plot4.plot(linestyle='dashed', marker=None, ecolor='orange', capsize=4)
_images/ExamplePlots_19_0.png

Axis labelling

The axis labels depend on the plot object and the data used in the prepare call. Functionality added in release 4.17.0 allows us to change the labels with the set_xlabel and set_ylabel calls of the Data class:

[12]:
# These changes must be made before the call to prepare.
data3.set_xlabel("distance [kpc]")
data3.set_ylabel("Relative abundance")

plot5 = plot.DataHistogramPlot()
plot5.prepare(data3)

plot5.plot(linestyle="solid", marker=None)
_images/ExamplePlots_21_0.png

Object-less plots

There are a number of plot classes that don’t need a data object, such as scatter plots:

[22]:
rng = np.random.default_rng(1273)

# I've never used the Wald distribution before, so let's see how it looks...
#
z1 = rng.wald(1000, 20, size=1000)
z2 = rng.wald(1000, 2000, size=1000)

# We take advantage of matplotlib functionality in labelling the axes and title.
#
splot = plot.ScatterPlot()
splot.prepare(z1, z2, xlabel='z$_1$', ylabel='z$_2$', name='(z$_1$, z$_2$)')
splot.plot(xlog=True)
_images/ExamplePlots_41_0.png

and cumulative plots:

[23]:
cplot = plot.CDFPlot()

cplot.prepare(z1, xlabel='z', name='z')
cplot.plot(xlog=True)

cplot.prepare(z2)
cplot.overplot()
_images/ExamplePlots_43_0.png

Note that this is a small sampling of the available plot types (although most are variants of plotting either the data or a model).