12

I want to display some points. Here is my code:

plt.scatter(y[:,0],y[:,1],c=col)
plt.show()

And as col I have:

Col:  [1 1 0 1 1 1 1 0 0 0 1 0 0 0 0 0 0 1 1 1 0 0 1 1 0 0 0 0 1 1 0 1 1 0 0 0 0
 0 0 0 0 0 0 0 1 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 1 0 0 1 1 1 1 1 1 1 1 1 0 0
 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1 1 0 1 0 0]

So I have points in two different colours. But I also want to have two different markers. How can I do it? markers=col gives an error.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Mar Mosh
  • 215
  • 2
  • 4
  • 9

3 Answers3

23

You can use one scatter plot per marker.

markers = ["s","o"]
for i, c in enumerate(np.unique(col)):
    plt.scatter(y[:,0][col==c],y[:,1][col==c],c=col[col==c], marker=markers[i])

For a way to use several markers in a single scatter plot, see this answer.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
21

Matplotlib does not support different markers in one call to scatter. You'll have to use two different calls to scatter; for example:

plt.scatter(y[col == 0, 0], y[col == 0, 1], marker='o')
plt.scatter(y[col == 1, 0], y[col == 1, 1], marker='+')
jakevdp
  • 77,104
  • 11
  • 125
  • 160
0

in my case, i have the features on one variable, x_train and the labels on another variable y_train

So I break the problem into two parts, select the data, which is label=0 and draw it.

then the data with label=1 and draw it.

zerodataf1=X_train[:,0][y_train==0]
zerodataf2=X_train[:,1][y_train==0]
plt.scatter(zerodataf1,zerodataf2,c='blue',marker='o',label='Not Admitted')
plt.scatter(X_train[:,0][y_train==1],X_train[:,1][y_train==1],c='red',marker='+',label='Admitted')
plt.xlabel('Exam 1')
plt.ylabel('Exam 2')
plt.title('Scatter Plot ...')
plt.legend()
Mohamed Fathallah
  • 1,274
  • 1
  • 15
  • 17