本文介绍了在 Pythons matplotlib 中添加标签列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 matplotlib 中有一个 3 维图,输入数据由 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, 投影='3d')颜色 = ['#008080',...,'#000000']plotlabels = ['Acer campestre L',...,'Viburnum tinus']#根据标签索引创建颜色列表颜色 = np.asarray(颜色)颜色列表 = 颜色[标签]ax.scatter(x_cords, y_cords, z_cords, color=colorslist)plt.show()

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

labels = np.asarray(labels)plotlabelslist = plotlabels[标签]

但是当我将标签添加到图中时:

ax.scatter(x_cords, y_cords, z_cords, color=colorslist, label=plotlabelslist)plt.legend(loc='左上')

我得到以下结果:

我尝试了其他添加标签的方法,但没有任何运气,是否有任何方法可以在添加颜色时添加标签列表,或者我是否必须逐一绘制每个类并添加标签,就像在答案中一样:

将 numpy 导入为 np从 mpl_toolkits.mplot3d 导入 Axes3D导入 matplotlib.pyplot 作为 pltx_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]颜色 = ['r','g','b']标签 = ['红色','绿色','蓝色']fig = plt.figure()ax = fig.add_subplot(111, 投影='3d')#plotlabels = ['Acer campestre L',...,'Viburnum tinus']#根据标签索引创建颜色列表颜色 = np.asarray(颜色)颜色列表 = 颜色[classlbl]标签 = np.asarray(标签)标签列表 = 标签[classlbl]# 逐点绘制对于 zip(x_cords, y_cords, z_cords,colorslist,labellist) 中的 x,y,z,c,l:ax.scatter(x, y, z, 颜色=c,label=l)# 获取标签和句柄句柄,标签 = ax.get_legend_handles_labels()# 过滤标签和句柄以删除重复项newLeg=dict()对于 zip 中的 h,l(手柄、标签):如果我不在 newLeg.keys() 中:新腿[l]=h# 创建新的句柄和标签句柄=[]标签=[]对于 newLeg.keys() 中的 l:handles.append(newLeg[l])标签.附加(l)# 创建新图例ax.legend(手柄,标签)plt.show()

I have a 3 dimensional plot in matplotlib, the input data consists of 3 lists of x,y,z coordinates and a list of labels that indicates which class each coordinate set belongs too. From the labels I create a colour list that then assigns a colour to each of coordinates.:

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

the labels list is created in the same fashion as the colour list:

labels = np.asarray(labels)
plotlabelslist = plotlabels[labels]

But when I add the labels to the plot:

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

plt.legend(loc='upper left')

I get the following result:

I have tried other ways of adding the labels but without any luck, are there any ways of adding a list of labels just as the colours are added, or do I have to plot every class one by one and add the labels, like in the answer from: How to get different colored lines for different plots in a single figure?

any help or nudge in the right direction would be much appreciated!

解决方案

Would this work for you?

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

这篇关于在 Pythons matplotlib 中添加标签列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:15