簡體   English   中英

Plotly+Python:如何在 3D 中繪制箭頭?

[英]Plotly+Python: How to plot arrows in 3D?

我正在使用 Plotly+Python。 如何在 3D 中繪制由箭頭表示的單個向量?

注釋(這本來是一個 hacky 解決方法)是僅 2D 的,而 Quiver 圖也是僅 2D 的。

正如 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)

這將產生:

在此處輸入圖片說明

如果打開交互式html文件會好很多

我認為您可以在 3D 中使用線和錐體的組合。 假設您的線從 (x, y, z) 開始並在 (p, q, r) 結束,然后錐體接受 XYZ 和 UV W。現在您可以設置 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/

從 Abdul Saboor 的回答中,這是(在 plotly.js 中)我制作合理的彩色箭頭的方式:

 // example arrow endpoints const x = [0.5, 1]; const y = [1, 2]; const z = [0, 0.5]; const data = [ { x: x, y: y, z: z, mode: "lines", type: "scatter3d", hoverinfo: "none", line: { color: "blue", width: 3 } }, { type: "cone", x: [x[1]], y: [y[1]], z: [z[1]], u: [0.3*(x[1]-x[0])], v: [0.3*(y[1]-y[0])], w: [0.3*(z[1]-z[0])], anchor: "tip", // make cone tip be at endpoint hoverinfo: "none", colorscale: [[0, "blue"], [1, "blue"]], // color all cones blue showscale: false, } ]; Plotly.newPlot('myDiv',data);
 <head> <!-- Load plotly.js into the DOM --> <script src='https://cdn.plot.ly/plotly-2.3.1.min.js'></script> </head> <body> <div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div> </body>

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

https://plotly.com/javascript/reference/cone/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM