# Plot wind vectors from a XArray DataArray

**URL:** <https://forum.generic-mapping-tools.org/t/plot-wind-vectors-from-a-xarray-dataarray/1002>\
**Category:** PyGMT Q&A\
**Created:** [November 2, 2020, 8:21pm UTC](https://forum.generic-mapping-tools.org/t/plot-wind-vectors-from-a-xarray-dataarray/1002 "2020-11-02T20:21:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdassink](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/jdassink/32/673_2.png) [@jdassink](https://forum.generic-mapping-tools.org/u/jdassink)\
**Post date:** [November 2, 2020, 8:21pm UTC](https://forum.generic-mapping-tools.org/t/plot-wind-vectors-from-a-xarray-dataarray/1002/1 "2020-11-02T20:21:19Z")

</div>

Hi!

I am trying to plot temperature and wind data from a Xarray data array.

To show what I want to achieve, I have attached an example Matplotlib plot and a GMT plot that somewhat mimics it, here still without the wind vectors. The plot is made with GMT 6.1.1 and pygmt 0.2.0.

I am getting stuck trying to plot the wind vectors.

How should I proceed in pygmt? I tried:

```auto
u=ds.u.isel(isobaricInhPa=0)
v=ds.v.isel(isobaricInhPa=0)
fig.plot(lons.values, lats.values, direction=[u.values, v.values], style='V')

```

which yields:

```auto
    ---------------------------------------------------------------------------
    GMTInvalidInput Traceback (most recent call last)
    <ipython-input-174-4dd1ef6c1a08> in <module>
         36 fig.coast(projection=PROJECTION, region=REGION, frame="g", land="gray")
         37 fig.grdimage(windspeed.isel(isobaricInhPa=0), n='b', Q=True)
    ---> 38 fig.plot(lons.values, lats.values, direction=[u.values, v.values], style='V')
         39 fig.coast(projection=PROJECTION, region=REGION, frame="g", shorelines=True)
         40 fig.text(position='BL',text='d.', font=label_font)

    ~/opt/miniconda3/envs/pygmt/lib/python3.6/site-packages/pygmt/helpers/decorators.py in new_module(*args, **kwargs)
        235 if alias in kwargs:
        236 kwargs[arg] = kwargs.pop(alias)
    --> 237 return module_func(*args, **kwargs)
        238 
        239 new_module.aliases = aliases

    ~/opt/miniconda3/envs/pygmt/lib/python3.6/site-packages/pygmt/helpers/decorators.py in new_module(*args, **kwargs)
        372 kwargs[arg] = separators[fmt].join(f"{item}" for item in value)
        373 # Execute the original function and return its output
    --> 374 return module_func(*args, **kwargs)
        375 
        376 return new_module

    ~/opt/miniconda3/envs/pygmt/lib/python3.6/site-packages/pygmt/base_plotting.py in plot(self, x, y, data, sizes, direction, **kwargs)
        561 )
        562 
    --> 563 with file_context as fname:
        564 arg_str = " ".join([fname, build_arg_string(kwargs)])
        565 lib.call_module("plot", arg_str)

    ~/opt/miniconda3/envs/pygmt/lib/python3.6/contextlib.py in __enter__ (self)
         79 def __enter__ (self):
         80 try:
    ---> 81 return next(self.gen)
         82 except StopIteration:
         83 raise RuntimeError("generator didn't yield") from None

    ~/opt/miniconda3/envs/pygmt/lib/python3.6/site-packages/pygmt/clib/session.py in virtualfile_from_vectors(self, *vectors)
       1160 rows = len(arrays[0])
       1161 if not all(len(i) == rows for i in arrays):
    -> 1162 raise GMTInvalidInput("All arrays must have same size.")
       1163 
       1164 family = "GMT_IS_DATASET|GMT_VIA_VECTOR"

    GMTInvalidInput: All arrays must have same size.

```

Any help would be greatly appreciated!  
Jelle

p.s. the `fig.plot()` is part of a larger pygmt call, for context:

```auto
label_font = '14p,Helvetica-Bold,black'
REGION = 'g'
PROJECTION = 'G-35/60/8c'
# Figuur D
fig.coast(projection=PROJECTION, region=REGION, frame="g", land="gray")
fig.grdimage(windspeed.isel(isobaricInhPa=0), n='b', Q=True)
fig.plot(lons.values, lats.values, direction=[u.values, v.values], style='V')
fig.coast(projection=PROJECTION, region=REGION, frame="g", shorelines=True)
fig.text(position='BL',text='d.', font=label_font)

```

 ![temperature-wind-pl](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/1X/5c1eef62052968054c5fe5f1f2221729a76d8a74.jpeg)

 ![ecmwf_gmt](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/1X/2825e7f27da62726685b93ade770740669515b8e.png)

---

<div class="post-metadata">

**Author:** ![weiji14](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/weiji14/32/33_2.png) [@weiji14](https://forum.generic-mapping-tools.org/u/weiji14)\
**Post date:** [November 3, 2020, 9:21am UTC](https://forum.generic-mapping-tools.org/t/plot-wind-vectors-from-a-xarray-dataarray/1002/2 "2020-11-03T09:21:32Z")

</div>

Hi @jdassink, welcome to the GMT forum!

I’m not too familiar with plotting velocity vectors, but the `GMTInvalidInput` error message you’re getting indicates that your arrays (`lons`, `lats`, `u` and `v`) are probably of different lengths. Perhaps do a `print(len(lons.values), len(lats.values), len(u.values), len(v.values))` and see if they are the same.

If not, maybe upload a sample of your `xarray.DataArray` (e.g. as a zipped NetCDF file) so that we can take a look at what’s wrong?

As an aside, if you want to get the pointy colorbar like in the matplotlib plot, do `fig.colorbar(position="JBC+e")` (key is the `+e` which adds sidebar triangles). See also [https://docs.generic-mapping-tools.org/latest/colorbar.html#d](https://docs.generic-mapping-tools.org/latest/colorbar.html#d).

P.S. There’s a PR in progress for `velo` at [https://github.com/GenericMappingTools/pygmt/pull/525](https://github.com/GenericMappingTools/pygmt/pull/525) which may be of interest if you need to plot more complicated velocity vectors (e.g. with error ellipses and annotations).

---

<div class="post-metadata">

**Author:** ![jdassink](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/jdassink/32/673_2.png) [@jdassink](https://forum.generic-mapping-tools.org/u/jdassink)\
**Post date:** [November 12, 2020, 5:48pm UTC](https://forum.generic-mapping-tools.org/t/plot-wind-vectors-from-a-xarray-dataarray/1002/3 "2020-11-12T17:48:38Z")

</div>

Hi @weiji14, thanks for your answer.

I tried a bit more and decided to use `grdvector` by using the GMT clib directly.  
I advanced a bit in my figure, and now am almost able to produce what I want. Basically the call that is use is:

```auto
fig.coast(projection=PROJECTION, region=REGION, frame="g", land="gray")
fig.grdimage(windspeed.isel(isobaricInhPa=0), n='b', Q=True)
fig.coast(projection=PROJECTION, region=REGION, frame="g", shorelines=True)
ds.u.isel(isobaricInhPa=0).to_netcdf('u.grd')
ds.v.isel(isobaricInhPa=0).to_netcdf('v.grd')
with pygmt.clib.Session() as session:
    session.call_module('grdvector', 
                        f'u.grd v.grd -I10d/10d -Gwhite -Q0.1i+e+gwhite -W0.8p,black -Si0.1d')
fig.text(position='BL',text='d.', font=label_font)

```

and so forth for the other subfigures.

I get the following plot:

 ![gmt_20201112](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/1X/3949c856a88516858018c379b64f15a4be460613.jpeg)

It shows these weird effects around the poles. Any idea?

---

<div class="post-metadata">

**Author:** ![thomas.schouten](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@thomas.schouten](https://forum.generic-mapping-tools.org/u/thomas.schouten)\
**Post date:** [May 25, 2022, 9:20am UTC](https://forum.generic-mapping-tools.org/t/plot-wind-vectors-from-a-xarray-dataarray/1002/4 "2022-05-25T09:20:21Z")

</div>

Hi!

I have been having the same error message (`GMTInvalidInput: All arrays must have same size`). I checked the length of each array and they are equal:

 ![image](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/b/b9411005fa4b7c06407e4ac5fc226dd4c1157e24.png)

Here is what I try to do:

```
fig = pygmt.Figure()
fig.basemap(region=region, projection=proj, frame=True)
fig.grdimage("ETOPO1_Ice_g_geotiff.tif", projection=proj, frame=True, cmap="oleron")
fig.colorbar(frame='af+l"Topography (m)"')
fig.plot(
    x=lon,
    y=lat,
    direction=[[azi],[mag]],
    style="V",
    pen="1p",
    color="black",
)
fig.savefig("tectonics_map.png")
fig.show()

```

Many thanks!

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [May 25, 2022, 11:10am UTC](https://forum.generic-mapping-tools.org/t/plot-wind-vectors-from-a-xarray-dataarray/1002/5 "2022-05-25T11:10:20Z")

</div>

> [@thomas.schouten](#):
>
> `direction=[[azi],[mag]]`

Despite my PyIgnorance I strongly suspect of the above line

---

<div class="post-metadata">

**Author:** ![thomas.schouten](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@thomas.schouten](https://forum.generic-mapping-tools.org/u/thomas.schouten)\
**Post date:** [May 25, 2022, 11:11am UTC](https://forum.generic-mapping-tools.org/t/plot-wind-vectors-from-a-xarray-dataarray/1002/6 "2022-05-25T11:11:06Z")

</div>

Hi Joaquim! Would you have any suggestion how I should code this instead?

Thanks!  
Thomas

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [May 25, 2022, 11:29am UTC](https://forum.generic-mapping-tools.org/t/plot-wind-vectors-from-a-xarray-dataarray/1002/7 "2022-05-25T11:29:49Z")

</div>

Sure, remove that trouble-maker " : " from file.

EDIT: hoops, we are mixing the threads.

---

<div class="post-metadata">

**Author:** ![weiji14](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/weiji14/32/33_2.png) [@weiji14](https://forum.generic-mapping-tools.org/u/weiji14)\
**Post date:** [May 25, 2022, 2:27pm UTC](https://forum.generic-mapping-tools.org/t/plot-wind-vectors-from-a-xarray-dataarray/1002/8 "2022-05-25T14:27:21Z")

</div>

Hmm, looking at [https://www.pygmt.org/v0.6.1/api/generated/pygmt.Figure.plot.html](https://www.pygmt.org/v0.6.1/api/generated/pygmt.Figure.plot.html), I’m guessing that you should do `direction=[azi, mag]` instead? What type of Python objects are `azi` and `mag`?

---

<div class="post-metadata">

**Author:** ![thomas.schouten](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@thomas.schouten](https://forum.generic-mapping-tools.org/u/thomas.schouten)\
**Post date:** [May 27, 2022, 8:45am UTC](https://forum.generic-mapping-tools.org/t/plot-wind-vectors-from-a-xarray-dataarray/1002/9 "2022-05-27T08:45:53Z")

</div>

Fixed it! I needed to plot it as `direction=[azi, mag]` indeed. Also, my angles were in radians, not degrees, so that’s why I kept running into the problem of only north-facing vectors when it did work 😅
