Plotting 2-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 uses the imshow function from the matplotlib library to visualize 2-D data.

[1]:
import numpy as np
import scipp as sc

Basic image plot#

2-D variables are plotted as images, with a colormap:

[3]:
N = 100
M = 50
xx = np.arange(N, dtype=np.float64)
yy = np.arange(M, dtype=np.float64)
x, y = np.meshgrid(xx, yy)
b = N/20.0
c = M/2.0
r = np.sqrt(((x-c)/b)**2 + (y/b)**2)
a = np.sin(r)
d1 = sc.Dataset()
d1['Signal'] = sc.Variable(dims=['y', 'x'], values=a, unit=sc.units.K)
d1.coords['x'] = sc.Variable(dims=['x'], values=xx, unit=sc.units.m)
d1.coords['y'] = sc.Variable(dims=['y'], values=yy, unit=sc.units.m)
sc.plot(d1)
/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. Scipp v23.03.0 (March 2023) and all later versions will use Plopp by default. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
  warnings.warn(
[3]:

The dimension displayed along each axis of the image can changed using the tranpose buttons in the toolbar to the left of the figure, or by transposing the input data:

[4]:
d1['Signal'].transpose().plot()
[4]:
../../_images/visualization_plotting_plotting-2d-data_6_0.svg

Changing the colorscale#

Changing the colorscale is handled in a similar way to Matplotlib syntax. The colormap is defined by the cmap argument:

[5]:
sc.plot(d1, cmap='magma')
/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. Scipp v23.03.0 (March 2023) and all later versions will use Plopp by default. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
  warnings.warn(
[5]:

A logarithmic colorscale is obtained by setting norm to 'log':

[6]:
sc.plot(d1, norm='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. Scipp v23.03.0 (March 2023) and all later versions will use Plopp by default. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
  warnings.warn(
[6]:

Upper and lower limits on the colorscale can be placed using vmin and vmax:

[7]:
sc.plot(d1, vmin=0*sc.Unit('K'), vmax=0.5*sc.Unit('K'))
/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. Scipp v23.03.0 (March 2023) and all later versions will use Plopp by default. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
  warnings.warn(
[7]:

Using labels along some axis#

Just like in the 1d plots, we can use labels along a chosen dimension:

[8]:
d1.coords['somelabels'] = sc.Variable(dims=['x'],
                                      values=np.linspace(101., 155., N),
                                      unit=sc.units.s)
sc.plot(d1, labels={'x': '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. Scipp v23.03.0 (March 2023) and all later versions will use Plopp by default. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
  warnings.warn(
[8]:

Collapsing dimensions#

Sometimes it is useful to collapse one or more of the data’s dimensions, if for instance most detector pixels contain noise, but one specific channel contains a strong signal. This is done by specifying the dimension to be displayed along the x axis as a keyword argument to sc.collapse. All other dimensions will be collapsed.

[9]:
N = 40
M = 5
x = np.arange(N).astype(np.float64)
b = 0.5 * N
a = 4.0*np.random.rand(M, N)
a[2, :] = np.abs(10.0 * np.cos((x-b)*2.0/b))
d2 = sc.Dataset()
d2['sample'] = sc.Variable(dims=['x', 'tof'], values=a,
                           variances=0.1*np.random.rand(M, N))
d2.coords['tof'] = sc.Variable(dims=['tof'], values=x, unit=sc.units.us)
d2.coords['x'] = sc.Variable(dims=['x'], values=np.arange(M).astype(np.float64),
                               unit=sc.units.m)
sc.plot(d2)
/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. Scipp v23.03.0 (March 2023) and all later versions will use Plopp by default. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
  warnings.warn(
[9]:
[10]:
sc.plot(sc.collapse(d2['sample'], keep='tof'))
/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. Scipp v23.03.0 (March 2023) and all later versions will use Plopp by default. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
  warnings.warn(
[10]:

Image aspect ratio#

By default, the aspect ratio of 2D images is not preserved; images are stretched to the size of the figure. You can choose to preserve the aspect ratio via the aspect keyword argument. The default value for aspect is auto which allows for scaling.

[11]:
sc.plot(d2, aspect='equal')
/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. Scipp v23.03.0 (March 2023) and all later versions will use Plopp by default. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
  warnings.warn(
[11]:

You can also make this a permanent setting by editing Scipp’s configuration (possible options are 'equal' and 'auto'):

[12]:
sc.config['plot']['aspect'] = 'equal'
# or
sc.config['plot']['aspect'] = 'auto'

Plotting masks#

If a dataset contains masks, they will appear as greyed out on the image:

[13]:
N = 100
M = 50
xx = np.arange(N, dtype=np.float64)
yy = np.arange(M, dtype=np.float64)
x, y = np.meshgrid(xx, yy)
b = N/20.0
c = M/2.0
r = np.sqrt(((x-c)/b)**2 + (y/b)**2)
a = np.sin(r)
d3 = sc.DataArray(data=sc.Variable(dims=['y', 'x'], values=a, unit=sc.units.K))
d3.coords['x'] = sc.Variable(dims=['x'], values=xx, unit=sc.units.m)
d3.coords['y'] = sc.Variable(dims=['y'], values=yy, unit=sc.units.m)
d3.masks['mask1'] = sc.Variable(dims=['y', 'x'], values=np.where(a < 0, True, False))
sc.plot(d3)
/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. Scipp v23.03.0 (March 2023) and all later versions will use Plopp by default. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
  warnings.warn(
[13]:

Note that hovering over a masked region still yields the underlying value of the element on the display.

A toggle button below the plot can be used to hide/show the masks.

The mask can be represented as a solid color with

[14]:
sc.plot(d3, masks={'color': 'magenta'})
/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. Scipp v23.03.0 (March 2023) and all later versions will use Plopp by default. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
  warnings.warn(
[14]:

We also note that any 1D mask will automatically broadcast onto a 2D image. This is due to underlying behavior of scipp around broadcasting rather than special handling in plotting:

[15]:
d3.masks['mask1'] = sc.Variable(dims=['x'], values=np.where(np.abs(xx-50) < 10, True, False))
sc.plot(d3)
/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. Scipp v23.03.0 (March 2023) and all later versions will use Plopp by default. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
  warnings.warn(
[15]:

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:

[16]:
sc.plot(d3, filename='my_2d_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. Scipp v23.03.0 (March 2023) and all later versions will use Plopp by default. See https://scipp.github.io/visualization/plotting-overview.html#Migrating-to-the-new-backend for details and a migration guide.
  warnings.warn(