Python 使用pandas.DataFrame.melt()使用seaborn绘制数据

Python 使用pandas.DataFrame.melt()使用seaborn绘制数据,python,pandas,dataframe,Python,Pandas,Dataframe,因此,我在一个数据框中有两列数据: Actual Predicted 0 2.8 2.854729 1 2.2 3.081473 2 4.2 3.211041 3 2.9 2.951905 4 3.4 3.211041 5 3.3 2.838533 6 3.5 3.194845 7 3.1 3.000493 8 2.8 3.016689 9 2.8 3.032885 我想学习用三个公共库绘制数据。它们似乎都是基于彼此的,但实现方式却大不相同。到目前为止

因此,我在一个数据框中有两列数据:

Actual  Predicted
0   2.8 2.854729
1   2.2 3.081473
2   4.2 3.211041
3   2.9 2.951905
4   3.4 3.211041
5   3.3 2.838533
6   3.5 3.194845
7   3.1 3.000493
8   2.8 3.016689
9   2.8 3.032885
我想学习用三个公共库绘制数据。它们似乎都是基于彼此的,但实现方式却大不相同。到目前为止,我已经使用Pandas和Matplotlib实现了相同的绘图。熊猫的方式很简单,matplotlib非常复杂(只是一种观点)

现在我正试图用Seaborn的文档来计算,但我很难计算出我的x和y值应该是多少。在我看来:

  • x-df.index,因为我想安排所有的数据点 沿x轴

  • y-我希望“y”值有两个:实际值和预测值。不 我想知道如何做到这一点

  • hue-df.columns,因为我希望有一个表示实际的条,一个表示预测的条

但是,这根本不起作用,以下任何尝试也不起作用:

sns.barplot(x=df.index, y=df.columns, data=df) 

sns.barplot(data=df)
我的直觉是,我需要使用DataFrame.melt()函数将数据转换为长格式,以便更好地绘图,但我尝试了以下方法:

df2 = df.melt(var_name='Type', value_name='Measurement')
df2


Type    Measurement
0   Actual  2.800000
1   Actual  2.200000
2   Actual  4.200000
3   Actual  2.900000
4   Actual  3.400000
... ... ...
85  Predicted   2.903317
86  Predicted   3.211041
87  Predicted   2.870925
88  Predicted   3.146257
89  Predicted   3.211041
但这会让我错失获得相邻正确条的机会,因为它将所有“实际”值集中在一起,与所有“预测”值分开

不管怎样,我还是尝试了一下,希望它能以某种方式正确地将酒吧组合在一起。我使用了seaborn.barplot文档页面下面示例中的模板,得到了
ValueError:无法解释输入的“测量值”

sns.barplot(x=df.index, y='Measurement', hue='Type')
所以我的想法是,这一切归结为正确使用melt函数,但在这一点上我完全不知所措。我的眼睛现在在流血,因为文件太混乱了。有人告诉我,seaborn是策划事情最简单的方法,而事实证明这绝对是最难的

我认为我的数据应该是这样的:

    Type        Measurement
0   Actual      2.800000
    Predicted   2.903317
1   Actual      2.200000
    Predicted   3.211041
... ... ...
然后我可以按照seaborn.barplot()的示例代码进行操作:


或者至少我希望如此…

诀窍是在融化之前重置索引。。。这将是您的
x
参数:

df_melted = df.reset_index().melt(id_vars='index')

#     index   variable     value
# 0       0     Actual  2.800000
# 1       1     Actual  2.200000
# 2       2     Actual  4.200000
# 3       3     Actual  2.900000
# 4       4     Actual  3.400000
# 5       5     Actual  3.300000
# 6       6     Actual  3.500000
# 7       7     Actual  3.100000
# 8       8     Actual  2.800000
# 9       9     Actual  2.800000
# 10      0  Predicted  2.854729
# 11      1  Predicted  3.081473
# 12      2  Predicted  3.211041
# 13      3  Predicted  2.951905
# 14      4  Predicted  3.211041
# 15      5  Predicted  2.838533
# 16      6  Predicted  3.194845
# 17      7  Predicted  3.000493
# 18      8  Predicted  3.016689
# 19      9  Predicted  3.032885

sns.barplot(data=df_melted, x='index', y='value', hue='variable')
[外]


出于某种原因,它不允许我添加绘图的图像。。。最近还有其他人有这个问题吗?获取
正文不能包含“i.stack.imgur.com”
错误(没有空格…我甚至不允许在评论中发布链接!?)嗯,很抱歉听到这个消息。然而,你的回答起了作用,所以我很感激。一个稍微相关的问题,熊猫在幕后融化是为了制作我使用
df.plot(kind='bar')
得到的情节吗。很好,我不必以这种方式重塑数据。是的,默认行为是使用DataFrame索引作为
x
值,并使用单个列作为单个图表系列。
sns.barplot(x=df.index, y="Measurement", hue="type", data=df)
df_melted = df.reset_index().melt(id_vars='index')

#     index   variable     value
# 0       0     Actual  2.800000
# 1       1     Actual  2.200000
# 2       2     Actual  4.200000
# 3       3     Actual  2.900000
# 4       4     Actual  3.400000
# 5       5     Actual  3.300000
# 6       6     Actual  3.500000
# 7       7     Actual  3.100000
# 8       8     Actual  2.800000
# 9       9     Actual  2.800000
# 10      0  Predicted  2.854729
# 11      1  Predicted  3.081473
# 12      2  Predicted  3.211041
# 13      3  Predicted  2.951905
# 14      4  Predicted  3.211041
# 15      5  Predicted  2.838533
# 16      6  Predicted  3.194845
# 17      7  Predicted  3.000493
# 18      8  Predicted  3.016689
# 19      9  Predicted  3.032885

sns.barplot(data=df_melted, x='index', y='value', hue='variable')