使用matplotlib在3d图中标记点

时间:2016-02-19 12:48:35

标签: python matplotlib 3d

我正在尝试绘制一个简单的3D多面体并尝试用坐标标记顶点。我想做的第一步是简单地用每个顶点标记它们的顺序或12,...

我在this回答中看到我可以使用循环来执行此操作。但我想知道是否有可能传递x,y,z坐标列表和标签列表,它将简单地绘制点并标记它,可能没有任何循环。如果存在我不知道的功能。
这就是我现在所拥有的

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D 
#coord = 10*np.random.rand(3,num)#num points in 3D #first axis is x, second = y, third = z
xcod = np.array([1,2,3,2.7,2.4,1])
ycod = np.array([1,1,4,5.,6,1])
zcod = np.array([1,2,1,2,3,1])
#coord = np.concatenate(coord,coord[0])
#####plotting in 3d
fig = plt.figure()
ax = fig.add_subplot(111,projection = '3d')
#plotting all the points
ax.plot(xcod,ycod,zcod,'x-')
#adding labels for vertice
#ax.text(xcod,ycod,zcod,["1","2","3","4","5","6","7"])
#supposed centroid
ax.scatter(np.mean(xcod),np.mean(ycod),np.mean(zcod),marker = 'o',color='g')
ax.set_xlabel("x axis")
ax.set_ylabel("y axis")
ax.set_zlabel("z axis")

plt.show()

enter image description here

我尝试使用ax.text(xcod,ycod,zcod,["1","2","3","4","5","6"])无效。我可以按照循环,但有另一种简单的方法吗?

1 个答案:

答案 0 :(得分:3)

ax.text()只将文字放在一个位置。

尝试:

for x,y,z,i in zip(xcod,ycod,zcod,range(len(xcod))):
    ax.text(x,y,z,i)

然后你得到:

enter image description here