我正在使用 Plotly+Python。如何在 3D 中绘制由箭头表示的单个向量?
注释(这本来是一个 hacky 解决方法)仅是 2D 的,而 Quiver 图也是仅 2D 的。
小智 5
我认为你可以在 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);Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
https://plotly.com/javascript/cone-plot/
https://plotly.com/javascript/reference/cone/
正如迈克·怀斯(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)
Run Code Online (Sandbox Code Playgroud)
这将产生:
如果你打开交互式html文件会好很多
| 归档时间: |
|
| 查看次数: |
4914 次 |
| 最近记录: |