【问题标题】:Problem with Matplotlib 3D Quiver plot unit vectors which point in random directionsMatplotlib 3D Quiver绘图单元向量指向随机方向的问题
【发布时间】:2021-07-08 20:20:26
【问题描述】:

您好,我想生成一个指向 3D 空间中随机方向的单位向量,但我需要有关 quiver 3d 的帮助,因为这段代码给了我ValueError: need at least one array to concatenate。谢谢!

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
np.random.seed(420)
campione = 10 # I want generate 10 vectors to plot
phi = 2*np.pi * np.random.random(size=campione)
theta = np.arccos(1 - 2*np.random.random(size=campione))
vett3D = np.array([ np.sin(theta)*np.cos(phi), np.sin(theta)*np.sin(phi), np.cos(theta) ])    
origine = np.zeros((3,campione))
fig = plt.figure(num=None, figsize=(8, 6), dpi=800, facecolor='w', edgecolor='k')
ax = fig.gca(projection='3d')
ax.quiver(origine, vett3D)
plt.show()

【问题讨论】:

    标签: python numpy matplotlib random montecarlo


    【解决方案1】:

    以此为指导:https://matplotlib.org/stable/gallery/mplot3d/quiver3d.html

    import numpy as np
    import matplotlib.pyplot as plt
    
    np.random.seed(1)
    
    campione = 10 # I want generate 10 vectors to plot
    
    phi = np.pi * np.random.random(size=campione)
    theta = 2 * np.pi * np.random.random(size=campione)
    
    ax = plt.figure().add_subplot(projection='3d')
    
    # Make the grid
    points3d = np.random.uniform(low=-3, high=3, size=(campione, 3)) #Each row is a point x, y, z
    
    # # Make the direction data for the arrows
    u = np.cos(theta) * np.sin(phi) #Using spherical coordinates
    v = np.sin(theta) * np.sin(phi)
    w = np.cos(phi)
    
    ax.quiver(points3d[:, 0], points3d[:, 1], points3d[:, 2], u, v, w, normalize=False)
    
    ax.set_xlim(-4, 4)
    ax.set_ylim(-4, 4)
    ax.set_zlim(-5, 5)
    
    plt.show()
    

    【讨论】:

    • 谢谢。但我不明白为什么ax.quiver(origine[0],origine[1],origine[2], vett3D[0],vett3D[1],vett3D[2]) 有效,而ax.quiver(origine, vett3D) 无效! originevett3D 是一个由 3 个数组组成的数组。
    • @Gabri 因为ax.quiver() 需要六个参数,而ax.quiver(origine, vett3D) 你提供两个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 2018-12-16
    • 2015-08-20
    相关资源
    最近更新 更多