【问题标题】:stacked bar chart from grouped object来自分组对象的堆积条形图
【发布时间】:2018-10-07 02:02:46
【问题描述】:

我得到以下分组查询的预期计数。但是当我添加.plot.bar() 方法时,我会得到每条记录的条形图。 如何获得堆积条形图?

df.groupby(['department', 'status'])['c_name'].count()

department                                                status  
Agriculture                                               Accepted      3
                                                          Pending       2
                                                          Rejected     13
Department of Education and Training                  Accepted    290
                                                          Rejected     65
Higher Education                                  Accepted    424
                                                          Pending      24
                                                          Rejected     92
Medical Education and Research                    Accepted     34
                                                          Pending       3
                                                          Rejected      1

这将创建一个条形图,但不是堆叠的。

.plot(kind='bar', stacked=True)

每个部门应该有 3 种颜色(接受、待定和拒绝)


更新:

我使用 pivot 进行管理。

gdf=df.groupby(['department', 'status'])['c_name'].count().reset_index()
gdf.pivot(index='department', columns='status').plot(kind='bar', stacked=True)

但是有没有可能提高图表质量?

【问题讨论】:

  • 堆叠需要按列进行,每个堆叠一列。在这种情况下 3 列 AcceptedPendingRejected
  • 感谢您使用edit 功能为您的帖子提供更多信息。但请don't pollute your post with EDIT: or UPDATE: sections。对于未来的读者,帖子需要是独立的,没有任何历史记录。这些网站不是论坛,而是旨在成为规范、高质量、问答的图书馆。看到各种各样的历史对未来的读者没有帮助。

标签: pandas matplotlib seaborn


【解决方案1】:

你很亲密,需要unstack

df.groupby(['department','status'])['c_name'].count().unstack().plot(kind='bar', stacked=True)

【讨论】:

  • 谢谢。默认图表看起来并不专业。有没有可能有更大更好的图表?
  • @shantanuo - 对于更大尺寸的地块,可以使用.plot(kind='bar', stacked=True, figsize=(8,6))better graph 怎么想?
猜你喜欢
  • 2020-08-22
  • 2012-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多