{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotting Overview\n", "\n", "## Getting started\n", "\n", "`scipp` offers a number of different ways to plot data from a `DataArray` or a `Dataset`.\n", "It uses the `matplotlib` graphing library to do so, as well as the `pythreejs` project for 3D visualizations.\n", "\n", "Plotting functionality is available in two different ways:\n", "- using the `plot()` free function\n", "- using the `.plot()` method on a Scipp object (variable, data array or dataset)\n", "\n", "The difference between the two possible plot functions is that the free function can accept more input types than just the Scipp objects.\n", "It can also plot raw numpy arrays, as well as python dicts of Scipp variables or data arrays.\n", "For Scipp objects, the produced plot will be the same with either approach: \n", "Internally, the `.plot()` method just forwards the Scipp object to the free function `plot()`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import scipp as sc" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "N = 20\n", "M = 5\n", "d = sc.Dataset(\n", " data={\n", " 'data':sc.array(dims=['y'], values=100*np.random.rand(N)+50, unit='counts'),\n", " 'data_with_errors':sc.array(dims=['y'],\n", " values=50*np.random.rand(N) + 20.,\n", " variances=50*np.random.rand(N),\n", " unit='counts'),\n", " 'data_2d':sc.array(dims=['x', 'y'], values=10.0*np.random.rand(M, N), unit=sc.units.K)},\n", " coords={\n", " 'x':sc.array(dims=['x'], values=np.arange(M), unit=sc.units.m),\n", " 'y':sc.array(dims=['y'], values=np.arange(N), unit=sc.units.us)} )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The information in a data array or dataset is typically enough to create meaningful plots:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.plot(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice in above that the dataset contains three data items.\n", "The two 1-D items with dimension y are combined into a single 1-D plot.\n", "The third item is 2-D and plotted separately.\n", "\n", "All plots will use dimension coordinates values for each axis by default if they are available.\n", "For the plot above, x and y dimensions have associated coordinates.\n", "Notice that the units on the axes are also taken from the coordinates." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting slices or items of a dataset\n", "\n", "The usual indexing and slicing can be used to create plots of slices of data, or plots of individual items from a dataset.\n", "\n", "### Plot a single entry of a dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.plot(d['data'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or alternatively" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d['data'].plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plot a slice range" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.plot(d['y', 4:7])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plot a 1-D slice of 2-D data\n", "\n", "When slicing without a range, the dimensionality reduces.\n", "This can be used to, e.g., plot a 1-D slice through 2-D data:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.plot(d['x', 4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Logarithmic scale\n", "\n", "1-D data can be plotted on a logarithmic scale on one or both axes:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.plot(d, scale={'y': 'log'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Axis labels and axis order\n", "\n", "By default scipp uses coordinate values to label the axes.\n", "If a data array or dataset contains auxiliary coordinates, these can be used instead.\n", "This is configured using the `labels` keyword argument of `plot`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d.coords['xlabels'] = sc.Variable(dims=['x'], values=np.arange(M) + 15.)\n", "sc.plot(d['data_2d'], labels={'x': 'xlabels'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "*Note*\n", "\n", "The `labels` dict uses *dimension labels* as keys, i.e., the `'x'` above refers to the `x` dimension and *not* the x-axis of the matplotlib figure.\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This also works for attributes:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d['data_2d'].attrs['T'] = sc.Variable(dims=['x'], values=np.linspace(3, 50, M), unit=sc.units.K )\n", "sc.plot(d['data_2d'], labels={'x': 'T'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To control which dimensions are shown along which axes of the matplotlib figure use the transpose button in the toolbar, or transpose data before calling `plot`, e.g." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d['data_2d'].transpose().plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "*Note*\n", "\n", "Plot settings are cached in a scipp `config.yaml` file.\n", "See runtime-configuration for more information\n", "\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.10" } }, "nbformat": 4, "nbformat_minor": 4 }