<p>The data format you have (magnitude and direction) is usually referred to as <em>polar co-ordinates</em>. The data format you need (horizontal and vertical offsets) is <em>Cartesian co-ordinates</em>. There's plenty of good material on this online. For a friendly introduction, see e.g. <a href="https://www.mathsisfun.com/polar-cartesian-coordinates.html" rel="nofollow noreferrer">this page on "Math is Fun!"</a> (or your favourite high-school maths textbook).</p>

<p>Converting between the two systems involves a bit of simple trigonometry, and the exact formulae depend on how you're measuring the angle in your polar co-ordinates. Mathematicians tend to measure anticlockwise from the positive x axis in radians. Geoscientists usually measure clockwise from the positive y axis in degrees (like a compass bearing):</p>

<p><a href="https://i.sstatic.net/Je0l8.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Je0l8.gif" alt="polar co-ordinates"></a><br>
<sup>Source: <a href="https://kartoweb.itc.nl/geometrics/Coordinate%20systems/coordsys.html" rel="nofollow noreferrer">kartoweb.itc.nl</a></sup></p>

<p>Assuming that this is the convention used in your data, the conversion formulae are refreshingly straighforward.</p>

<p>$x = d\sin(\alpha)$<br>
$y = d\cos(\alpha)$</p>

<p>One thing to watch out for: like most programming languages, Python uses radians, not degrees, for its trigonometric functions. If your angles are in degrees, don't forget to run them through the <code>math.radians()</code> function before taking sines and cosines!</p>

<p>Incidentally, having each measurement in a <em>separate</em> text-file is probably a recipe for inconvenience and confusion. If I were you, I'd have "combine measurements as columns in a single file" as the first step in my processing workflow.</p>

CC BY-SA 3.0
2017-08-03T11:04:02.380Z
2017-08-03T11:04:02.380Z
Pont
<p>I can answer this in two steps</p>

<ol>
<li><p>Calculate $u$ and $v$. This <a href="http://tornado.sfsu.edu/geosciences/classes/m430/Wind/WindDirection.html" rel="nofollow noreferrer">site</a> has formulas for $u$ and $v$. They are as follows: $$u=-|\vec{v}|\sin(\frac{\pi}{180}\phi)$$ and $$v=-|\vec{v}|\cos(\frac{\pi}{180}\phi)$$
where $\phi$ is the wind direction in the meteorological system, and $|\vec{v}|$ is the wind speed.</p></li>
<li><p>Having calculated $u$ and $v$, you can plot the wind barbs by following the example found at <a href="https://matplotlib.org/examples/pylab_examples/barb_demo.html" rel="nofollow noreferrer">this</a> site. </p></li>
</ol>

