How to infer and display legend?
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('iris.csv')
for i, s in enumerate(species := df['species'].drop_duplicates().values):
df.loc[df['species'] == s, 'c'] = i
df.plot(kind='scatter', x='sepal_length', y='sepal_width', c='c', cmap='viridis')
plt.show()
Which results in:
I tried calling plt.legend(species)
expecting the 3 species names to show, but only 1 of the 3 is displayed, why? and is there a way to infer it somehow in the same fashion as c='c'
?
CodePudding user response:
df.plot(kind='scatter', x='sepal_length', y='sepal_width', c='c', cmap='viridis', legend = True)
Does adding legend=True work?
CodePudding user response:
you can use seaborn library and make your life easy:
import seaborn as sns
sns.FacetGrid(iris, hue ="class",
height = 6).map(plt.scatter,
'sepallength',
'sepalwidth').add_legend()
but if you insist on doing it in your way, I think the easiest way is to separate each category in a for loop as follows:
iris_df=pd.read_csv('iris.csv')
for i in range(0, 3):
species_df = iris_df[iris_df['class'] == i]
plt.scatter(
species_df['sepal_length'],
species_df[''],
color=colours[i],
alpha=0.5,
label=species[i]
)
plt.xlabel('sepal length (cm)')
plt.ylabel('sepal width(cm)')
plt.title('Iris dataset: petal length vs sepal length')
plt.legend(loc='lower right')
plt.show()