在streamplot()中使用1D数组的Matplotlib

2024-04-25 13:45:20 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在这里发现了两个类似的问题:

How to use streamplot function when 1D data of x-coordinate, y-coordinate, x-velocity and y-velocity are available?

How to plot Streamlines with Matplotlib given 1-D arrays of X cords, Y cords, U components and V components

第一个问题涉及不同大小的数组(在我的例子中,X、Y、U和V的长度总是相同的),而第二个问题确实提供了一些更大的间隔,在问题后面变得难以理解,并且没有提供解决方案。在

继续我的问题,我有4个一维数组,每个向量所在的X坐标和Y坐标,然后是每个向量的U和V值。我试图用streamplot将向量场(我可以在.quiver中正确地可视化)可视化为一个流线可视化,但是我遇到了使U和V为2D的问题。我不完全理解第二个维度需要包含什么,所以任何澄清(和代码理想情况下都会很好)。在

我能提供的唯一代码是第二个链接的实现,但这对我不起作用,所以会被丢弃。在


Tags: andofto代码coordinateuse可视化components
1条回答
网友
1楼 · 发布于 2024-04-25 13:45:20

Use griddata(另请参见^{})将一维数据插值到二维网格。在

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as interpolate

# lowercase variables are 1D arrays
x = np.linspace(0, 2 * np.pi, 10)
y = np.sin(x)
u = np.cos(x)
v = np.sin(x)

# capitalized variables are 2D arrays
xi = np.linspace(x.min(), x.max(), 100)
yi = np.linspace(y.min(), y.max(), 100)
X, Y = np.meshgrid(xi, yi)
U = interpolate.griddata((x, y), u, (X, Y), method='cubic')
V = interpolate.griddata((x, y), v, (X, Y), method='cubic')

plt.figure()
plt.quiver(x, y, u, v, scale_units='xy', angles='xy', scale=1.5)
plt.streamplot(X, Y, U, V, color=U**2+V**2, linewidth=2, cmap=plt.cm.autumn)
plt.show()

enter image description here


^{pr2}$

收益率

enter image description here

相关问题 更多 >