Matplotlib 3.0 Cookbook
上QQ阅读APP看书,第一时间看更新

There's more...

We have seen a basic stream plot in the previous section. We can control the density and thickness as functions of the speed and color of the stream lines. Here is the code and its output:

# Define the speed as a function of U and V
speed = np.sqrt (U*U + V*V)

# Varying line width along a streamline
lw = 5 * speed / speed.max()

strm = plt.streamplot(X, Y, U, V, density=[0.5, 1], color=V,
linewidth=lw)
plt.colorbar(strm.lines)

plt.title('Varying Density, Color and Line Width')

plt.show()

np.sqrt(U*U + V*V) defines the speed; line width (lw) is defined as a function of speed.plt.streamplot() draws the stream plot using the parameters supplied. density=[0.5, 1] specifies density values to be used, color=V specifies how the color of streamlines should change based on the values of V, and linewidth specifies how the width of streamlines should change: