数据框
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
我想绘制
y1
、y2
、y3
、y4
与 Sample
散点图,色调为 Type
。
Seaborn 有什么办法可以做到吗?
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()
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()
如果您想要 2x2 网格:
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()
如果您想要 1x4 网格:
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()