从Pandas数据框创建PyGal图表

3
我希望尝试使用pygal,因为它能够创建SVG数据,我将从合并的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的角度来看,这似乎是错误的。我应该采用不同的方法吗?

如果数据框有多列,我该如何做到这一点?

1个回答

5
您正在错误地使用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)))

调用。


我在这里(http://pbpython.com/visualization-tools-1.html) 给出的示例中遇到了困难,只有按照您上面描述的方式添加数据,我才能绘制我的 pandas.Series。谢谢。 - Pyderman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接