在python中绘制多个Y轴+'hue'散点图

问题描述

数据框

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与以色相为类型的样本散点图。

在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()

enter image description here

如果需要多个散点图,可以使用sns.relplot

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

enter image description here

如果要使用2x2网格:

df1 = df.melt(['Type',col_wrap=2,aspect=1.5)
plt.show()

enter image description here

如果要使用1x4网格:

df1 = df.melt(['Type',col_wrap=1,aspect=1.5)
plt.show()