从 pandas 数据框创建PyGal图 [英] Create PyGal graph from pandas dataframe

查看:69
本文介绍了从 pandas 数据框创建PyGal图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试使用pygal来创建SVG数据,因为我将通过合并的HTML和SVG图形创建打印的PDF页面。

I wanted to try using pygal with it's ability to create SVG data as i'm going to create a printed PDF page from the merged HTML with the SVG graph.

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

What i want to do is the equivalent of pygal.plot(dataframe) but i'm not seeing that in the docs.

我知道我可以做到:

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的角度看,这似乎是错误的。我应该做不同的事情吗?

But that seems wrong from a python point of view. Should i be doing it differently?

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

Plus how would i do this if the dataframe had multiple columns?

推荐答案

您正在使用 pygal API 错误的方式。标签应与索引相对应。另外,在使用熊猫时,应避免使用迭代项。

You are using the pygal API the wrong way. The labels should correspond to the index. Also, you should avoid the iteritems when using Pandas.

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')

希望这会有所帮助。

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

edit: for a dataframe you can just use all of the series and add them one by one with the

line_chart.add('Series name', pd.Series(np.random.randn(5)))

通话。

这篇关于从 pandas 数据框创建PyGal图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