{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import hvplot.xarray # noqa" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`vectorfield` accepts 2d arrays of magnitude and angle on a grid and produces an array of vectors. x and y can be 2d or 1d coordinates. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import xarray as xr\n", "import geoviews as gv\n", "import cartopy.crs as ccrs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def sample_data(shape=(20, 30)):\n", " \"\"\"\n", " Return ``(x, y, u, v, crs)`` of some vector data\n", " computed mathematically. The returned crs will be a rotated\n", " pole CRS, meaning that the vectors will be unevenly spaced in\n", " regular PlateCarree space.\n", "\n", " \"\"\"\n", " crs = ccrs.RotatedPole(pole_longitude=177.5, pole_latitude=37.5)\n", "\n", " x = np.linspace(311.9, 391.1, shape[1])\n", " y = np.linspace(-23.6, 24.8, shape[0])\n", "\n", " x2d, y2d = np.meshgrid(x, y)\n", " u = 10 * (2 * np.cos(2 * np.deg2rad(x2d) + 3 * np.deg2rad(y2d + 30)) ** 2)\n", " v = 20 * np.cos(6 * np.deg2rad(x2d))\n", "\n", " return x, y, u, v, crs\n", "\n", "xs, ys, U, V, crs = sample_data()\n", "\n", "mag = np.sqrt(U**2 + V**2)\n", "angle = (np.pi/2.) - np.arctan2(U/mag, V/mag)\n", "\n", "ds = xr.Dataset({'mag': xr.DataArray(mag, dims=('y', 'x'), coords={'y': ys, 'x': xs}),\n", " 'angle': xr.DataArray(angle, dims=('y', 'x'), coords={'y': ys, 'x': xs})}, \n", " attrs={'crs': crs})\n", "ds" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds.hvplot.vectorfield(x='x', y='y', angle='angle', mag='mag', hover=False).opts(magnitude='mag')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Geographic Data\n", "If a dataset has an attr called `crs` which is a cartopy object or a proj4 string, then just by setting the option `geo=True` will use the correct crs." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds.hvplot.vectorfield(x='x', y='y', angle='angle', mag='mag',\n", " hover=False, geo=True).opts(magnitude='mag') * gv.tile_sources.CartoLight" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 2 }