# Plotting 2-D data¶

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:

[2]:

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)


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:

[3]:

d1['Signal'].transpose().plot()


## Changing the colorscale¶

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

[4]:

sc.plot(d1, cmap='magma')


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

[5]:

sc.plot(d1, norm='log')


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

[6]:

sc.plot(d1, vmin=0*sc.Unit('K'), vmax=0.5*sc.Unit('K'))


## Using labels along some axis¶

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

[7]:

d1.coords['somelabels'] = sc.Variable(dims=['x'],
values=np.linspace(101., 155., N),
unit=sc.units.s)
sc.plot(d1, labels={'x': 'somelabels'})


## 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.

[8]:

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)

[9]:

sc.plot(sc.collapse(d2['sample'], keep='tof'))


## 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.

[10]:

sc.plot(d2, aspect='equal')


You can also make this a permanent setting by editing the config file (possible options are 'equal' and 'auto'):

[11]:

sc.config.update({'plot.aspect': 'equal'})
sc.config.update({'plot.aspect': 'auto'})


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

[12]:

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)
sc.plot(d3)


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

[13]:

sc.plot(d3, masks={'color': 'magenta'})


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:

[14]:

d3.masks['mask1'] = sc.Variable(dims=['x'], values=np.where(np.abs(xx-50) < 10, True, False))
sc.plot(d3)


## 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:

[15]:

sc.plot(d3, filename='my_2d_figure.pdf')