In [1]:
%matplotlib qt
import xarray as xr
from matplotlib import colors
from matplotlib.animation import FuncAnimation, writers, PillowWriter
import matplotlib.animation as animation
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import as ccrs
import cartopy
import numpy as np
import copy

Load in particle data, define the time range at which to plot (plottimes) and select the indices of the first timestep in the variable 'b'.

In [2]:
filename = ''
pfile = xr.open_dataset(str(filename), decode_cf=True)
lon =['lon'], np.nan)
lat =['lat'], np.nan)
time =['time'], np.nan)


plottimes = np.arange(time[0,0], np.nanmax(time), np.timedelta64(10, 'D'))
starttime = 20
b = time == plottimes[0+starttime]

The particle data in is available at

The animation consists of three figures: the northern hemisphere, the southern hemisphere and the oceanparcels logo. To organize their locations we use matplotlib.gridspec. The animation spans 12 frames and updates the particle positions based on the timestep in plottimes.

In [3]:
fig = plt.figure(figsize=(8,4))
gs = gridspec.GridSpec(ncols=8, nrows=4, figure=fig)

### Northern Hemisphere
ax1 = fig.add_subplot(gs[:, :4],projection=ccrs.NearsidePerspective(central_latitude=90, central_longitude=-30, satellite_height=15000000))
scat1 = ax1.scatter(lon[b], lat[b], marker='.', s=25, c='#AB2200',edgecolor='white', linewidth=0.15, transform=ccrs.PlateCarree())

### Southern Hemisphere
ax2 = fig.add_subplot(gs[:, 4:],projection=ccrs.NearsidePerspective(central_latitude=-90, central_longitude=-30, satellite_height=15000000))
scat2 = ax2.scatter(lon[b], lat[b], marker='.', s=25, c='#AB2200',edgecolor='white', linewidth=0.15, transform=ccrs.PlateCarree())

frames = np.arange(0, 20)

def animate(t):
    b = time == plottimes[t+starttime]
    scat1.set_offsets(np.vstack((lon[b], lat[b])).transpose())
    scat2.set_offsets(np.vstack((lon[b], lat[b])).transpose())
    return scat1, scat2

anim = animation.FuncAnimation(fig, animate, frames=frames, interval=150, blit=True)
fig.canvas.draw()  # needed for tight_layout to work with cartopy
# writergif = PillowWriter(fps=6) 
In [ ]: