# Plotting 1-D data#

Deprecated

This particular guide uses the old plotting backend which is now deprecated and will be removed in the future. See Migrating to the New Backend for general migration instructions. And see the documentation of Plopp for details on plotting with the new backend.

Scipp offers a number of different ways to plot data from a DataArray or a Dataset. It uses the matplotlib graphing library to do so.

[1]:

import numpy as np
import scipp as sc


## Basic plot#

Plotting is done using the plot function. Generally the information in a dataset is sufficient to produce a useful plot out of the box.

For example, a simple plot from a 1D dataset is produced as follows:

[3]:

d = sc.Dataset()
N = 50
d['Signal'] = sc.Variable(dims=['tof'], values=5.0 + 5.0*np.sin(np.arange(N, dtype=float)/5.0),
unit=sc.units.counts)
d.coords['tof'] = sc.Variable(dims=['tof'], values=np.arange(N).astype(float),
unit=sc.units.us)
sc.plot(d)

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[3]:


## With error bars#

Error bars are shown automatically if variances are present in the data:

[4]:

d['Signal'].variances = np.square(np.random.random(N))
sc.plot(d)

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[4]:


Note that the length of the errors bars is the standard-deviation, i.e., the square root of the variances stored in the data.

## Multiple variables on the same axes#

### Plotting a Dataset with multiple entries#

If a dataset contains more than one 1D variable with the same coordinates, they are plotted on the same axes:

[5]:

d['Background'] = sc.Variable(dims=['tof'], values=3.0*np.random.random(N),
unit=sc.units.counts)
sc.plot(d)

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[5]:


It is possible to hide the error bars with

[6]:

sc.plot(d, errorbars=False)

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[6]:


We can always plot just a single item of the dataset.

[7]:

sc.plot(d['Background'])

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[7]:


### Overplotting using a dict of DataArrays#

One can also supply the plot function with a dict of data arrays. Compatible data arrays will be overplotted on the same axes:

[8]:

sc.plot({"My sample": d['Signal'], "My background": d["Background"]})

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[8]:


Note that the newly supplied names (keys) have also been adopted as the graph names.

We can also overplot sections of interest with the help of slicing.

[9]:

sc.plot({"My sample": d['Signal']['tof', 10:40], "My background": d["Background"]})

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[9]:


This overplotting is useful for plotting slices of a 2D data array:

[10]:

M = 100
L = 5
xx = np.arange(M, dtype=float)
yy = np.arange(L, dtype=float)
x, y = np.meshgrid(xx, yy)
b = M/20.0
c = M/5.0
e = L/10.0
r = np.sqrt(((x-c)/b)**2 + (y/e)**2)
a = np.sin(r)
d2d = sc.DataArray(data=sc.Variable(dims=['y', 'x'], values=a, unit=sc.units.counts))
d2d.coords['x'] = sc.Variable(dims=['x'], values=xx, unit=sc.units.m)
d2d.coords['y'] = sc.Variable(dims=['y'], values=yy, unit=sc.units.m)
sc.plot({f'slice-{i}': d2d['y', i] for i in range(L)})

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[10]:


Or using the collapse helper function, which returns a dict of data arrays:

[11]:

sc.plot(sc.collapse(d2d, keep='x'))

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[11]:


## Customizing linestyles, markers and colors#

Linestyles can be customized following the Matplotlib syntax. For instance, it is possible to connect the dots by setting linestyle='solid':

[12]:

sc.plot(d, linestyle='solid')

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[12]:


Marker colors and symbols can be changed via the color and marker keyword arguments:

[13]:

sc.plot(d, color=['red', '#30D5F9'], marker=['s', 'x'])

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[13]:


The supplied color and marker arguments can also be a list of integers, which correspond to one of the pre-defined colors or markers (which were taken from matplotlib). In addition, the grid can also be displayed:

[14]:

sc.plot(d, color=[6, 8], grid=True)

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[14]:


## Logarithmic scales#

Use the keyword arguments scale and norm to apply logarithmic scales to the horizontal and vertical axes, respectively.

[15]:

sc.plot(d, norm='log', scale={'tof': 'log'})

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[15]:


## Histograms#

Histograms are automatically generated if the coordinate is bin edges:

[16]:

