Levi Cowan
Levi Cowan

Reputation: 799

Python matplotlib barbs/quiver map colors to different sets of values

I am trying to create a barb vector plot in matplotlib and map some colors to specific magnitudes: for example, to have vectors with magnitudes between 10 and 20 plotted as blue, and between 20 and 30 as rgb(0,15,40), and so on. The documentation for the barbs and quiver functions (they are similar) mentions the C input arg:


barb(X, Y, U, V, C, **kw)

Arguments:

X, Y: The x and y coordinates of the barb locations (default is head of barb; see pivot kwarg)

U, V: Give the x and y components of the barb shaft

C: An optional array used to map colors to the barbs


However, this is very vague, and after searching all over Google, I am no closer to understanding how to use this color array in specific ways. I managed to discover that by setting C equal to the array of vector magnitudes and specifying the "cmap" kwarg, it will map the barbs to the specified colormap, as in the example code below. However, this is not what I want. I want to control the colors of specific groups of magnitudes. Any help would be appreciated.

Example code:

from matplotlib import pyplot as plt
from numpy import arange,meshgrid,sqrt

u,v = arange(-50,51,10),arange(-50,51,10)
u,v = meshgrid(u,v)
x,y = u,v
C = sqrt(u**2 + v**2)
plt.barbs(x,y,u,v,C,cmap=plt.cm.jet)
plt.show()

Resulting plot image link: (sorry can't post images directly yet)

http://i49.tinypic.com/xombmc.jpg

Upvotes: 4

Views: 8578

Answers (2)

zaheer
zaheer

Reputation: 143

sx = 0
ex = 135
sy = 0
ey = 234
plt.barbs(x[sx:ex:5, sy:ey:5], y[sx:ex:5, sy:ey:5],
        u[sx:ex:5, sy:ey:5], v[sx:ex:5, sy:ey:5],
        u[sx:ex:5, sy:ey:5], cmap='coolwarm',
        linewidth=1)

try this for "different color barbs w.r.t wind speed"

Upvotes: 0

imsc
imsc

Reputation: 7840

You can get it by discretizing the map.

import matplotlib as mpl 
import pyplot as plt
from numpy import arange,meshgrid,sqrt

u,v = arange(-50,51,10),arange(-50,51,10)
u,v = meshgrid(u,v)
x,y = u,v
C = sqrt(u**2 + v**2)
cmap=plt.cm.jet
bounds = [10, 20, 40, 60]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
img=plt.barbs(x,y,u,v,C,cmap=cmap,norm=norm)
plt.colorbar(img, cmap=cmap, norm=norm, boundaries=bounds, ticks=bounds)
plt.show()

enter image description here

Upvotes: 3

Related Questions