显示来自按组结果分组的共享条形图

huangapple go评论55阅读模式
英文:

Displaying a shared bar chart from groupby results

问题

我有这种类型的数据框架:

        origin  delta
    month                
    2021-09  admin  -1000
    2021-09    ext     20
    2021-10    ext    648
    2021-11  admin  -1000
    2021-11    ext    590
monthframe (32,3)

(我重新处理了"month"索引,最初是解析为日期的)

我尝试重现以下两个链接的内容:

https://stackoverflow.com/questions/41494942/pandas-dataframe-groupby-plot

https://stackoverflow.com/questions/42988302/pandas-groupby-results-on-the-same-plot

使用以下代码:

monthframe.groupby("origin").plot(kind="bar", stacked=True, legend=True, xlabel="Month", ylabel="Delta", layout=(20,10), subplots=False)

在之前将"month"设置为索引,以便它可以作为x轴。

但我无法让它在同一图中显示带有不同颜色的条形图。当我执行以下代码时,我只得到一个图:

monthframe.plot(kind="bar", stacked=True, legend=True, xlabel="Month", ylabel="Delta", layout=(20,10), subplots=False)

但然后所有来源都合并到相同的月份中,都是蓝色,这真的不够信息。

我尝试了我能找到的一切(在绘图之前设置轴等等),但图形甚至不考虑它的命名参数。

请问该怎么做?

英文:

I have this type of Dataframe :

    origin  delta
month                
2021-09  admin  -1000
2021-09    ext     20
2021-10    ext    648
2021-11  admin  -1000
2021-11    ext    590
monthframe (32,3)

(I reprocessed "month" index from dates in a colum, parsed as datetime initially)
I tried to reproduce
https://stackoverflow.com/questions/41494942/pandas-dataframe-groupby-plot
https://stackoverflow.com/questions/42988302/pandas-groupby-results-on-the-same-plot

with

monthframe.groupby("origin").plot(kind="bar",stacked=True, legend=True, xlabel="Month", ylabel="Delta", layout=(20,10),subplots=False)

setting month as index before so that it will work as x.

But I can't get it to display bars in the same graph, with various colors.
I only have one graph when I do

monthframe.plot(kind="bar",stacked=True, legend=True, xlabel="Month", ylabel="Delta", layout=(20,10),subplots=False)

but then all origins are merged into the same months, all is blue and it really isn't informative.

I tried everything I could find (setting axes before; etc, but the plot doesn't even take its named arguments into account).

What to do please?

答案1

得分: 1

使用matplotlib,您需要重新整理您的数据帧:

ax = (monthframe.set_index('origin', append=True)['delta'].unstack()
                .plot(kind='bar', stacked=True, legend=True, rot=45, 
                      xlabel='Mois', ylabel='Consommation'))
plt.tight_layout()
plt.show()

重新整理的输出:

>>> monthframe.set_index('origin', append=True)['delta'].unstack()

origin    admin    ext
month                 
2021-09 -1000.0   20.0
2021-10     NaN  648.0
2021-11 -1000.0  590.0

显示来自按组结果分组的共享条形图

英文:

With matplotlib, you have to reshape your dataframe:

ax = (monthframe.set_index('origin', append=True)['delta'].unstack()
                .plot(kind='bar', stacked=True, legend=True, rot=45, 
                      xlabel='Mois', ylabel='Consommation'))
plt.tight_layout()
plt.show()

显示来自按组结果分组的共享条形图

Output of reshape:

>>> monthframe.set_index('origin', append=True)['delta'].unstack()

origin    admin    ext
month                 
2021-09 -1000.0   20.0
2021-10     NaN  648.0
2021-11 -1000.0  590.0

huangapple
  • 本文由 发表于 2023年2月16日 04:14:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定