# Interactively plotting high-res vector field (XArray + Holoviz)

**URL:** <https://discourse.pangeo.io/t/interactively-plotting-high-res-vector-field-xarray-holoviz/3809>\
**Category:** Pangeo Showcase\
**Created:** [October 31, 2023, 2:33pm UTC](https://discourse.pangeo.io/t/interactively-plotting-high-res-vector-field-xarray-holoviz/3809 "2023-10-31T14:33:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![itss](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/itss/32/1740_2.png) [@itss](https://discourse.pangeo.io/u/itss)\
**Post date:** [October 31, 2023, 2:33pm UTC](https://discourse.pangeo.io/t/interactively-plotting-high-res-vector-field-xarray-holoviz/3809/1 "2023-10-31T14:33:48Z")

</div>

This starts with the problem discussed [here](https://discourse.holoviz.org/t/highres-quiver-plot-returning-memory-error/6261/25). If we follow the example from documentation [Vectorfield — hvPlot 0.9.0 documentation](https://hvplot.holoviz.org/reference/xarray/vectorfield.html), but set to a high number of points, this is going to return either an overcrowded plot or memory error. Thanks @ahuang11 and @jbednar for the help with the discussion!

![image](https://canada1.discourse-cdn.com/flex030/uploads/pangeo/original/2X/d/d407d1ad972e99ab8116ebb8476189c46892808e.jpeg)

The solution is to create a `streams` to check the x and y range of the view and use that to check the size of the bins to perform a box average using `xarray`. Thus, zooming out will show the large scale, with increasing resolution by zooming in.

**The example below works sharply with 2000 x 2000 = 4,000,000 points!!**  
To make it clear, the code works because it coarsen the data by box averaging to keep always 10 x 10 = 100 arrows in the view (user defined). Of course, if there is nothing to average, it will just show all arrows.

![ezgif.com-video-to-gif (4)](https://canada1.discourse-cdn.com/flex030/uploads/pangeo/original/2X/f/fd3a258f5d11b3ca780f190ed4a1af01d2f67f19.gif)

Full reproducible code:

```python
import numpy as np
import xarray as xr
import hvplot.xarray # noqa
import holoviews as hv
import panel as pn

def sample_data(shape=(2000, 2000)):
    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.0) - 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}),
    }
)

def create_quiver(x_range, y_range, nmax = 10):
    if x_range is None or y_range is None:
        xs, ys = ds.x.size, ds.y.size
        ix, iy = xs // nmax, ys // nmax
    
        ix = 1 if ix <= 0 else ix
        iy = 1 if iy <= 0 else iy
    
        sub = ds.coarsen(x = ix, y = iy, side = "center", boundary = "trim").mean()
        quiver = sub.hvplot.vectorfield(
            x="x",
            y="y",
            mag="mag",
            angle="angle",
            hover=False,
        ).opts(magnitude="mag")
        return quiver

    sub = ds.sel(x = slice(*x_range), y = slice(*y_range))

    xs, ys = sub.x.size, sub.y.size
    ix, iy = xs // nmax, ys // nmax

    ix = 1 if ix <= 0 else ix
    iy = 1 if iy <= 0 else iy

    sub = sub.coarsen(x = ix, y = iy, side = "center", boundary = "trim").mean()
    
    quiver = sub.hvplot.vectorfield(
        x="x",
        y="y",
        mag="mag",
        angle="angle",
        hover=False,
    ).opts(magnitude="mag")
    
    return quiver

range_xy = hv.streams.RangeXY()
filtered = hv.DynamicMap(create_quiver, streams=[range_xy])
range_xy.source = filtered
filtered

```
