【问题标题】:Creating pygal.Bar chart from Pandas series value counts?从 Pandas 系列值计数创建 pygal.Bar 图?
【发布时间】:2018-01-31 10:19:52
【问题描述】:

我正在尝试从应用 .value_counts() 的 pandas DataFrame 系列构建一个 pygal 条形图,例如无需按照documentationthis question 中的建议将系列拆分为多个列并单独添加这些列。

import pandas as pd
import pygal

df = pd.DataFrame.from_dict({'categorical': ['foo','foo','bar','foo','bar','too']})

series = df.categorical.value_counts()
series
> foo    3
  bar    2
  too    1

理想情况下,结果看起来像this question 解决方案中的图。这个可以吗?

提前致谢!

【问题讨论】:

  • 您链接的问题在解决方案中没有图片

标签: python pandas pygal


【解决方案1】:

我认为这就是你的目标:

Pygal 不能直接绘制 Pandas 对象,但您可以利用 value_counts 返回的 Series 对象的属性来设置图表的 x 标签并将计数添加为一系列数据。

上面的图表是使用以下代码创建的:

import pandas as pd
import pygal

df = pd.DataFrame.from_dict({'categorical': ['foo','foo','bar','foo','bar','too']})
series = df.categorical.value_counts()

chart = pygal.Bar(width=400, height=300)
chart.x_labels = series.index
chart.add("Count", series.values)

chart.render_to_png("bar.png")  # Or any other output option.

【讨论】:

    猜你喜欢
    • 2018-09-28
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 2015-10-12
    • 1970-01-01
    相关资源
    最近更新 更多