0

I am trying to create an XY chart using Python and the Pygal library. The source data is contained in a CSV file with three columns; ID, Portfolio and Value. Unfortunately I can only plot one axis and I suspect it's an issue with the array. Can anyone point me in the right direction? Do I need to use numpy? Thank you!

import pygal
import pandas as pd
data = pd.read_csv("profit.csv")
data.columns = ["ID", "Portfolio", "Value"]
xy_chart = pygal.XY()

xy_chart.add('Portfolio', data['Portfolio','Value'] << I suspect this is wrong
)
xy_chart.render_in_browser()

With

import pygal
import pandas as pd
data = pd.read_csv("profit.csv")
data.columns = ["ID", "Portfolio", "Value"]
xy_chart = pygal.XY()

xy_chart.add('Portfolio', data['Portfolio']
)
xy_chart.render_in_browser()

I get:

A graph with a series of horizontal data points/values; i.e. it has the X values but no Y values.

With:

import pygal
import pandas as pd
data = pd.read_csv("profit.csv")
data.columns = ["ID", "Portfolio", "Value"]
xy_chart = pygal.XY()

xy_chart.add('Portfolio', data['Portfolio','Value']
)
xy_chart.render_in_browser()

I get:

KeyError: ('Portfolio', 'Value')

Sample data:

ID  Portfolio   Value
1   1   -2560.042036
2   2   1208.106958
3   3   5702.386949
4   4   -8827.63913
5   5   -3881.665733
6   6   5951.602484
Edmund
  • 65
  • 2
  • 10
  • Please elaborate. What do you exactly mean by "*I can only plot one axis*"? What happens with the other? Does it not appear? Does it overwrite the previous one? Does it cause an error? If so, what is the error? – Andras Deak -- Слава Україні Nov 19 '15 at 22:08
  • Thanks, I have tried to add more detail. I tried to upload an image too but it returned an error. Imagine a graph with an X and Y axis but all data points are along the X axis at Y=0. – Edmund Nov 19 '15 at 23:24
  • I'm unfamiliar with `pygal`, but did you [look at the docs](http://www.pygal.org/en/latest/documentation/types/xy.html)? It would seem you can add a general 2d scatter plot by adding a list of 2-element tuples to `pygal.XY.add`. I.e. try `zip(data['Portfolio'],data['Value'])` inside `add()`. – Andras Deak -- Слава Україні Nov 19 '15 at 23:28
  • Ah that's interesting, with xy_chart.add('Portfolio', zip(data['Portfolio'],data['Value'])) I get 'no data' in the chart but the axes and scales look correct! – Edmund Nov 19 '15 at 23:43
  • Are you sure neither `data['Portfolio']` nor `data['Value']` are empty? – Andras Deak -- Слава Україні Nov 19 '15 at 23:44
  • I've added the first few rows of data to the questions. Thanks again for your help. – Edmund Nov 19 '15 at 23:47
  • It *might* be possible that the columns of `data` are actually columns (`Nx1` matrices), but for `zip` to work you need a true 1d array. Does `pandas` support a `numpy`-like transpose? As in `xy_chart.add('Portfolio', zip(data['Portfolio'].T,data['Value'].T))`? It might also be possible to use `.flatten()` instead of `.T`, if that is supported. – Andras Deak -- Слава Україні Nov 19 '15 at 23:55

1 Answers1

1

Maybe a little late here, but I just did something similar. Your second example requires multiple columns to be handed in as a array and then the DataFrame you get back needs to be converted into a list of tuples.

import pygal
import pandas as pd
data = pd.read_csv("profit.csv")
data.columns = ["ID", "Portfolio", "Value"]

points = data[['Portfolio','Value']].to_records(index=False).tolist()

xy_chart = pygal.XY()
xy_chart.add('Portfolio', points)
xy_chart.render_in_browser()

There may be a more elegant use of the pandas or pygal API to get the columns into a list of tuples.

freebie
  • 2,161
  • 2
  • 19
  • 36