Search code examples
pythonlegendscatter-plotpca

Adding legend to scatterplot when color is encoded


enter image description here

I can't add legend to my scatterplot. The problem that color is encoded by variable y which takes two values 0 or 1. X comes from PCA method, I try to plot 2 principal components with different colors corresponding to different y. I get error mesage "No handles with labels found to put in legend."

Tried different tutorial, but still cofused.

fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(111)

plt.scatter(x_reduced[:,0], x_reduced[:,1],c=y, alpha=0.5)

plt.legend()
plt.show()

Solution

  • If you're using a newer version of Matplotlib (>=3.1), then you can add legends to a scatterplot following this answer: Scatterplot legends

    Otherwise, a workaround is to do two separate calls to plt.scatter

    # one scatter for y == 0
    plt.scatter(x_reduced[y==0,0], x_reduced[y==0,1], alpha=0.5, label = "group1")
    # another scatter for y == 1
    plt.scatter(x_reduced[y==1,0], x_reduced[y==1,1], alpha=0.5, label = "group2")
    
    # create legend for both
    plt.legend()