Plotly + Python:如何在3D中绘制箭头? [英] Plotly+Python: How to plot arrows in 3D?
问题描述
我正在使用Plotly + Python.如何在3D模式下绘制单个矢量,如箭头所示?
I am using Plotly+Python. How can I plot a single vector, as represented by an arrow, in 3D?
注释(这本来是个棘手的解决方法)仅适用于2D,箭袋图也仅适用于2D.
Annotations (which would have been a hacky workaround) are 2D-only, and Quiver plots are also 2D-only.
推荐答案
正如Mike Wise所说,不可能直接进行,但是,您可以计算向量,然后通过将线画到原点来绘制向量:
As Mike Wise mentioned, it is not possible to do it straight forwardly, nevertheless, you can compute your vector and then plot it by drawing the line to the origin:
例如: 在3D中绘制一些点,并绘制与这些点的质心相对应的向量
For example: Plot some points in 3D and draw a vector corresponding to the centroid of those points
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)
这将产生:
如果您打开交互式html文件,那就更好了
It will be much better if you open the interactive html file
这篇关于Plotly + Python:如何在3D中绘制箭头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!