Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Pythons matplotlib中添加标签列表_Python_Matplotlib_Plot - Fatal编程技术网

在Pythons matplotlib中添加标签列表

在Pythons matplotlib中添加标签列表,python,matplotlib,plot,Python,Matplotlib,Plot,我在matplotlib中有一个三维绘图,输入数据由3个x、y、z坐标列表和一个标签列表组成,标签列表指示每个坐标集也属于哪个类。我从标签中创建一个颜色列表,然后为每个坐标指定一种颜色: x_cords = projected_train[:,0] y_cords = projected_train[:,1] z_cords = projected_train[:,2] fig = plt.figure() ax = fig.add_subplot(111, projection='3d')

我在matplotlib中有一个三维绘图,输入数据由3个x、y、z坐标列表和一个标签列表组成,标签列表指示每个坐标集也属于哪个类。我从标签中创建一个颜色列表,然后为每个坐标指定一种颜色:

x_cords = projected_train[:,0]
y_cords = projected_train[:,1]
z_cords = projected_train[:,2]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
colors = ['#008080',...,'#000000']
plotlabels = ['Acer campestre L',...,'Viburnum tinus']

#Creating colorlist from labels indexes
colors = np.asarray(colors)
colorslist = colors[labels]

ax.scatter(x_cords, y_cords, z_cords, color=colorslist)

plt.show()

标签列表的创建方式与颜色列表相同:

labels = np.asarray(labels)
plotlabelslist = plotlabels[labels]
但当我将标签添加到绘图时:

ax.scatter(x_cords, y_cords, z_cords, color=colorslist, label=plotlabelslist)

plt.legend(loc='upper left')
我得到以下结果:

我尝试过其他添加标签的方法,但没有任何运气,是否有任何方法可以在添加颜色的同时添加标签列表,或者我必须一个一个地绘制每个类并添加标签,如下面的答案所示:

我们将非常感谢任何朝着正确方向的帮助或推动

这对你有用吗?


建议分离散点:我知道这是一个解决方案,但正如问题中所述,我正在寻找一种方法来避免使用颜色条,就像这样,如果我能将它与我的颜色列表相匹配,这将是一个解决方案
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

x_cords = [1,2,4,2,5,3,2,5,3,4,6,2,3,4,5,3,4,2,4,5]
y_cords = [6,5,3,4,5,6,3,5,4,6,3,4,5,6,3,4,5,6,3,4]
z_cords = [3,1,3,4,2,4,5,6,3,4,5,6,2,4,5,7,3,4,5,6]
classlbl= [0,2,0,1,2,0,2,0,1,2,0,1,0,2,0,2,0,1,0,2]

colors  = ['r','g','b']
Labels  = ['RED','GREEN','BLUE']

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
#plotlabels = ['Acer campestre L',...,'Viburnum tinus']

#Creating colorlist from labels indexes
colors = np.asarray(colors)
colorslist = colors[classlbl]
Labels = np.asarray(Labels)
labellist = Labels[classlbl]

# Plot point by point
for x,y,z,c,l in zip(x_cords, y_cords, z_cords,colorslist,labellist):
    ax.scatter(x, y, z, color=c,label=l)

# Get the labels and handles
handles, labels = ax.get_legend_handles_labels()

# Filter the labels and handles to remove duplicates
newLeg=dict()
for h,l in zip(handles,labels):
    if l not in newLeg.keys():
        newLeg[l]=h

# Create new handles and labels
handles=[]
labels=[]
for l in newLeg.keys():
    handles.append(newLeg[l])
    labels.append(l)

# Create new Legend
ax.legend(handles, labels)    

plt.show()