简体   繁体   中英

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. First step I want do is simply label each vertices with their order or 1 , 2 , ...

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. 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. I could follow the loop but is there another simple way to do it?

ax.text() only places text in one position.

Try:

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

then you get:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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