0

我的 ggplot 图有一些问题,因为我似乎有很好的数据,但绘图没有采用这些值。

这是数据:

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

这是我为情节所做的事情:

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

得到这个例外:

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

显然,我正在尝试创建一个堆叠条形图,其中 x 轴上的单位和条形图在入口和出口之间分开。我觉得我只是错过了 ggplot 的概念——因为我根本无法工作。

以下是一些其他信息:

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

更新:为单位值创建了一个新列:

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

这是我得到的 - 仍然没有考虑实际值......看起来它只是计算并绘制它(每个1个,总共2个类型): 在此处输入图像描述

4

1 回答 1

0

我没有在 Python 中使用 ggplot,但该消息让我认为它只是不知道在哪里可以找到“单位”,因为它不是列 - 它是熊猫索引(这是熊猫对象,对吗?)尝试制作一个独特的'unit' 列并再次执行此操作?

于 2015-09-25T13:22:41.010 回答