【问题标题】:3D vector diagram in python - arrow heads not showing properlypython中的3D矢量图-箭头未正确显示
【发布时间】:2016-07-05 04:03:30
【问题描述】:

这是我从下面的代码中得到的情节:

    aa = np.zeros(len(self.depthrange))
    bb = np.zeros(len(self.depthrange))

    for i in range(0, self.n):
        goodin = ~np.isnan(self.u[:, i])
        bb[i] = self.u[goodin, i].mean()
        aa[i] = self.v[goodin, i].mean()

    speed = np.sqrt(bb**2 + aa**2)
    dirt = np.arctan2(bb, aa) * 180 / np.pi
    dirt[dirt < 360] += 360
    dirt[dirt > 360] -= 360
    binrange = -np.mean(self.variables.depth) + self.binrange[1, :]
    aa = speed * np.cos(dirt * np.pi / 180)
    bb = speed * np.sin(dirt * np.pi / 180)

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.quiver(0, 0, binrange, aa, bb, np.zeros(len(bb)), pivot='tail', arrow_length_ratio=0.3, length=0.15, color='tomato', lw='2')
    ax.text(0, -0.025, -15, 'W', color='k', fontsize=15)
    ax.text(-0.025, 0, -15, 'S', color='k', fontsize=15)
    ax.text(0.025, 0, -15, 'N', color='k', fontsize=15)
    ax.text(0, 0.025, -15, 'E', color='k', fontsize=15)
    ax.set_zlim(-15, 0)
    ax.set_xlim(-0.25, 0.25)
    ax.set_ylim(-0.25, 0.25)
    plt.gca().invert_xaxis()
    #plt.gca().invert_yaxis()
    ax.view_init(elev=18, azim=30)
    ax.dist = 8
    ax.set_xlabel('m/s')
    ax.set_ylabel('m/s')
    ax.set_zlabel('Depth (m)')
    ax.set_title('Mean Current Vector')
    ax.plot([0, 0], [0, 0], zs=[-15, 0], lw=2, color='grey')
    for i in range(0, self.n):
        ax.plot([-0.1*0.25, 0.1*0.25], [0, 0], zs=[binrange[i], binrange[i]], lw=2, color='grey')
        ax.plot([0, 0], [-0.1*0.25, 0.1*0.25], zs=[binrange[i], binrange[i]], lw=2, color='grey')
    plt.show()

存在箭头,因为您可以看到线的颜色有所不同,但它们看起来不像箭头,有人知道我如何让箭头正确显示吗?

非常感谢,

【问题讨论】:

  • 显示生成您在该函数中使用的数据的其余代码。很难从图像中复制它。此外,我在您显示的代码行中没有看到任何说明箭头是灰色的或者应该出现带有基点的文本。我的猜测是您犯了一个错误,这不一定是您共享的代码部分。

标签: python vector matplotlib 3d


【解决方案1】:

箭头(红色)在那里。我认为这是 matplotlib 库中的一个错误,但无论如何问题出在组件(U、V、W)上。尝试在轴的轴线上稍微旋转它们,您应该会看到头部出现。这是一个最小的例子:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')
X = (0, 0, 0)
Y = (0, 1, 2)
Z = (0,0,0)
ax.quiver(X,Y,Z,(0,2,1),(1,1,1),(1,3,1),length=0.05,arrow_length_ratio=0.3)
plt.show()

,结果是这样的:

注意箭头是如何遭受与您相同的问题的。其他都很好。它们之间的唯一区别是组件。

我认为您应该尝试提供数据(您正在使用我们看不到的类,因此您的情节仍然难以重现)以及代码,以防您在设置正确的组件时遇到问题.

【讨论】:

    猜你喜欢
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多