使用 Seaborn 绘制 pandas DataFrame 的多列
Plot multiple columns of pandas DataFrame using Seaborn
假设我有包含列 ['X_Axis','col_2','col_3',...,'col_n',]
的 DataFrame
我需要在 X 轴上绘制第一列,在 Y 轴上绘制其余部分。
仅供参考:所有值均已根据 X 轴分组,X 轴值的范围为 0-25
,所有其他列值已标准化为 0 - 1
的比例。我希望它在同一个图上,而不是子图上。
首选:FactorPlot,法线图。
- 一些 seaborn 绘图将接受宽数据帧,
sns.pointplot(data=df, x='X_Axis', y='col_2')
,但不接受 sns.pointplot(data=df, x='X_Axis', y=['col_2', 'col_3'])
,因此最好重塑数据帧。
- 使用
pandas.DataFrame.melt
将 DataFrame 从宽变长。
- 将数据帧从宽格式转换为长格式是 all seaborn plots 的标准,而不仅仅是所示示例。
- 测试于
python 3.8.12
、pandas 1.3.4
、matplotlib 3.4.3
、seaborn 0.11.2
示例数据帧
import pandas as pd
import seaborn as sns
df = pd.DataFrame({'X_Axis':[1,3,5,7,10,20],
'col_2':[.4,.5,.4,.5,.5,.4],
'col_3':[.7,.8,.9,.4,.2,.3],
'col_4':[.1,.3,.5,.7,.1,.0],
'col_5':[.5,.3,.6,.9,.2,.4]})
# display(df)
X_Axis col_2 col_3 col_4 col_5
0 1 0.4 0.7 0.1 0.5
1 3 0.5 0.8 0.3 0.3
2 5 0.4 0.9 0.5 0.6
3 7 0.5 0.4 0.7 0.9
4 10 0.5 0.2 0.1 0.2
5 20 0.4 0.3 0.0 0.4
# convert to long (tidy) form
dfm = df.melt('X_Axis', var_name='cols', value_name='vals')
# display(dfm.head())
X_Axis cols vals
0 1 col_2 0.4
1 3 col_2 0.5
2 5 col_2 0.4
3 7 col_2 0.5
4 10 col_2 0.5
当前绘图方法
catplot
:图级
将 seaborn.catplot
与 kind=
结合使用(例如 kind='point'
以重现 FactorPlot
默认值):
g = sns.catplot(x="X_Axis", y="vals", hue='cols', data=dfm, kind='point')
pointplot
: 轴级
sns.pointplot(x="X_Axis", y="vals", hue='cols', data=dfm)
原创
factorplot
:已重命名为 catplot
v0.9.0(2018 年 7 月)
新版本的 seaborn 收到警告:
The factorplot
function has been renamed to catplot
. The original name will be removed in a future release. Please update your code. Note that the default kind
in factorplot
('point'
) has changed 'strip'
in catplot
.
g = sns.factorplot(x="X_Axis", y="vals", hue='cols', data=dfm)
# using pd.melt instead of pd.DataFrame.melt for pandas < 0.20.0
# dfm = pd.melt(df, 'X_Axis', var_name='cols', value_name='vals')
# g = sns.factorplot(x="X_Axis", y="vals", hue='cols', data=dfm)
除了来自 google 的强大的@jezrael 之外,如果您打算使用原始数据帧的索引绘制线条,请执行以下操作:
df = pd.DataFrame({'col_2':[.4,.5,.4,.5,.5,.4],
'col_3':[.7,.8,.9,.4,.2,.3],
'col_4':[.1,.3,.5,.7,.1,.0],
'col_5':[.5,.3,.6,.9,.2,.4]})
# resetting index before melting to save the current index in 'index' column...
df = df.reset_index().melt('index', var_name='cols', value_name='vals')
g = sns.catplot(x="index", y="vals", hue='cols', data=df, kind='point')
假设我有包含列 ['X_Axis','col_2','col_3',...,'col_n',]
我需要在 X 轴上绘制第一列,在 Y 轴上绘制其余部分。
仅供参考:所有值均已根据 X 轴分组,X 轴值的范围为 0-25
,所有其他列值已标准化为 0 - 1
的比例。我希望它在同一个图上,而不是子图上。
首选:FactorPlot,法线图。
- 一些 seaborn 绘图将接受宽数据帧,
sns.pointplot(data=df, x='X_Axis', y='col_2')
,但不接受sns.pointplot(data=df, x='X_Axis', y=['col_2', 'col_3'])
,因此最好重塑数据帧。 - 使用
pandas.DataFrame.melt
将 DataFrame 从宽变长。- 将数据帧从宽格式转换为长格式是 all seaborn plots 的标准,而不仅仅是所示示例。
- 测试于
python 3.8.12
、pandas 1.3.4
、matplotlib 3.4.3
、seaborn 0.11.2
示例数据帧
import pandas as pd
import seaborn as sns
df = pd.DataFrame({'X_Axis':[1,3,5,7,10,20],
'col_2':[.4,.5,.4,.5,.5,.4],
'col_3':[.7,.8,.9,.4,.2,.3],
'col_4':[.1,.3,.5,.7,.1,.0],
'col_5':[.5,.3,.6,.9,.2,.4]})
# display(df)
X_Axis col_2 col_3 col_4 col_5
0 1 0.4 0.7 0.1 0.5
1 3 0.5 0.8 0.3 0.3
2 5 0.4 0.9 0.5 0.6
3 7 0.5 0.4 0.7 0.9
4 10 0.5 0.2 0.1 0.2
5 20 0.4 0.3 0.0 0.4
# convert to long (tidy) form
dfm = df.melt('X_Axis', var_name='cols', value_name='vals')
# display(dfm.head())
X_Axis cols vals
0 1 col_2 0.4
1 3 col_2 0.5
2 5 col_2 0.4
3 7 col_2 0.5
4 10 col_2 0.5
当前绘图方法
catplot
:图级
将 seaborn.catplot
与 kind=
结合使用(例如 kind='point'
以重现 FactorPlot
默认值):
g = sns.catplot(x="X_Axis", y="vals", hue='cols', data=dfm, kind='point')
pointplot
: 轴级
sns.pointplot(x="X_Axis", y="vals", hue='cols', data=dfm)
原创
factorplot
:已重命名为 catplot
v0.9.0(2018 年 7 月)
新版本的 seaborn 收到警告:
The
factorplot
function has been renamed tocatplot
. The original name will be removed in a future release. Please update your code. Note that the defaultkind
infactorplot
('point'
) has changed'strip'
incatplot
.
g = sns.factorplot(x="X_Axis", y="vals", hue='cols', data=dfm)
# using pd.melt instead of pd.DataFrame.melt for pandas < 0.20.0
# dfm = pd.melt(df, 'X_Axis', var_name='cols', value_name='vals')
# g = sns.factorplot(x="X_Axis", y="vals", hue='cols', data=dfm)
除了来自 google 的强大的@jezrael 之外,如果您打算使用原始数据帧的索引绘制线条,请执行以下操作:
df = pd.DataFrame({'col_2':[.4,.5,.4,.5,.5,.4],
'col_3':[.7,.8,.9,.4,.2,.3],
'col_4':[.1,.3,.5,.7,.1,.0],
'col_5':[.5,.3,.6,.9,.2,.4]})
# resetting index before melting to save the current index in 'index' column...
df = df.reset_index().melt('index', var_name='cols', value_name='vals')
g = sns.catplot(x="index", y="vals", hue='cols', data=df, kind='point')