Skip to content Skip to sidebar Skip to footer

Add A List Of Labels In Pythons Matplotlib

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.

Solution 1:

Would this work for you? enter image description here

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

Post a Comment for "Add A List Of Labels In Pythons Matplotlib"