所以我正在尝试使用seaborn创建一个barplot。我的数据格式为
Packet number,Flavour,Contents
1,orange,4
2,orange,3
3,orange,2
4,orange,4
...
36, orange,3
1, coffee,5
2, coffee,3
...
1, raisin,4
etc.
我的代码当前为:
revels_data = pd.read_csv("testtt.txt") rd = revels_data
ax = sns.barplot(x="Packet number", y="Contents", data=rd) plt.show()
我正在尝试为每个数据包编号(在x轴上)创建条形,这些颜色按每个条形图内部的颜色除以味道,并在y轴上每个数据包的总含量。
开始尝试计算每个数据包的总数
total_1 = (rd.loc[rd["Packet number"] == 1, "Contents"].sum())
但不确定我将如何去那里,或者是否有更简单的方法来做到这一点。
任何建议深表感谢!
最佳答案
您想要使用hue
。同样,当前您正在显示每个类别的平均值。要计算其他函数,可以使用estimator
。
因此,您的代码应为:
ax = sns.barplot(x="Packet number", y="Contents", hue="Flavour", data=rd)
或者,如果要显示总和而不是均值:
ax = sns.barplot(x="Packet number", y="Contents", hue="Flavour", estimator=np.sum, data=rd)
编辑:
如果您对堆积的barplot感兴趣,则可以直接使用pandas制作它,但是您需要首先对数据进行分组:
# Sum (or mean if you'd rather) the Contents per packet number and flavor
# unstack() will turn the flavor into columns, and fillna will put 0 in
# all missing columns
grouped = rd.groupby(["Packet number", "Flavour"])["Contents"].sum().unstack().fillna(0)
# The x axis is taken from the index. The y axis from the columns
grouped.plot(kind="bar", stacked=True)