0

Dataframe

df
Sample Type   y1   y2   y3  y4
S1     H     1000  135  220  171
S2     H     2900  1560 890  194
S3     P     678   350  127  255
S4     P     179   510  154  275

I want to plot y1, y2, y3, y4 vs Sample scatterplot with hue as Type.

Is there any way to do it in Seaborn?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Snehal
  • 1
  • 1
  • 3

1 Answers1

4

Since, you want just one plot you can use sns.scatterplot:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

#df = pd.read_csv('yourfile.csv')

#plotting
df1 = df.melt(['Type','Sample'])
sns.scatterplot(data=df1, x="Sample", y="value",hue="Sample",style="Type")
plt.show()

enter image description here

In case you want multiple scatter plots, you can use sns.relplot:

#some preprocessing
df1 = df.melt(['Type','Sample'])
#plotting
sns.relplot(data=df1, x="Sample", y="value", hue="Type", col="variable", height=2, aspect=1.5)
plt.show()

enter image description here

In case, you want 2x2 grid :

df1 = df.melt(['Type','Sample'])
#plotting
sns.relplot(data=df1, x="Sample", y="value", hue="Type", col="variable",col_wrap=2, height=2,   aspect=1.5)
plt.show()

enter image description here

In case, you want 1x4 grid :

df1 = df.melt(['Type','Sample'])
#plotting
sns.relplot(data=df1, x="Sample", y="value", hue="Type", col="variable",col_wrap=1, height=2,   aspect=1.5)
plt.show()
Grayrigel
  • 3,474
  • 5
  • 14
  • 32