Siderius
Siderius

Reputation: 304

Scatter plot legend with respect to colour [Python3]

I have this MWE of six points to plot:

import matplotlib.pyplot as plt

plt.scatter(1,2,marker= 'o', color='darkred')
plt.scatter(3,5,marker= 'o', color='yellowgreen')
plt.scatter(11,21,marker= 'o', color='black')
plt.scatter(4,6,marker= 'o', color='blue')
plt.scatter(8,11,marker= 'o', color='yellowgreen')
plt.scatter(2,3,marker= 'o', color='darkred')
plt.show()

I would like to write a legend based on colour e.g

blue --> first_experiment

yelowgreen --> second_experiment

and so on.

Any idea?

Upvotes: 0

Views: 49

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150725

Try adding label option:

plt.scatter(1,2,marker= 'o', color='darkred',label='first')
plt.scatter(3,5,marker= 'o', color='yellowgreen', label='second')
plt.scatter(11,21,marker= 'o', color='black')
plt.scatter(4,6,marker= 'o', color='blue')
plt.scatter(8,11,marker= 'o', color='yellowgreen')
plt.scatter(2,3,marker= 'o', color='darkred')

# add the legend
plt.legend()

# show the plot
plt.show()

Output:

enter image description here

Upvotes: 1

Related Questions