问题
I am using Plotly+Python. How can I plot a single vector, as represented by an arrow, in 3D?
Annotations (which would have been a hacky workaround) are 2D-only, and Quiver plots are also 2D-only.
回答1:
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:
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)
This will produce:
It will be much better if you open the interactive html file
来源:https://stackoverflow.com/questions/43164909/plotlypython-how-to-plot-arrows-in-3d