I am having a hard time trying to create a scatter plot that will show points of different classes with different colors and the corresponding label of the class in the legend.
I have 52 samples, 2 features and 3 classes(class 1,2 and 3). The data are in X
array and the labels in labels
array.
I want to plot the legend that will contain the class names (1,2 and 3) for the corresponding colors.
My code:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.rand(52,2)
labels = np.ones(x.shape[0])
labels[0:8] = 1
labels[8:31] = 2
labels[31:] = 3
plt.scatter(x[:,0], x[:,1], c = labels, label = labels)
plt.legend()
plt.show()
Result: