Reputation: 13
I only just stepped into Python and I'm trying to generate a scatter plot with several categories. I am using the Iris Dataset as a basis, here is what I have so far:
import matplotlib.pyplot as plt
import numpy as np
fig,ax = plt.subplots(1)
iris_data = np.genfromtxt(
"iris.csv", names=True,
dtype="float", delimiter=",")
x=iris_data["sepal_length"]
y=iris_data["sepal_width"]
g=iris_data["class"]
plt.scatter(x,y)
plt.show()
I don't know how to separate out the classes and plot each one on the same graph.
I am coming from Matlab where all I needed was the "gscatter(x,y,g) creates a scatter plot of x and y, grouped by g" to get the job done, but I am finding out that python needs a little more to get the group by g part done.
Thank you in advance for any help.
Upvotes: 0
Views: 2778
Reputation: 169274
Use seaborn:
import seaborn as sns, matplotlib.pyplot as plt
iris = sns.load_dataset('iris')
sns.scatterplot(x='sepal_length',y='sepal_width',data=iris,hue='species')
plt.show()
Result:
You can do the same in matplotlib as in the example below as long as your data is organized in a pandas DataFrame:
for k,g in iris.groupby('species'):
plt.scatter(g['sepal_length'],g['sepal_width'],label=k)
Upvotes: 1