CC BY-SA 3.0
2017-08-11T08:30:24.893Z
Thank you for that. In addition to my previous question. Having lat, lon, u, v wind txt files, how could I use matplotlib in order to plot the wind barbs on a map (let's say UK map). I read the links that you gave me, but as I am newbie in python I can't find the way.
Stavros Keppas
CC BY-SA 3.0
2017-08-11T11:37:38.370Z
Sure. In addition to those links, try looking here: https://matplotlib.org/basemap/users/examples.html. The idea is to make a basemap with whatever projection & parameters you want, use it to convert to longitude & latitude to x & y, and plot the barbs on the basemap.
BarocliniCplusplus
CC BY-SA 3.0
2017-08-11T14:55:57.213Z
I finaly found the solution, doing sth like that:


m = Basemap(projection='merc',llcrnrlat=33,urcrnrlat=43,\
            llcrnrlon=18,urcrnrlon=30,lat_ts=20,resolution='i')
jet = plt.cm.get_cmap('jet')
x,y = m(lon, lat)
barbs = m.barbs(x,y,wind_u,wind_v,wind_spd_knots, cmap=jet,length=7,linewidth=0.8,pivot='middle')


However I can't find how to set limints on the colorscale for example like setting vmin, vmax. Any ideas on that?
Stavros Keppas
CC BY-SA 3.0
2017-08-11T16:24:22.343Z
Did you try levels= <array>, such as what contour and contourf use?
BarocliniCplusplus
CC BY-SA 3.0
2017-08-11T18:53:58.403Z
No. Could you explain that please?
Stavros Keppas
CC BY-SA 3.0
2017-08-11T19:14:33.330Z
So `levels=np.arange(0,5)` for example, will make contours at 0-4 in increments of 1. That is generally the other option to vmin and vmax. However, I am unsure of why you are trying to color the barbs. If that doesn't work, you could devise a brute force way of coloring the wind barbs.
BarocliniCplusplus
CC BY-SA 3.0
2017-08-11T19:50:34.607Z
It seems to be quite random. I am not sure how it works. I guess the problem is that in this command:

barbs = m.barbs(x,y,wind_u,wind_v, lelvels ,length=7,linewidth=0.8,pivot='middle')

there are two lists instead of just one, wind_u and wind_v. So the colorscale can't refer to the combination of u and v winds.
Stavros Keppas
CC BY-SA 3.0
2017-08-11T20:31:04.353Z
After looking through the demo (provided in the answer) I found a couple of different things. `levels` likely won't work. Try using things like `barb_increments` and `barbcolor` as seen in the example: https://matplotlib.org/examples/pylab_examples/barb_demo.html.
BarocliniCplusplus
CC BY-SA 3.0
2017-08-11T20:43:17.353Z
It seems that barb_increments sets the shape od wind barbs for specific values. Barbcolors cannot set specific colors for specific value.
Stavros Keppas
CC BY-SA 3.0
2017-08-11T21:20:42.147Z
barb_increments is a dictionary that tells Python what the different flags and barbs mean. I am unsure what the colors represent. You may want to play around with that, or just set one standard color.
BarocliniCplusplus
CC BY-SA 3.0
2017-08-11T21:28:25.933Z
Finally I found it. Using clim=[0,50], I set the range between 0 and 50 knots. the very last thing is how to plot the colorbar. plt.colorbar doesn't work.

I also found an alternative way by plotting arrows:
sc=m.quiver(x,y,wind_u,wind_v, wind_spd_knots,cmap=jet,clim=[0,50])
Stavros Keppas
CC BY-SA 4.0
2021-09-06T21:43:04.017Z
@gansub What do you mean? Latitude and longitude are static variables, whereas wind angle changes with time. The meteorological wind angle would be what is reported on a METAR, and the mathematical wind angle would be $\tan^{-1}\left(\frac{v}{u}\right)$. If you follow the website, it shows the equations in more detail.
BarocliniCplusplus
CC BY-SA 3.0
2017-08-03T15:52:13.670Z
2017-08-03T15:52:13.670Z
BarocliniCplusplus
<p>I've got 4 different .txt files for longitude, latitude, wind speed and wind direction (so no U and V components of the wind). Is there any way to plot wind barbs from such a dataset?</p>

CC BY-SA 3.0
2017-08-03T09:14:18.647Z
Possible duplicate of https://earthscience.stackexchange.com/questions/7012/plotting-wind-barbs-in-python
Communisty
CC BY-SA 3.0
2017-08-03T09:31:27.727Z
If that answer doesn't satisfy you because it is specifically for u,v wind then maybe you should do the transition from speed and direction to u,v first.
Communisty
CC BY-SA 3.0
2017-08-03T10:09:00.697Z
Possible duplicate of [Plotting wind barbs in python](https://earthscience.stackexchange.com/questions/7012/plotting-wind-barbs-in-python)
Communisty
CC BY-SA 3.0
2017-08-03T11:09:02.887Z
@Communisty I don't think it's a duplicate. The other question is mainly about how to read the data out of a netCDF file. This question is about how to convert some wind data from polar to Cartesian co-ordinates for plotting (though not phrased in those terms).
Pont
<meteorology><atmosphere><wind><python>
Plotting wind barbs in python (no U,V component)
Stavros Keppas
# Plotting wind barbs in python (no U,V component)

(Asked by: username_0 on 2017-08-03)

I've got 4 different .txt files for longitude, latitude, wind speed and wind direction (so no U and V components of the wind). Is there any way to plot wind barbs from such a dataset?

**Tags:** meteorology, atmosphere, wind, python

> **Comments:**
> **username_1**: Possible duplicate of https://earthscience.stackexchange.com/questions/7012/plotting-wind-barbs-in-python  
> **username_1**: If that answer doesn't satisfy you because it is specifically for u,v wind then maybe you should do the transition from speed and direction to u,v first.  
> **username_1**: Possible duplicate of [Plotting wind barbs in python](https://earthscience.stackexchange.com/questions/7012/plotting-wind-barbs-in-python)  
> **username_2**: @username_1 I don't think it's a duplicate. The other question is mainly about how to read the data out of a netCDF file. This question is about how to convert some wind data from polar to Cartesian co-ordinates for plotting (though not phrased in those terms).  

---

## 2 answers

### Answer by username_3 on 2017-08-03. Score: 3 | Accepted answer

I can answer this in two steps

1. Calculate $u$ and $v$. This [site](http://tornado.sfsu.edu/geosciences/classes/m430/Wind/WindDirection.html) has formulas for $u$ and $v$. They are as follows: $$u=-|\vec{v}|\sin(\frac{\pi}{180}\phi)$$ and $$v=-|\vec{v}|\cos(\frac{\pi}{180}\phi)$$
   where $\phi$ is the wind direction in the meteorological system, and $|\vec{v}|$ is the wind speed.
2. Having calculated $u$ and $v$, you can plot the wind barbs by following the example found at [this](https://matplotlib.org/examples/pylab_examples/barb_demo.html) site.

> **Comments:**
> **username_0**: Thank you for that. In addition to my previous question. Having lat, lon, u, v wind txt files, how could I use matplotlib in order to plot the wind barbs on a map (let's say UK map). I read the links that you gave me, but as I am newbie in python I can't find the way.  
> **username_3**: Sure. In addition to those links, try looking here: https://matplotlib.org/basemap/users/examples.html. The idea is to make a basemap with whatever projection & parameters you want, use it to convert to longitude & latitude to x & y, and plot the barbs on the basemap.  
> **username_0**: I finaly found the solution, doing sth like that:   m = Basemap(projection='merc',llcrnrlat=33,urcrnrlat=43,\             llcrnrlon=18,urcrnrlon=30,lat_ts=20,resolution='i') jet = plt.cm.get_cmap('jet') x,y = m(lon, lat) barbs = m.barbs(x,y,wind_u,wind_v,wind_spd_knots, cmap=jet,length=7,linewidth=0.8,pivot='middle')   However I can't find how to set limints on the colorscale for example like setting vmin, vmax. Any ideas on that?  
> **username_3**: Did you try levels= <array>, such as what contour and contourf use?  
> **username_0**: No. Could you explain that please?  
> **username_3**: So `levels=np.arange(0,5)` for example, will make contours at 0-4 in increments of 1. That is generally the other option to vmin and vmax. However, I am unsure of why you are trying to color the barbs. If that doesn't work, you could devise a brute force way of coloring the wind barbs.  
> **username_0**: It seems to be quite random. I am not sure how it works. I guess the problem is that in this command:  barbs = m.barbs(x,y,wind_u,wind_v, lelvels ,length=7,linewidth=0.8,pivot='middle')  there are two lists instead of just one, wind_u and wind_v. So the colorscale can't refer to the combination of u and v winds.  
> **username_3**: After looking through the demo (provided in the answer) I found a couple of different things. `levels` likely won't work. Try using things like `barb_increments` and `barbcolor` as seen in the example: https://matplotlib.org/examples/pylab_examples/barb_demo.html.  
> **username_0**: It seems that barb_increments sets the shape od wind barbs for specific values. Barbcolors cannot set specific colors for specific value.  
> **username_3**: barb_increments is a dictionary that tells Python what the different flags and barbs mean. I am unsure what the colors represent. You may want to play around with that, or just set one standard color.  
> **username_0**: Finally I found it. Using clim=[0,50], I set the range between 0 and 50 knots. the very last thing is how to plot the colorbar. plt.colorbar doesn't work.  I also found an alternative way by plotting arrows: sc=m.quiver(x,y,wind_u,wind_v, wind_spd_knots,cmap=jet,clim=[0,50])  
> **username_3**: @gansub What do you mean? Latitude and longitude are static variables, whereas wind angle changes with time. The meteorological wind angle would be what is reported on a METAR, and the mathematical wind angle would be $\tan^{-1}\left(\frac{v}{u}\right)$. If you follow the website, it shows the equations in more detail.  

---

### Answer by username_2 on 2017-08-03. Score: 3

The data format you have (magnitude and direction) is usually referred to as *polar co-ordinates*. The data format you need (horizontal and vertical offsets) is *Cartesian co-ordinates*. There's plenty of good material on this online. For a friendly introduction, see e.g. [this page on "Math is Fun!"](https://www.mathsisfun.com/polar-cartesian-coordinates.html) (or your favourite high-school maths textbook).

Converting between the two systems involves a bit of simple trigonometry, and the exact formulae depend on how you're measuring the angle in your polar co-ordinates. Mathematicians tend to measure anticlockwise from the positive x axis in radians. Geoscientists usually measure clockwise from the positive y axis in degrees (like a compass bearing):

[![polar co-ordinates](https://i.sstatic.net/Je0l8.gif)](https://i.sstatic.net/Je0l8.gif)  
Source: [kartoweb.itc.nl](https://kartoweb.itc.nl/geometrics/Coordinate%20systems/coordsys.html)

Assuming that this is the convention used in your data, the conversion formulae are refreshingly straighforward.

$x = d\sin(\alpha)$  
$y = d\cos(\alpha)$

One thing to watch out for: like most programming languages, Python uses radians, not degrees, for its trigonometric functions. If your angles are in degrees, don't forget to run them through the `math.radians()` function before taking sines and cosines!

Incidentally, having each measurement in a *separate* text-file is probably a recipe for inconvenience and confusion. If I were you, I'd have "combine measurements as columns in a single file" as the first step in my processing workflow.


---

