我想在Python中使用3D箭图绘制3D速度矢量场,来自3个数据文件(。npy),其给予x、y和z方向上的速度的u、v和w分量。这就是我到目前为止所做的,也显示了错误消息。我如何创建一个三维 Flutter 图从三个三维。npy数据文件?我还看了其他例子here和here。下面的代码改编自此处的二维 Flutter 图示例。
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.pyplot import figure
from mpl_toolkits.mplot3d import proj3d
c1 = 1
nx, ny, nz = 284, 160, 160
x1 = range(nx)
y1 = range(ny)
z1 = range(nz)
U = np.load("u.npy") # 3d array file
V = np.load("v.npy") # 3d array file
W = np.load("w.npy") # 3d array file
X1, Y1, Z1 = np.meshgrid(x1, y1, z1)
fig = plt.figure(figsize=(10,10))
ax = fig.gca(projection='3d')
ax.set_title("pivot='mid'; every 10th arrow; units='velocity vector' time=" + str(c1))
Q = ax.quiver(X1[::10, ::10], Y1[::10, ::10], Z1[::10, ::10], U[::10, ::10],
V[::10, ::10], W[::10, ::10], pivot='mid', units='inches')
#Not sure if it should be formatted as below:
#Q = ax.quiver(X1[::10, ::10, ::10], Y1[::10, ::10, ::10], Z1[::10, ::10, ::10], U[::10, ::10, ::10],
#V[::10, ::10, ::10], W[::10, ::10, ::10], pivot='mid', units='inches')
qk = ax.quiverkey(Q, 0.9, 0.9, 5, r'$1 \frac{m}{s}$', labelpos='E', coordinates='figure') #possibly for 2D (X, Y) only.
ax.scatter(X1[::10, ::10], Y1[::10, ::10], Z1[::10, ::10], color='c', s=0)
plt.tight_layout()
plt.savefig('3D_video_velocity_' + str(c1) + '.png')
错误信息:
$ python3 test_3d_quiver_plot_1a.py
Traceback (most recent call last):
File "test_3d_quiver_plot_1a.py", line 69, in <module>
Q = ax.quiver(X1[::10, ::10], Y1[::10, ::10], Z1[::10, ::10], U[::10, ::10],
File "/home/brendan/.local/lib/python3.8/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 2628, in quiver
bcast = np.broadcast_arrays(*input_args, *masks)
File "<__array_function__ internals>", line 5, in broadcast_arrays
File "/home/brendan/.local/lib/python3.8/site-packages/numpy/lib/stride_tricks.py", line 264, in broadcast_arrays
shape = _broadcast_shape(*args)
File "/home/brendan/.local/lib/python3.8/site-packages/numpy/lib/stride_tricks.py", line 191, in _broadcast_shape
b = np.broadcast(*args[:32])
ValueError: shape mismatch: objects cannot be broadcast to a single shape
结果应该如下所示:
2条答案
按热度按时间bnl4lu3b1#
基于上述评论,要创建3D Flutter 图,
X1, Y1, Z1, U, V, W
必须具有相同的形状。尝试修改以下代码行:
请注意,我将
nx, ny, nz = 284, 160, 160
更改为nx, ny, nz = 160, 284, 160
。这将给予X1, Y1, Z1
提供正确的形状。mfuanj7w2#
我发现下面的代码从3个数据文件中生成了一个3D振动图(3D阵列中。npy文件)。代码改编自@tacaswell。
生成的3d箭图显示在这里(我还没有解决如何使箭头更大):x1c 0d1x