问题
How should the syntaxis be for changing the limits of the color range when plotting with quivers in matplotlib? I have the following code:
PyPlot.ion()
xlim(1, 64)
ylim(1, 64)
flechitas = quiver(x, y, EFx, EFy, sqrt((EFx.*EFx+EFy.*EFy)),
pivot="middle", cmap="Blues")
cb=colorbar(flechitas)
Which produces an adecuate image but uses automatically detected range for the 5th argument (the color). In the manual of matplotlib it says that I can use the clim
keyword but if I put it inside the arguments or outside, after the xy limits, I get an error indicating that I must create an image first, with imageshow. If I do that, then I get a more obscure error, an AssertionError()
. My matplotlib is 1.3.1.
回答1:
Okey, I got it, this is the right way to do it with matplotlib 1.3.1
figure()
xlim(1,64)
ylim(1,64)
flechitas=quiver(x,y,EFx,EFy, sqrt((EFx.*EFx+EFy.*EFy)), units="x",
pivot="middle", cmap="Blues", width=0.4);
cb=colorbar(flechitas, clim(0,120));
So, clim(min, max)
must be an argument to colorbar(...)
, it seems.
回答2:
Use the matplotlib function clim
():
plt.clim(0,120)
Extended example:
import matplotlib.pyplot as plt
import numpy as np
X, Y = np.meshgrid(np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2))
U = np.cos(X)
V = np.sin(Y)
M = np.hypot(U, V)
plt.figure(figsize=(17,5))
plt.subplot(121)
Q = plt.quiver(X, Y, U, V, M)
plt.colorbar()
plt.subplot(122)
Q = plt.quiver(X, Y, U, V, M)
plt.colorbar()
plt.clim(0,1)
来源:https://stackoverflow.com/questions/31280498/change-color-limits-with-quiver-in-matplotlib