使用Seaborn绘制Pandas DataFrame的多列 [英] Plot multiple columns of Pandas DataFrame using Seaborn

查看:432
本文介绍了使用Seaborn绘制Pandas DataFrame的多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的DataFrame的列为['X_Axis','col_2','col_3',...,'col_n',]

suppose I have DataFrame with columns ['X_Axis','col_2','col_3',...,'col_n',]

我需要在X轴上绘制第一列,并在Y轴上绘制. 仅供参考:所有值均已根据X轴分组,X轴值的范围为0-25,所有其他列值均已归一化为0 - 1.我希望它在同一图形图上,而不是子图上.

I need to plot the first column on X-Axis and rest on Y-Axis. FYI : all the values have been grouped according to X-Axis, the X-Axis values range from 0-25 and all other column values have been normalized to the scale of 0 - 1. I want it on same graph plot, not subplots.

首选:FactorPlot,法线图.

Preferred : FactorPlot , normal line graph.

推荐答案

您需要 melt 用于使用:

df = df.melt('X_Axis', var_name='cols', value_name='vals')
#alternative for pandas < 0.20.0
#df = pd.melt(df, 'X_Axis', var_name='cols',  value_name='vals')
g = sns.factorplot(x="X_Axis", y="vals", hue='cols', data=df)

示例:

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]})

print (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

df = df.melt('X_Axis', var_name='cols',  value_name='vals')
g = sns.factorplot(x="X_Axis", y="vals", hue='cols', data=df)

在seaborn的新版本中获得警告:

In new versions of seaborn get warning:

factorplot功能已重命名为catplot.原始名称将在将来的版本中删除.请更新您的代码.请注意,factorplot('point')中的默认kind已更改catplot中的'strip'.

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.

因此,请使用 seaborn.catplot ,如果需要相同的行为,请使用:

So use seaborn.catplot, if need same behaviour use kind='point':

df = df.melt('X_Axis', var_name='cols',  value_name='vals')
g = sns.catplot(x="X_Axis", y="vals", hue='cols', data=df, kind='point')

这篇关于使用Seaborn绘制Pandas DataFrame的多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