【问题标题】:How to show the angle (by an arc) between two 3D vectors in matplotlib?如何在 matplotlib 中显示两个 3D 向量之间的角度(通过弧线)?
【发布时间】:2018-04-29 12:43:07
【问题描述】:

假设这两个向量由公共点 (1,1,1) 的两个顶点 (1,2,3) 和 (0,2,-1) 定义。如何显示这两个向量或线所承受的锐角(在 3D 图中由虚弧表示)?在我的代码中,我已经在使用

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

我是初学者,请帮助。

【问题讨论】:

  • 没有可以自动执行此操作的命令。您可能必须生成一个局部坐标系,使两个向量位于 x-y 平面内。然后,您需要生成位于局部 x-y 平面内的圆弧上的点。最后,您必须将这些点的坐标转换为全局坐标。如果你能写出完成所有这些事情的方程式,ax.plot(x,y,z) 可以为你制作弧线。
  • 谢谢。我虽然想到了,但做起来太乏味了。相反,我可以用python中的一些绘图工具手工绘制弧线吗?我需要矢量格式(pdf)的图形。有可能吗?
  • 您可以使用InkScape。它与 Python 无关,但您可以将其用于后期处理。它可以将图形导出为PDF、EPS、SVG等。它是Adobe Illustrator的开源替代品
  • 好的,我试试看。我希望矢量格式保持不变。
  • 因为这个问题似乎以“我怎样才能画出一些东西?”而告终。我猜这里是题外话。

标签: matplotlib 3d angle


【解决方案1】:

我遇到了类似的问题,拼凑了以下解决方案:

import numpy as np
import math
from math import sin, cos
from mpl_toolkits.mplot3d import proj3d

def plot_arc3d(vector1, vector2, radius=0.2, fig=None, colour='C0'):
    """ Plot arc between two given vectors in 3D space. """

    """ Confirm correct input arguments """
    assert len(vector1) == 3
    assert len(vector2) == 3

    """ Calculate vector between two vector end points, and the resulting spherical angles for various points along 
        this vector. From this, derive points that lie along the arc between vector1 and vector2 """
    v = [i-j for i, j in zip(vector1, vector2)]
    v_points_direct = [(vector2[0]+v[0]*l, vector2[1]+v[1]*l, vector2[2]+v[2]*l) for l in np.linspace(0, 1)]
    v_phis = [math.atan2(v_point[1], v_point[0]) for v_point in v_points_direct]
    v_thetas = [math.acos(v_point[2]/np.linalg.norm(v_point)) for v_point in v_points_direct]

    v_points_arc = [(radius*sin(theta)*cos(phi), radius*sin(theta)*sin(phi), radius*cos(theta))
                    for theta, phi in zip(v_thetas, v_phis)]
    v_points_arc.append((0, 0, 0))

    if fig is None:
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
    else:
        ax = fig.gca()

    from mpl_toolkits.mplot3d.art3d import Poly3DCollection
    """ Plot polygon (face colour must be set afterwards, otherwise it over-rides the transparency)
        https://stackoverflow.com/questions/18897786/transparency-for-poly3dcollection-plot-in-matplotlib """
    points_collection = Poly3DCollection([v_points_arc], alpha=0.4)
    points_collection.set_facecolor(colour)
    ax.add_collection3d(points_collection)

    return fig

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多