Python 使用 Seaborn 绘制 Pandas DataFrame 的多列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44941082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:38:41  来源:igfitidea点击:

Plot multiple columns of Pandas DataFrame using Seaborn

pythonpandasmatplotlibdataframeseaborn

提问by Rakmo

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

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

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-25and all other column values have been normalized to the scale of 0 - 1. I want it on same graph plot, not subplots.

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

Preferred : FactorPlot , normal line graph.

首选: FactorPlot ,法线图。

回答by jezrael

You need meltfor reshape with seaborn.factorplot:

您需要melt使用seaborn.factorplot进行重塑:

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)

Sample:

样品

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)

graph

图形

EDIT: In new versions of seaborn get warning:

编辑:在新版本的 seaborn 中得到警告:

The factorplotfunction has been renamed to catplot. The original name will be removed in a future release. Please update your code. Note that the default kindin factorplot('point') has changed 'strip'in catplot.

factorplot函数已重命名为catplot. 原始名称将在未来版本中删除。请更新您的代码。需要注意的是默认kindfactorplot'point')已经改变'strip'catplot

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

所以使用seaborn.catplot,如果需要相同的行为使用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')