Search code examples
pythonpython-ggplot

Python ggplot not giving correct y-axis values?


Having some issues with my ggplot figure, in that I appear to have nice data but the plotting is not taking the values.

Here is the data:

In[127]: ts_top10_stk

Out[127]: 
                 type    value
unit                          
R084  entriesn_hourly  1868674
R084    exitsn_hourly  1467338
R022  entriesn_hourly  1773372
R022    exitsn_hourly  1483494
R012  entriesn_hourly  1618262
R012    exitsn_hourly  1084521
R046  entriesn_hourly  1555117
R046    exitsn_hourly   968557
R055  entriesn_hourly  1554806
R055    exitsn_hourly  1174953
R033  entriesn_hourly  1534652
R033    exitsn_hourly   843390
R018  entriesn_hourly  1444569
R018    exitsn_hourly  1200120
R011  entriesn_hourly  1355492
R011    exitsn_hourly   484352
R029  entriesn_hourly  1347727
R029    exitsn_hourly   771924
R179  entriesn_hourly  1270579
R179    exitsn_hourly   415908

Here is what I am doing with it for a plot:

plot_top10 = ggplot(aes(x = 'unit',y='value',fill='type'),data=ts_top10_stk) + geom_bar()

Getting this exception:

Exception: Could not evaluate the 'x' mapping: 'unit' (original error: name 'unit' is not defined)

Clearly I am trying to create a stacked bar chart with the units on the x-axis and the bar split between the entries and the exits. I feel like i am just missing the concept of ggplot - because I cant get much to work at all.

Here is some other information:

In[202]: ts_top10_stk.columns

Out[202]: Index([u'type', u'value'], dtype='object')
In[203]: ts_top10_stk.index
Out[203]: 
Index([u'R084', u'R084', u'R022', u'R022', u'R012', u'R012', u'R046', u'R046',
       u'R055', u'R055', u'R033', u'R033', u'R018', u'R018', u'R011', u'R011',
       u'R029', u'R029', u'R179', u'R179'],
      dtype='object', name=u'unit')

UPDATE: Created a new column for the unit value:

ts_top10_stk['unit2'] = ts_top10_stk.index
plot_top10 = ggplot(aes(x = 'unit2',y='value',fill='type'),data=ts_top10_stk) + geom_bar()

Here is what I am getting though - still not considering the actual values... Looks like its just taking a count and plotting that (1 for each, 2 total types): enter image description here


Solution

  • I haven't used ggplot in Python but that message makes me think that it just doesn't know where to find 'unit' because it's not a column - it's the pandas index (this is a pandas object right?) Try making a distinct 'unit' column and do this again?