Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colormap with colored quiver

I am plotting a map with arrows on top of it. These arrows represent winddirections, average windspeed (per direction) and the occurence (per direction).

The direction is indicated by the direction of the arrow. The length of the arrow indicated the average windspeed in that direction. The color of the arrow indicates the occurence of winds in such a direction.

This all works fine with the script below:

windData = pd.read_csv(src+'.txt'), sep='\t', names=['lat', 'lon', 'wind_dir_start', 'wind_dir_end', 'total_num_data_points','num_data_points', 'avg_windspeed']).dropna()

# plot map
m = Basemap(llcrnrlon=minLon, llcrnrlat=minLat, urcrnrlon=maxLon, urcrnrlat=maxLat, resolution='i')
Left, Bottom = m(minLon, minLat)
Right, Top = m(maxLon, maxLat)

# get x y
x, y = m(windData['lon'], windData['lat'])

# angles
angleStart = -windData['wind_start']+90
angleStart[angleStart<0] = np.radians(angleStart[angleStart<0]+360.)

angleEnd = -windData['wind_end']+90
angleEnd[angleEnd<0] = np.radians(angleEnd[angleEnd<0]+360.)

angle = angleStart + math.radians(binSize/2.)

xux = np.cos(angle) * windData['avg_windspeed']
yuy = np.sin(angle) * windData['avg_windspeed']

# occurence
occurence = (windData['num_data_points']/windData['total_num_data_points'])

xi = np.linspace(minLon, maxLon, 300)
yi = np.linspace(minLat, maxLat, 300)

# plotting
## xux and yuy are used negatively because they are measured as "coming from" and displayed as "going to"
# To make things more readable I left a threshold for the occurence out
# I usually plot x, y, xux, yuy and the colors as var[occurence>threshold]
Q = m.quiver(x, y, -xux, -yuy, scale=75, zorder=6, color=cm.jet, width=0.0003*Width, cmap=cm.jet)
qk = plt.quiverkey(Q, 0.5, 0.92, 3, r'$3 \frac{m}{s}$', labelpos='S', fontproperties={'weight': 'bold'})
m.scatter(x, y, c='k', s=20*np.ones(len(x)), zorder=10, vmin=4.5, vmax=39.)

This plot shows the arrows well, but now I want to add a colormap that indicates the percentage of occurence next to the plot. How would I do this?

like image 815
Yorian Avatar asked May 31 '17 08:05

Yorian


People also ask

What is quiver plot in Python?

Quiver plot is a type of 2-D plot make up of vector lines in shape of arrows. These plots are mainly used to visualize gradients. The basic syntax of a quiver plot is: The plot below shows one quiver arrow, which is starting from (0,0) and is pointing towards the up and the right side at (1,1).

How to plot velocity vectors in Matplotlib quiver plot?

A matplotlib quiver plot is basically something that helps in displaying the velocity vectors as arrows with the components (u, v) at the points (x, y). To plot the coordinates specified above, we can use the following command in each corresponding pair of the elements present in x and y. x: It represents the x-coordinates of the arrow location.

How do I mask a reversed colormap?

The name for the reversed colormap. If it's None the name will be the name of the parent colormap + "_r". Set the color for masked values. Set the colors for masked ( bad) values and, when norm.clip = False, low ( under) and high ( over) out-of-range values.

What interval should X be in when using colormap?

For floats, X should be in the interval [0.0, 1.0] to return the RGBA values X*100 percent along the Colormap line. For integers, X should be in the interval [0, Colormap.N) to return RGBA values indexed from the Colormap with index X.


2 Answers

OK

Usual imports, plus import matplotlib

%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

Fake the data to be plotted (tx for the MCVE)

NP = 10
np.random.seed(1)
x = np.random.random(NP)
y = np.random.random(NP)
angle = 1.07+np.random.random(NP) # NE to NW
velocity = 1.50+np.random.random(NP)
o = np.random.random(NP)
occurrence = o/np.sum(o)
dx = np.cos(angle)*velocity
dy = np.sin(angle)*velocity

Create a mappable so that Matplotib has no reason to complain "RuntimeError: No mappable was found to use for colorbar creation."

norm = matplotlib.colors.Normalize()
norm.autoscale(occurrence)
cm = matplotlib.cm.copper

sm = matplotlib.cm.ScalarMappable(cmap=cm, norm=norm)
sm.set_array([])

and plot the data

plt.quiver(x, y, dx, dy, color=cm(norm(o)))
plt.colorbar(sm)
plt.show()

quiverplot cum colorbar

References:

  1. A logarithmic colorbar in matplotlib scatter plot ,

  2. Drawing a colorbar aside a line plot, using Matplotlib and

  3. Different colours for arrows in quiver plot.


P.S. In recent (for sure in 3.+) Matplotlib releases the cm.set_array incantation is no more necessary

like image 123
gboffi Avatar answered Oct 13 '22 22:10

gboffi


Do you want the colorbar to show the different wind speeds? If so, it might be sufficient to place plt.colorbar() between the lines Q = m.quiver(...) and qk = ....

like image 1
ml4294 Avatar answered Oct 13 '22 21:10

ml4294