Vector Field#

A vector field plot plot of a synthetic dataset simulating wind speed and direction.

import hvplot.xarray  # noqa
import numpy as np
import xarray as xr

def sample_data(shape=(20, 30)):
    x = np.linspace(311.9, 391.1, shape[1])
    y = np.linspace(-23.6, 24.8, shape[0])
    x2d, y2d = np.meshgrid(x, y)
    u = 10 * (2 * np.cos(2 * np.deg2rad(x2d) + 3 * np.deg2rad(y2d + 30)) ** 2)
    v = 20 * np.cos(6 * np.deg2rad(x2d))
    return x, y, u, v

xs, ys, U, V = sample_data()
mag = np.sqrt(U**2 + V**2)
angle = (np.pi/2.) - np.arctan2(U/mag, V/mag)
ds = xr.Dataset({
    'mag': xr.DataArray(mag, dims=('y', 'x'), coords={'y': ys, 'x': xs}),
    'angle': xr.DataArray(angle, dims=('y', 'x'), coords={'y': ys, 'x': xs})
})

ds.hvplot.vectorfield(
    x='x',
    y='y',
    angle='angle',
    mag='mag',
    color='mag',
    cmap='viridis',
    colorbar=True,
    title='Vector Field (Bokeh)',
).opts(magnitude='mag')
This web page was generated from a Jupyter notebook and not all interactivity will work on this website.