3

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?

cryptoref
  • 413
  • 1
  • 7
  • 17

1 Answers1

5

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.

freethrow
  • 1,068
  • 3
  • 20
  • 34
  • I struggled with the example given here http://pbpython.com/visualization-tools-1.html, and only by adding the data as you have described above was I able to plot my `pandas.Series`. Thanks. – Pyderman Mar 31 '16 at 01:20