Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python matplotlib scatter - different markers in one scatter

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.

like image 420
Mar Mosh Avatar asked Apr 20 '17 19:04

Mar Mosh


People also ask

How do you plot multiple scatters in Python?

Multiple scatter plots can be graphed on the same plot using different x and y-axis data calling the function Matplotlib. pyplot. scatter() multiple times.

How do I overlay two Scatterplots in matplotlib?

You simply call the scatter function twice, matplotlib will superimpose the two plots for you. You might want to specify a color, as the default for all scatter plots is blue. This is perhaps why you were only seeing one plot. Save this answer.


2 Answers

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.

like image 174
ImportanceOfBeingErnest Avatar answered Sep 22 '22 07:09

ImportanceOfBeingErnest


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='+')
like image 25
jakevdp Avatar answered Sep 22 '22 07:09

jakevdp