Date created: June 10, 2022
Author: P. Alexander Burnham

Summary Here we explore the process for plotting data in spacetime. Spacetime using the plotly interface for plotting which gives the user a lot of great functionality but due to it’s web interface design, plot windows open in a seperate browser window and are not shown in r markdown files. See the companion video to see how this works.

Loading file names and creating a file object

We will load in a previously created cube. with 101 daily time points and 2 variables


# spacetime function "read_data()" to create a file object
cube = load_cube(file="demoData/cubeDay.nc4")

# check to make sure the data laoded
cube.get_data_array()
<xarray.DataArray (variables: 2, lat: 149, lon: 297, time: 101)>
array([[[[-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,
           0.0000000e+00,  0.0000000e+00,  0.0000000e+00],
         [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,
           0.0000000e+00,  0.0000000e+00,  0.0000000e+00],
         ...,
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38]],

        [[-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,
           0.0000000e+00,  0.0000000e+00,  0.0000000e+00],
         [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,
           0.0000000e+00,  0.0000000e+00,  0.0000000e+00],
...
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38]],

        [[-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [ 8.1205005e+02,  8.1205005e+02,  1.2280000e+03, ...,
           1.2370000e+03,  1.2070000e+03,  1.1830000e+03],
         [ 2.0893887e+02,  2.0893887e+02,  3.6000000e+02, ...,
           8.3000000e+02,  8.1100000e+02,  7.6800000e+02],
         ...,
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38]]]],
      dtype=float32)
Coordinates:
  * variables  (variables) <U4 '10km' '1km'
  * lon        (lon) float32 -83.75 -83.67 -83.58 -83.5 ... -59.25 -59.17 -59.08
  * lat        (lat) float32 49.0 48.92 48.83 48.75 ... 36.92 36.83 36.75 36.67
  * time       (time) datetime64[ns] 2000-10-12 2000-10-13 ... 2001-01-20

Turning the cube into a dataframe

The majority of plotting libraries in both R and Python require the data to be structured as a dataframe. While storing data as a cube can be ccomputationally efficient, if the user wants to plot data outside of spacetime, a cube to dataframe function has been included. Here’s how it works…

# do the conversion
df = cube_to_dataframe(cube=cube)

# Here is how it looks
print(df)
##                lat        lon variables       time         value
## 0        49.000000 -83.750000      10km 2000-10-12 -3.400000e+38
## 1        49.000000 -83.750000      10km 2000-10-13 -3.400000e+38
## 2        49.000000 -83.750000      10km 2000-10-14 -3.400000e+38
## 3        49.000000 -83.750000      10km 2000-10-15 -3.400000e+38
## 4        49.000000 -83.750000      10km 2000-10-16 -3.400000e+38
## ...            ...        ...       ...        ...           ...
## 8939101  36.666668 -59.083332       1km 2001-01-16 -3.400000e+38
## 8939102  36.666668 -59.083332       1km 2001-01-17 -3.400000e+38
## 8939103  36.666668 -59.083332       1km 2001-01-18 -3.400000e+38
## 8939104  36.666668 -59.083332       1km 2001-01-19 -3.400000e+38
## 8939105  36.666668 -59.083332       1km 2001-01-20 -3.400000e+38
## 
## [8939106 rows x 5 columns]

We can now see that the dataset is in a pandas dataframe and is ready for any plotting outside of spacetime. It can even be written out as a .csv file or passed to R in reticulate as an R dataframe. Let’s look at spacetime’s plotting functionality.

Spatial plotting

If type is “space” spacetime will plot a heatmap with a time slider. You must select the variable you wish to plot, in this case 10km. The dataframe used to make the plot is returned if the user wishes to make further custom plots in other programs.

plot_cube(cube=cube, plot_type="space")

Temporal plotting

A temporal plot is similar, just select “time_series” and the summary method you wish to use across the space dimension.

plot_cube(cube=cube, plot_type="timeseries", summary="mean")