Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create PyGal graph from pandas dataframe

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?

like image 460
cryptoref Avatar asked Jun 30 '26 22:06

cryptoref


1 Answers

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.

like image 132
freethrow Avatar answered Jul 03 '26 11:07

freethrow