0

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.

1
  • Can you be more specific about which part you're struggling with? Have you looked at the matplotlib documentation? Commented May 7, 2020 at 23:05

1 Answer 1

1

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:

enter image description here

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.