绘制多个Y轴+“色调”散点图

问题描述 投票:0回答:1

数据框

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 有什么办法可以做到吗?

python pandas seaborn scatter-plot relplot
1个回答
4
投票

因为,您只需要一个可以使用的图

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()
© www.soinside.com 2019 - 2024. All rights reserved.