Skip to content

Commit d5a9eee

Browse files
authored
Update README.md
1 parent 74db318 commit d5a9eee

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

README.md

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ There is no pip or conda install for this package at the moment
2424

2525
## Usage examples
2626

27-
### 1. Make animation of dataarray using a bespoke frame plotting function
27+
### 1. Make animation of a single dataarray using a bespoke frame plotting function
2828

2929
Import what we need:
3030

@@ -41,15 +41,16 @@ fp = <PATH TO NETCDF FILE>
4141
ds = xr.open_dataset(fp, chunks={'ocean_time':10})
4242
```
4343

44-
Next, define a single function to plot a single frame of this data. This function will be
45-
applied to the xarray dataset to create individual frames. The only things that matter
46-
to this package are that it takes an input argument called `data_ii` (which is data from
47-
a single iteration of the plotting) and outputs the `matplotlib.figure()` object. For example
48-
to apply the basic xarray `.plot()` routine with some preset params:
44+
Next, define a single function to plot a single frame of this data -- called a "Frame Function".
45+
This function will be applied to the xarray dataset to create individual frames.
46+
A Frame Function must take at least one DataArray or Dataset as input. It may also take multiple.
47+
In addition, it must return a single `matplotlib.figure()` object. For example, we can plot a simple
48+
pcolormesh and animate it as follows:
4949

5050
```python
51-
def frame_func( data_ii ):
52-
f = data_ii.plot(vmin = -.5, vmax = .5, cmap=plt.get_cmap('bwr', 12)).figure
51+
def frame_func( data ):
52+
f,a = plt.subplots(1,1)
53+
a.pcolormesh( data.lon, data.lat, data, vmin=-.4, vmax=.4, cmap=plt.get_cmap('Blues',12))
5354
return f
5455
```
5556

@@ -64,38 +65,26 @@ xAnimate.make_animation( ds.zeta, fp_out = './anim.gif',
6465
There are a number of optional arguments you can pass to this routine, which you can find
6566
in docstring of the function.
6667

67-
### 2. Make animation using the default Frame Functions from the `frames` module function
68+
### 2. Make animation using multiple dataarrays and/or datasets
6869

69-
If you are animating a single data array, you can call the xr.DataArray.plot() function by importing the
70-
`xAnimate.frames` module. This contains a `Plot` class, which can be used in place of a bespoke function.
71-
You can pass this class directly to the make_animation function (using the `frame_func` argument), passing
72-
any of the arguments for `xr.DataArray.plot()`.
73-
74-
An example might be a better way to explain:
70+
As mentioned in the previous section, you can also pass multiple datasets or dataarrays to
71+
a frame function and `make_animation()`. For example, maybe we want to plot a pcolormesh
72+
with a contour overlayed from a different dataset:
7573

7674
```python
77-
import xAnimate
78-
import xAnimate.frames as frames
79-
80-
# Read dataset from netcdf
81-
fp = <PATH TO NETCDF FILE>
82-
ds = xr.open_dataset(fp, chunks={'ocean_time':10})
83-
84-
# Make animation
85-
xAnimate.make_animation( ds.zeta,
86-
fp_out = './anim.gif',
87-
anim_dim = 'ocean_time',
88-
frame_func = frames.Plot(vmin = 0, cmap='Reds'))
75+
def frame_func( data, dataset ):
76+
f,a = plt.subplots(1,1)
77+
a.pcolormesh( data.lon, data.lat, data, vmin=-.4, vmax=.4, cmap=plt.get_cmap('Blues',12))
78+
a.contour( dataset.lon, dataset.lat, dataset.contourdata )
79+
return f
8980
```
9081

91-
Alternatively, you can instantiate `Plot()` before the make animation call:
92-
82+
Then, we can use `make_animation()` by passing our data array and datasets in a list:
9383
```python
94-
plot_func = frames.Plot(vmin = 0, vmax = 1, cmap='Reds')
95-
xAnimate.make_animation( ds.zeta,
96-
fp_out = './anim.gif',
84+
xAnimate.make_animation( [ds.zeta, ds], fp_out = './anim.gif',
9785
anim_dim = 'ocean_time',
98-
frame_func = plot_func)
86+
frame_func = frame_func)
9987
```
10088

101-
`frames.Plot()` is currently the only frame function class.
89+
When using multiple datasets, you must ensure that all inputs have the same time dimension,
90+
both in terms of length and name. Spatial dimensions can vary however.

0 commit comments

Comments
 (0)