Drawing barbs

A barb is a representation of the speed and direction of wind, and is mainly deployed by meteorology scientists. In theory, they can be used to visualize any type of two-dimensional vector quantities. They are similar to arrows (quivers), but the difference is that arrows represent vector magnitude by the length of the arrow, while barbs give more information about the vector's magnitude by employing lines or triangles as increments of magnitude.

We will explain what barbs are, how to read them, and how to visualize them using Python and matplotlib. Here's a typical set of barbs:

Drawing barbs

In the preceding diagram, the triangle, also known as flag, represents the largest increment.

A full line or barb represents a smaller increment; a half line is the smallest increment.

The increments are in the order of 5, 10, and 65 for a half-line, line, and triangle, respectively. The values here represent, for meteorologists at least, wind speed in nautical miles per hour (knots).

We ordered the barbs from left to right to represent the following magnitudes: 0, 5, 10, 15, 30, 40, 50, 60, and 100 knots. The direction here is the same for each barb and is from north to south, because the east-west speed component is 0 for each barb.

Getting ready

A barb can be created using a matplotlib function from matplotlib.pyplot.barbs.

The barbs function accepts various arguments, but we can also only specify X and Y coordinates, representing locations of observed data points. The second pair of arguments—U, V—represents the magnitude of the vector in north-south and east-west directions in knots.

Other arguments that can be useful are pivots, sizes, and various coloring arguments.

A pivot argument (pivot) represents the part of the arrow represented on the grid point. We get a pivot argument when the arrow rotates around this point. The arrow can rotate around the tip or middle, which are valid values for the pivot argument.

Because barbs consist of several parts, we can set up the coloring of any of those parts. So, we have a few color-related arguments that we can set up:

  • barbcolor: This defines the color of all the parts for a barb, except for flags
  • flagcolor This defines the color of any flag on the barb
  • facecolor: This argument is used if none of the preceding color arguments are specified (or the default value is read from rcParams)

If any of the preceding color-related arguments are specified, the argument facecolor is overridden. The facecolor argument is the one used in coloring polygons.

The size argument (sizes) specifies the ration of a feature to the length of the barb. This is a collection of coefficients that can be specified using any or all of the following keys:

  • spacing: This defines the space among features of the flag/barb
  • height: This defines the distance from the shaft to the top of a flag or barb
  • width: This defines the width of a flag
  • emptybarb: This defines the circle radius used for low magnitudes

How to do it...

Let's demonstrate how to use a barb function by performing the following steps:

  1. Generate a grid of coordinates to simulate observations.
  2. Simulate observational values for wind speed.
  3. Plot barb diagrams.
  4. Plot quivers to demonstrate different appearances.

The following code will generate the figure:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-20, 20, 8) 
y = np.linspace(  0, 20, 8) 

# make2D coordinates 
X, Y = np.meshgrid(x, y) 

U, V = X+25, Y-35 


# plot the barbs 
plt.subplot(1,2,1) 
plt.barbs(X, Y, U, V, flagcolor='green', alpha=0.75) 
plt.grid(True, color='gray') 

# compare that with quiver / arrows 
plt.subplot(1,2,2) 
plt.quiver(X, Y, U, V, facecolor='red', alpha=0.75) 

# misc settings 
plt.grid(True, color='grey') 
plt.show()

The preceding code renders two subplots as shown in the following figure:

How to do it...

How it works...

To illustrate how the same data can bring different information to light, we used barbs and quiver plots from matplotlib to visualize simulated observed wind data.

First, we used NumPy to generate samples of variations for x and y arrays. Then we used NumPy's meshgrid() function to create a 2D grid of coordinates where our observed data is sampled at certain coordinates. Finally, U and V are wind speed values in NS (north-south) and EW (east-west) directions in knots (nautical miles per hours). For the purpose of the recipe, we adjusted some values from the already available X and Y matrices.

We then divided the figure into two subplots, plotting barbs in the leftmost plot and arrow-patches in the rightmost plot. We adjusted the color and transparency of both the subplots slightly, as well as turned the grid on both the subplots.

There's more...

This is all fine on the northern hemisphere where the wind rotates in a counter-clockwise direction and the feathers (triangles, full lines, and half lines of the barb) point in the direction of lower pressure. On the southern hemisphere, this is inverted so our wind barb graph would not represent the data we are visualizing correctly.

We have to invert this direction of feathers. Luckily, the barbs function has the argument flip_barb. This argument can be of one single Boolean value (True or False) or a sequence of Boolean values such as the shape of other data arrays, when each item in the sequence specifies a flip decision for each barb.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.144.12