0

i have the following data and code, where im trying to plot the data with a scatterplot, however i dont know how to seperate the labels of the class in the actual plotting:

X = np.array([[3,4],[1,4],[2,3],[6,-1],[7,-1],[5,-3],[2,4]] )
y = np.array([-1,-1, -1, 1, 1 , 1, 1 ])

[...]
plt.axline((0,b),slope=a, color='r', linestyle='-', label="Decision Boundy")
plt.scatter(X[:,0],X[:,1],c=y)
plt.legend()
plt.show()

resulting in the plot:

enter image description here

Is it possible to have seperate labels for the colors/classes or do i have to plot them seperatly?

Typo
  • 47
  • 7
  • Does this [answer](https://stackoverflow.com/questions/26558816/matplotlib-scatter-plot-with-legend) works for what you're looking for? – matheubv Jul 30 '21 at 13:03
  • Not really, im looking for a solution looking similar to something like = `plt.scatter(X[:,0],X[:,1],c=y,labels=["c1","c2"])`. Where i can simply define the labels for the classes seperated by color – Typo Jul 30 '21 at 13:17
  • Isn't that exactly what the second answer on that post shows? I might be missing something. – matheubv Jul 30 '21 at 13:38
  • 1
    Oh youre right, i didnt read the second answer! That is indeed what i wanted, thanks – Typo Jul 30 '21 at 15:01

1 Answers1

0

Here you go:

import numpy as np
import matplotlib.pyplot as plt

X = np.array([[3,4],[1,4],[2,3],[6,-1],[7,-1],[5,-3],[2,4]] )
y = np.array([-1,-1, -1, 1, 1 , 1, 1 ])

# get indices of each label
class_a = np.where(y == 1)
class_b = np.where(y == -1)

fig, ax = plt.subplots()
b = 1.5
a = 1
ax.axline((0,b),slope=a, color='r', linestyle='-', label="Decision Boundy")
ax.scatter(X[class_a][:,0],X[class_a][:,1],c='y', label="class a")
ax.scatter(X[class_b][:,0],X[class_b][:,1],c='b', label="class b")
ax.legend()
ax.set_xlim(-10,10)
ax.set_ylim(-10,10)
plt.show()

enter image description here

sehan2
  • 1,700
  • 1
  • 10
  • 23
  • This isnt exactly what i wanted, i wanted to create seperate labels without seperating the actual data, i.e. see comments – Typo Jul 30 '21 at 16:05
  • you can do this of course https://stackoverflow.com/questions/11481644/how-do-i-assign-multiple-labels-at-once-in-matplotlib – sehan2 Jul 30 '21 at 16:07