从 pandas 数据框创建 PyGal 图

Create PyGal graph from pandas dataframe

我想尝试使用具有创建 SVG 数据能力的 pygal,因为我要从合并的 HTML 和 SVG 图形创建打印的 PDF 页面。

我想做的是 pygal.plot(dataframe) 的等价物,但我没有在文档中看到它。

我知道我能做到:

df = pd.Series(np.random.randn(5), index = ['a', 'b', 'c', 'd', 'e'])
chart = pygal.Line()
for index, row in df.iteritems():
    chart.add(index,row)

但从 python 的角度来看,这似乎是错误的。我应该采取不同的做法吗?

另外,如果数据框有多个列,我该怎么做?

您使用 pygal API 的方式有误。标签应与索引相对应。此外,使用 Pandas.

时应避免使用 iteritems
line_chart = pg.Line()
line_chart.x_labels = ['a', 'b', 'c', 'd', 'e']
line_chart.add('Firefox', pd.Series(np.random.randn(5)))
line_chart.render_to_file('bchart.svg')

希望对您有所帮助。

编辑:对于数据框,您可以使用所有系列并使用

将它们一一添加
line_chart.add('Series name', pd.Series(np.random.randn(5)))

呼唤。