histogram = sc.DataArray(data=sc.Variable(dims=['tof'],
values=20.0 + 20.0*np.cos(np.arange(N-1, dtype=float) / 3.0),
unit=sc.units.counts),
coords={'tof':d.coords['tof']})
sc.plot(histogram)

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[16]:


and with error bars

[17]:

histogram.variances = 5.0*np.random.random(N-1)
sc.plot(histogram)

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[17]:


The histogram color can be customized:

[18]:

sc.plot(histogram, color='#000000')

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[18]:


## Multiple 1D variables with different dimensions#

scipp.plot also supports multiple 1-D variables with different dimensions (note that the data entries are grouped onto the same graph if they have the same dimension and unit):

[19]:

M = 60
d['OtherSample'] = sc.Variable(dims=['x'], values=10.0*np.random.rand(M),
unit=sc.units.s)
d['OtherNoise'] = sc.Variable(dims=['x'], values=7.0*np.random.rand(M),
variances=3.0*np.random.rand(M),
unit=sc.units.s)
d['SomeKgs'] = sc.Variable(dims=['x'], values=20.0*np.random.rand(M),
unit=sc.units.kg)
d.coords['x'] = sc.Variable(dims=['x'],
values=np.arange(M).astype(float),
unit=sc.units.m)
sc.plot(d)

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[19]:


## Custom labels along x axis#

Sometimes one wishes to have labels along the x axis instead of the dimension-coordinate. This can be achieved via the labels keyword argument by specifying which dimension should run along the x axis:

[20]:

d1 = sc.Dataset()
N = 100
x = np.arange(N, dtype=float)
d1['Sample'] = sc.Variable(dims=['tof'],
values=np.sqrt(x),
unit=sc.units.counts)
d1.coords['tof'] = sc.Variable(dims=['tof'],
values=x,
unit=sc.units.us)
d1.coords['somelabels'] = sc.Variable(dims=['tof'],
values=np.linspace(101., 105., N),
unit=sc.units.s)
sc.plot(d1, labels={'tof': 'somelabels'})

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[20]:


If a dataset item contains masks, the symbols of masks data points will have a thick black contour in a 1D plot:

[21]:

d4 = sc.Dataset()
N = 50
x = np.arange(N).astype(float)
d4['Sample'] = sc.Variable(dims=['tof'], values=3*np.sin(x/5)+3,
unit=sc.units.counts)
d4['Background'] = sc.Variable(dims=['tof'], values=1.0*np.random.rand(N),
unit=sc.units.counts)
d4.coords['tof'] = sc.Variable(dims=['tof'], values=x,
unit=sc.units.us)
values=np.where(np.abs(x-40) < 10, True, False))
sc.plot(d4)

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[21]:


Checkboxes below the plot can be used to hide/show the individual masks. A global toggle button is also available to hide/show all masks in one go.

The color of the masks can be changed as follows:

[22]:

sc.plot(d4, masks={'color': 'red'})

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[22]:


Masks on a histogram show up as a thick black line:

[23]:

N = 50
x = np.arange(N+1).astype(float)
d4 = sc.DataArray(data=sc.Variable(dims=['tof'], values=3*np.sin(x[:-1]/5)+3, unit=sc.units.counts))
d4.coords['tof'] = sc.Variable(dims=['tof'], values=x, unit=sc.units.us)
sc.plot(d4)

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(

[23]:


## Plotting time series#

When plotting data with time-series (dtype=datetime64) coordinates, a special axis tick label formatting, which dynamically adapts to the zoom level, is used.

[24]:

time = sc.array(dims=['time'], values=np.arange(np.datetime64('2017-01-01T12:00:00'),
np.datetime64('2017-01-01T13:00:00'), 20))
N = time.sizes['time']
data = sc.DataArray(data=sc.array(dims=['time'],
values=np.arange(N) + 50.*np.random.random(N),
unit="K"),
coords={'time':time})
data.plot(title="Temperature as a function of time")

[24]:


## Saving figures#

Static pdf or png copies of the figures can be saved to file (note that any buttons displayed under a figure are not saved to file). This is achieved as follows:

[25]:

sc.plot(d4, filename='my_1d_figure.pdf')

/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/plotting/__init__.py:142: VisibleDeprecationWarning: You are using Scipp's deprecated plotting backend. This will be removed in Scipp v23.08.0 (August 2023) or after. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
warnings.warn(