Plotly + Python:如何在3D中绘制箭头?

时间:2017-04-02 05:25:12

标签: python plotly

我正在使用Plotly + Python。如何在3D中绘制单个矢量(如箭头所示)?

注释(本来是一个hacky解决方法)仅限2D,而Quiver图也只是2D。

2 个答案:

答案 0 :(得分:1)

正如Mike Wise所说,不可能直接做到这一点,然而,你可以计算你的矢量,然后通过画线到原点来绘制它:

例如:     在3D中绘制一些点并绘制与这些点的质心相对应的矢量

import plotly.graph_objs as go
from plotly.offline import plot

#prepare plotting points
#points are: (0,5,5),(5,0,0),(5,10,5),(10,5,5)
points = go.Scatter3d( x = [0,5,5,10],
                       y = [5,0,10,5],
                       z = [5,0,5,0],
                       mode = 'markers',
                       marker = dict( size = 2,
                                      color = "rgb(227,26,28)")
                     )
#Compute centroid of all 3 points by taking the mean of each of
#its coordinates (not sure this is the right definition of centroid)
centerX = (0+5+5+10) / float(4)
centerY = (5+0+10+5) / float(4)
centerZ = (5+0+5+0) / float(4)

#Prepare centroid vector
vector = go.Scatter3d( x = [0,centerX],
                       y = [0,centerY],
                       z = [0,centerZ],
                       marker = dict( size = 1,
                                      color = "rgb(84,48,5)"),
                       line = dict( color = "rgb(84,48,5)",
                                    width = 6)
                     )
data = [points,vector]
layout = go.Layout(margin = dict( l = 0,
                                  r = 0,
                                  b = 0,
                                  t = 0)
                  )
fig = go.Figure(data=data,layout=layout)
plot(fig,filename="vector.html",auto_open=False,image='png',image_height=800,image_width=1500)

这将产生:

enter image description here

如果打开交互式html文件

会好得多

答案 1 :(得分:0)

我认为您可以在3D中结合使用线和圆锥。 假设您的线从(x,y,z)开始到(p,q,r)结束,然后圆锥接受XYZ和UVW。现在您可以设置X = p,Y = q,Z = r为中间点锥的底部。要使圆锥指向与线相同的方向,但小于线的长度(例如10%),可以将U = 0.1 *(px),V = 0.1 *(qy),W = 0.1 *(rz)设置。

https://plotly.com/python/cone-plot/