且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Matplotlib绘制流线-Python

更新时间:2022-03-12 04:45:39

pylab示例使用 Y,X ,然后使用numpy.mgrid绘制流图的X,Y.

The pylab examples use Y,X then plot X,Y for streamplots using numpy.mgrid.

import numpy as np
import matplotlib.pyplot as plt

Y, X = np.mgrid[-3:3:100j, -3:3:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U*U + V*V)

plt.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=plt.cm.autumn)
plt.colorbar()

f, (ax1, ax2) = plt.subplots(ncols=2)
ax1.streamplot(X, Y, U, V, density=[0.5, 1]    
lw = 5*speed/speed.max()
ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)

plt.show()

来自此处.

y,x = numpy.mgrid[-2:2:4j,-2:2:4j]
x = [[-2.         -0.66666667  0.66666667  2.        ]
    [-2.         -0.66666667  0.66666667  2.        ]
    [-2.         -0.66666667  0.66666667  2.        ]
    [-2.         -0.66666667  0.66666667  2.        ]]


y = [[-2.         -2.         -2.         -2.        ]
     [-0.66666667 -0.66666667 -0.66666667 -0.66666667]
     [ 0.66666667  0.66666667  0.66666667  0.66666667]
     [ 2.          2.          2.          2.        ]]

关于数据在x和y轴(-2,2等)方面的外观似乎有直接关系.类似于x轴,而y值类似于y轴.

There seems a direct relation with how the data looks in regard to x and y axis, the -2,2 etc.. resembling an x-axis and y values resembling a y-axis.