# Plotting wind barbs in python (no U,V component)

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?

• Possible duplicate of earthscience.stackexchange.com/questions/7012/… – Communisty Aug 3 '17 at 9:14
• 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 Aug 3 '17 at 9:31
• Possible duplicate of Plotting wind barbs in python – Communisty Aug 3 '17 at 10:09
• @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 Aug 3 '17 at 11:09

I can answer this in two steps

1. Calculate $u$ and $v$. This site 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 site.

• 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 Aug 11 '17 at 8:30
• Sure. In addition to those links, try looking here: 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 Aug 11 '17 at 11:37
• 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 Aug 11 '17 at 14:55
• Did you try levels= <array>, such as what contour and contourf use? – BarocliniCplusplus Aug 11 '17 at 16:24
• 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 Aug 11 '17 at 21:28

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!" (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):

Source: kartoweb.itc.nl

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.