NDDataset creation and plotting example#

In this example, we create a 3D NDDataset from scratch, and then we plot one section (a 2D plane)


Creation#

Now we will create a 3D NDDataset from scratch

As usual, we start by loading the spectrochempy library

import spectrochempy as scp

Coordinates#

The Coord object allows creating an array of coordinates directly with Coord.linspace, attaching metadata (units, labels, title) in a single step — no separate numpy array needed.

coord0 = scp.Coord.linspace(
    200.0,
    300.0,
    3,
    labels=["cold", "normal", "hot"],
    units="K",
    title="temperature",
)
coord1 = scp.Coord.linspace(0.0, 60.0, 100, units="minutes", title="time-on-stream")
coord2 = scp.Coord.linspace(4000.0, 1000.0, 100, units="cm^-1", title="wavenumber")

Labels can be useful for instance for indexing

a = coord0["normal"]
a
Coord [Coord_ac0f06bd:temperature] — float64, size: 1, K
size
:
1
title
:
temperature
coordinates
:
[ 250] K
labels
:
[ normal]


Data and nd-Dataset#

scp.fromfunction builds an NDDataset directly from a Python function. The function receives the coordinate arrays and returns the intensity values.

from math import tau


def synth_func(temperature, time, wavenumber):
    return scp.sin(tau * wavenumber / 4000.0) * scp.exp(-time / 60) * temperature


mydataset = scp.fromfunction(
    synth_func,
    coordset=[coord0, coord1, coord2],
    title="Absorbance",
    units="absorbance",
)
mydataset.description = """Dataset example created for this tutorial.
It's a 3-D dataset (with dimensionless intensity: absorbance )"""
mydataset.name = "An example from scratch"
mydataset.author = "Blake and Mortimer"
mydataset
NDDataset [An example from scratch] — float64, shape: (z:3, y:100, x:100), a.u.
name
:
An example from scratch
author
:
Blake and Mortimer
created
:
2026-11-15 08:43:46+00:00
description
:
Dataset example created for this tutorial.
It's a 3-D dataset (with dimensionless intensity: absorbance )
history
:
2026-11-15 08:43:47+00:00> Created using method : fromfunction
Data
title
:
Absorbance
values
:
[[[ 1236 1227 ... 318.4 309]
[ 1224 1214 ... 315.2 305.9]
...
[ 459.3 455.9 ... 118.3 114.8]
[ 454.7 451.3 ... 117.1 113.7]]

[[ 1531 1519 ... 394.3 382.7]
[ 1515 1504 ... 390.3 378.8]
...
[ 568.8 564.5 ... 146.5 142.2]
[ 563.1 558.9 ... 145 140.8]]

[[ 1816 1802 ... 467.7 454]
[ 1798 1784 ... 463 449.4]
...
[ 674.8 669.7 ... 173.8 168.7]
[ 668.1 663 ... 172.1 167]]] a.u.
shape
:
(z:3, y:100, x:100)
Dimension `x`
size
:
100
title
:
wavenumber
coordinates
:
[ 4000 3970 ... 1030 1000] cm⁻¹
Dimension `y`
size
:
100
title
:
time-on-stream
coordinates
:
[ 0 0.606 ... 59.39 60] min
Dimension `z`
size
:
3
title
:
temperature
coordinates
:
[ 200 250 300] K
labels
:
[ cold normal hot]


We want to plot a section of this 3D NDDataset:

NDDataset can be sliced like conventional numpy-array…

new = mydataset[..., 0]

or maybe more conveniently in this case, using an axis labels:

new = mydataset["hot"]

To plot a dataset, use the plot command (generic plot). As the section NDDataset is 2D, a stack plot is displayed by default. As you can see, the x-axis is in wavenumber and the ordinate axis is in absorbance units (au). The y dimension of the dataset is the time-on-stream (in minutes). Because the time-on-stream values are floats, this triggers the default sequential colormap (‘viridis’). The corresponding values can be seen if colorbar is passed as True:

_ = new.plot(colorbar=True)
plot a create dataset

It is also possible to display this dataset as an image (actually a filled contour plot). The x is the same as before, but the ordinates are now the time-on-stream values. The color of the pixels is now related to the value of the absorbance. As the dataset contains both negative and positive values, the default colormap is diverging (RdBu).

_ = new.plot(method="image", colorbar=True)
plot a create dataset

If a dataset contains only positive values, the default colormap is sequential (viridis):

_ = scp.abs(new).plot(method="image", colorbar=True)
plot a create dataset
_ = new.plot(method="contour")
_ = scp.abs(new).plot(method="contour")
  • plot a create dataset
  • plot a create dataset

Note that the scp allows one to use this syntax too:

plot a create dataset

Uncomment the following line to display all figures when running the script directly with Python.

# scp.show()

Total running time of the script: (0 minutes 1.564 seconds)