我希望采用以下三个[x,y,z]坐标:
[0.799319 -3.477045e-01 0.490093]
[0.852512 9.113778e-16 -0.522708]
[0.296422 9.376042e-01 0.181748]
将它们绘制为矢量,其中矢量从原点[0,0,0]开始。我该怎么做呢?我一直在尝试使用matplotlib的箭袋,但我仍然保持以下值错误:
strcmp
这是我的代码(document_matrix_projections是以上表示为矩阵的三个坐标):
ValueError: need at least one array to concatenate
答案 0 :(得分:6)
quiver()
函数需要箭头的位置为X,Y,Z
和U,V,W
作为箭头的组成部分。因此,以下脚本可以绘制您的数据:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
p0 = [0.799319, -3.477045e-01, 0.490093]
p1 = [0.852512, 9.113778e-16, -0.522708]
p2 = [0.296422, 9.376042e-01, 0.181748]
origin = [0,0,0]
X, Y, Z = zip(origin,origin,origin)
U, V, W = zip(p0,p1,p2)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.quiver(X,Y,Z,U,V,W,arrow_length_ratio=0.01)
plt.show()
但结果不是很好。如果您想使用Mayavi,则可以使用以下方法:
import numpy as np
import mayavi.mlab as m
p0 = [0.799319, -3.477045e-01, 0.490093]
p1 = [0.852512, 9.113778e-16, -0.522708]
p2 = [0.296422, 9.376042e-01, 0.181748]
origin = [0,0,0]
X, Y, Z = zip(origin,origin,origin)
U, V, W = zip(p0,p1,p2)
m.quiver3d(X,Y,Z,U,V,W)
答案 1 :(得分:0)
使用matplolib的quiver()的一个很好的替代方法是使用plotly进行绘图,它具有交互性的优势。以下函数可以使用Scatter3d()绘制向量。在这里,向量有一个大的点来标记方向,而不是箭头。
import numpy as np
import plotly.graph_objs as go
def vector_plot(tvects,is_vect=True,orig=[0,0,0]):
"""Plot vectors using plotly"""
if is_vect:
if not hasattr(orig[0],"__iter__"):
coords = [[orig,np.sum([orig,v],axis=0)] for v in tvects]
else:
coords = [[o,np.sum([o,v],axis=0)] for o,v in zip(orig,tvects)]
else:
coords = tvects
data = []
for i,c in enumerate(coords):
X1, Y1, Z1 = zip(c[0])
X2, Y2, Z2 = zip(c[1])
vector = go.Scatter3d(x = [X1[0],X2[0]],
y = [Y1[0],Y2[0]],
z = [Z1[0],Z2[0]],
marker = dict(size = [0,5],
color = ['blue'],
line=dict(width=5,
color='DarkSlateGrey')),
name = 'Vector'+str(i+1))
data.append(vector)
layout = go.Layout(
margin = dict(l = 4,
r = 4,
b = 4,
t = 4)
)
fig = go.Figure(data=data,layout=layout)
fig.show()
绘图可以简单地通过
完成p0 = [0.799319, -3.477045e-01, 0.490093]
p1 = [0.852512, 9.113778e-16, -0.522708]
p2 = [0.296422, 9.376042e-01, 0.181748]
vector_plot([p0,p1,p2])
上面的输出看起来像: