Create PyGal graph from pandas dataframe

僤鯓⒐⒋嵵緔 提交于 2020-01-02 07:55:10

问题


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.

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

I know that i can do:

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)

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?


回答1:


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

Hope this helps.

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

call.



来源:https://stackoverflow.com/questions/35261588/create-pygal-graph-from-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!