简体   繁体   English

使用Matplotlib在3D图中标记点

[英]Label points in 3d plot using matplotlib

I am trying to draw a simple 3D polyhedron and trying to label the vertices with the coordinates. 我试图绘制一个简单的3D多面体,并尝试用坐标标注顶点。 First step I want do is simply label each vertices with their order or 1 , 2 , ... 第一步,我希望做的仅仅是标签与它们的顺序或各顶点12 ,...

I saw in this answer where I can use loop to do so. 我在答案中看到可以使用循环执行此操作的地方。 But I was wondering if there was possibility to pass on the list of x,y,z coordinates and a list of labels and it will simply plot the points and label it, possibly without any loop. 但是我想知道是否有可能传递x,y,z坐标列表和标签列表,并且它将简单地绘制点并标记它,可能没有任何循环。 If there exist such functions that I am not aware of. 如果存在我不知道的功能。
This is what i have till now 这就是我到目前为止

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()

在此处输入图片说明

I tried with ax.text(xcod,ycod,zcod,["1","2","3","4","5","6"]) which did not work. 我尝试了ax.text(xcod,ycod,zcod,["1","2","3","4","5","6"]) I could follow the loop but is there another simple way to do it? 我可以遵循循环,但是还有另一种简单的方法吗?

ax.text() only places text in one position. ax.text()仅将文本放在一个位置。

Try: 尝试:

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

then you get: 然后您得到:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM