Line plot#

[1]:
import scipp as sc
import plopp as pp

Basic line plot#

The most common way to plot data with Plopp is to use the plot function. This can either be done using the plopp.plot() free function, or calling the .plot() method on a Scipp data object (both are equivalent).

[2]:
da = pp.data.data1d()
da.plot()
[2]:
../../_images/user-guide_plot-types_line-plot_3_0.svg

Changing line style and color#

[3]:
da.plot(linestyle='solid', color='black', marker=None)
[3]:
../../_images/user-guide_plot-types_line-plot_5_0.svg
[4]:
da.plot(linestyle='dashed', linewidth=5, marker=None)
[4]:
../../_images/user-guide_plot-types_line-plot_6_0.svg
[5]:
da.plot(marker='^')
[5]:
../../_images/user-guide_plot-types_line-plot_7_0.svg

Controlling the axes#

Logarithmic axes#

[6]:
da.plot(scale={'x': 'log'})
[6]:
../../_images/user-guide_plot-types_line-plot_9_0.svg
[7]:
da.plot(norm='log')
[7]:
../../_images/user-guide_plot-types_line-plot_10_0.svg

Setting the axes limits#

To set the range of the vertical axis, use the vmin and vmax keyword arguments:

[8]:
da.plot(vmin=sc.scalar(-0.5, unit='m/s'), vmax=sc.scalar(1.5, unit='m/s'))
[8]:
../../_images/user-guide_plot-types_line-plot_12_0.svg

Note that if the unit in the supplied limits is not identical to the data units, an on-the-fly conversion is attempted. It is also possible to omit the units altogether, in which case it is assumed the unit is the same as the data unit.

[9]:
da.plot(vmin=-0.5, vmax=1.5)
[9]:
../../_images/user-guide_plot-types_line-plot_14_0.svg

To set the range of the horizontal axis, slice the data before plotting:

[10]:
da['x', 10:40].plot()
[10]:
../../_images/user-guide_plot-types_line-plot_16_0.svg

Overplotting#

The plot function will accept a dict of data arrays as an input, and will place all entries on the same axes (as long as all entries have the same units and dimension labels).

[11]:
pp.plot({'a': da, 'b': 0.2 * da})
[11]:
../../_images/user-guide_plot-types_line-plot_18_0.svg

This also works with a dataset:

[12]:
ds = pp.data.dataset1d()
ds.plot()
[12]:
../../_images/user-guide_plot-types_line-plot_20_0.svg

Customizing line styles for multiple entries#

If a single color is passed to plot when plotting a dict of data arrays, the color will apply to all entries

[13]:
pp.plot({'a': da, 'b': 0.2 * da}, color='red')
[13]:
../../_images/user-guide_plot-types_line-plot_22_0.svg

To control the style of the individual entries, the line properties should be a dict with the same keys as the input dict:

[14]:
pp.plot(
    {'a': da, 'b': 0.2 * da},
    color={'a': 'red', 'b': 'black'},
    linestyle={'a': 'solid', 'b': 'dashed'},
    marker=None,
)
[14]:
../../_images/user-guide_plot-types_line-plot_24_0.svg

Bin edges#

When the coordinate of the data contains bin-edges, Plopp represents it as a step function instead of markers:

[15]:
da = pp.data.histogram1d()
pp.plot(da)
[15]:
../../_images/user-guide_plot-types_line-plot_26_0.svg

Masks#

Masks are represented by black markers

[16]:
da = pp.data.data1d()
da.masks['large-x'] = da.coords['x'] > sc.scalar(30, unit='m')
pp.plot(da)
[16]:
../../_images/user-guide_plot-types_line-plot_28_0.svg

or a thick black line in the case of bin-edges

[17]:
da = pp.data.histogram1d()
da.masks['close-to-zero'] = abs(da.data) < sc.scalar(0.5, unit='m/s')
pp.plot(da)
[17]:
../../_images/user-guide_plot-types_line-plot_30_0.svg

Using a non-dimension coordinate#

For a dimension of name 'x', Plopp will use the corresponding coordinate of name 'x' to set the horizontal position of the points and the axis labels. To use a different coordinate, use the coords argument.

[18]:
da = pp.data.data1d()
da.coords['x2'] = da.coords['x'] ** 2
pp.plot(da, coords=['x2'])
[18]:
../../_images/user-guide_plot-types_line-plot_32_0.svg

Note that if no coordinate of name 'x' exists, a dummy one will be generated using scipp.arange.

Plotting one variable as a function of another#

af06a7c926504d64818bba5b287d9b40   New in version 23.10.0.

Sometimes it is useful, for quickly inspecting data, to plot one variable as a function of another, without having to first explicitly store them both in a DataArray.

For this, one can use a small dedicated function called xyplot:

[19]:
x = sc.arange('distance', 50.0, unit='m')
y = x**2

pp.xyplot(x, y)
[19]:
../../_images/user-guide_plot-types_line-plot_34_0.svg

Any additional keyword arguments are forwarded to the plot function:

[20]:
pp.xyplot(x, y, ls='solid', color='purple', marker=None, lw=3)
[20]:
../../_images/user-guide_plot-types_line-plot_36_0.svg