machinery
machinery

Reputation: 6290

Scatterplot using Seaborn

I have the following dataframe df:

           A           B                  C               D           E       Gender
0          3.125       3.333333           3.333333        2.500       3.6     male
1          3.875       4.444444           4.555556        2.000       4.3     male
2          3.750       2.555556           4.111111        2.750       3.1     female
3          3.125       4.111111           4.444444        2.000       3.9     female
4          4.000       4.777778           4.777778        1.250       3.6     female
5          2.875       4.333333           4.000000        3.250       3.6     male
6          3.250       3.444444           2.333333        2.875       4.1     male

Now I would like to draw the following plot using Seaborn:

ax = sns.stripplot(x=" ", y=" ", hue="Gender", data=df, jitter=True, palette="Set2", dodge=True)

Unfortunately, I don't know what I have to put into x and y because the format of my dataframe does somehow not really match. I would like to have 5 scatterplots on the x-axis (A,B,C,D and E) and on the y-axis the values the values of A,B,C,D and E should be plotted.

How can I achieve this?

Upvotes: 0

Views: 1533

Answers (2)

Alexandre B.
Alexandre B.

Reputation: 5500

You need to chose a x-axis. You can take one of your column. However, all the columns seems to be some data so we will define another one x :

df["x"] = np.linspace(1, 7, 7)
df
>>>
       A         B         C      D    E  Gender    x
0  3.125  3.333333  3.333333  2.500  3.6    male  1.0
1  3.875  4.444444  4.555556  2.000  4.3    male  2.0
2  3.750  2.555556  4.111111  2.750  3.1  female  3.0
3  3.125  4.111111  4.444444  2.000  3.9  female  4.0
4  4.000  4.777778  4.777778  1.250  3.6    male  5.0
5  2.875  4.333333  4.000000  3.250  3.6    male  6.0
6  3.250  3.444444  2.333333  2.875  4.1    male  7.0

Now you can display one scatterplot per column with a loop :

# style
plt.style.use('seaborn-darkgrid')

# create a color palette
palette = plt.get_cmap('Set1')

# Title
plt.title("A - B - C - D - E - Gender `plot`")
# multiple line plot
for i, column in enumerate(df.drop('x', axis=1)):
    plt.plot(df['x'], df[column], marker='', color=palette(i), linewidth=1, alpha=0.9, label=column)

plt.xlabel("x")

plt.show()

enter image description here

You can do the same and display one plot per column :

# style
plt.style.use('seaborn-darkgrid')

# create a color palette
palette = plt.get_cmap('Set1')

# Title
plt.title("A - B - C - D - E `Spaghetti plot`", loc='left',
          fontsize=12, fontweight=0, color='orange')
# For each column
for i, column in enumerate(df.drop('x', axis=1)):
    plt.subplot(6,1,i+1)
    plt.plot(df['x'], df[column], marker='', color=palette(i), linewidth=1, alpha=0.9, label=column)

# Add legend
plt.legend(loc=2, ncol=2)

plt.xlabel("x")
plt.ylabel("y")

plt.show()

enter image description here

Upvotes: 0

Quang Hoang
Quang Hoang

Reputation: 150805

Try:

new_df = df.set_index('Gender').stack().reset_index()

sns.stripplot(x=new_df.level_1, y=new_df[0], hue=new_df.Gender)
plt.show()

Output:

enter image description here

Upvotes: 1

Related Questions