Plotting particle distributions with holoviews


In [17]:
import numpy as np
from netCDF4 import Dataset
import holoviews as hv
from postladim import ParticleFile
hv.extension('bokeh')


Background map

Make a background bathymetric map. A simple land representation is given by colouring the land cells in the ROMS file. Take the logarithm of the bathymetry to enhance topographic details in the shallow North Sea.


In [18]:
# Read bathymetry and land mask
with Dataset('../data/ocean_avg_0014.nc') as ncid:
    H = ncid.variables['h'][:, :]
    M = ncid.variables['mask_rho'][:, :]
jmax, imax = M.shape

# Select sea and land features
H = np.where(M > 0, H, np.nan)  # valid at sea
M = np.where(M < 1, M, np.nan)  # valid on land

# Make land image
ds_land = hv.Dataset((np.arange(imax), np.arange(jmax), M), ['x', 'y'], 'Land mask')
im_land = ds_land.to(hv.Image, kdims=['x', 'y'], group='land')

# Make bathymetry image
ds_bathy = hv.Dataset((np.arange(imax), np.arange(jmax), -np.log10(H)),
                      ['x', 'y'], 'Bathymetry')
im_bathy = ds_bathy.to(hv.Image, kdims=['x', 'y'])

background = im_bathy * im_land

Particle plot function

Open the particle file and make a function to make a Scatter element of the particle distribution at a given time step.


In [19]:
pf = ParticleFile('line.nc')

def pplot(timestep):
    """Scatter plot of particle distibution at a given time step"""
    X, Y = pf.position(timestep)
    return background * hv.Scatter((X, Y))

Still images

Set a greyish colour on land and use shades of blue at sea. Show initial and final particle distributions.


In [20]:
%%opts Image (cmap='blues_r' alpha=0.7) 
%%opts Image.land (cmap=['#AABBAA'])    
%%opts Scatter (color='red')

pplot(0) + pplot(pf.num_times-1)  # Final particle distribution


Out[20]:

Dynamic map

Make a DynamicMap of all the particle distributions.


In [21]:
%%output size=150
%%opts Scatter (color='red')

dmap = hv.DynamicMap(pplot, kdims=['timestep'])
dmap.redim.range(timestep=(0, pf.num_times-1))


Out[21]:

In [ ]: