So I was trying to change the legend from a scatterplot using seaborn and matplotlib. I'm using the iris dataset to illustrate my problem.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
url = "https://gist.githubusercontent.com/curran/a08a1080b88344b0c8a7/raw/0e7a9b0a5d22642a06d3d5b9bcbad9890c8ee534/iris.csv"
iris = pd.read_csv(url)
I wanted to move the legend to the outside of the plot area and to change the title and labels to their capitalized from. Running this code
sns.scatterplot(x="sepal_width", y="petal_width", data=iris, hue="species")
plt.legend(
title="Species",
labels=['Setosa', 'Versicolor', 'Virginica'],
bbox_to_anchor = (1,1),
loc=2)
I get this plot:
I noticed that the "Setosa" dot was smaller than the others and was the same color as "Versicolor". So I tried adding a fourth label:
sns.scatterplot(x="sepal_width", y="petal_width", data=iris, hue="species")
plt.legend(
title="Species",
labels=['Setosa', 'Versicolor', 'Virginica', 'test'],
bbox_to_anchor = (1,1),
loc=2)
Now I believe it is plotting an additional label at the beginning and I don't now why. Anyone has an idea on how to avoid this "extra" label?