条形图列表中的唯一元素

时间:2017-10-18 00:57:15

标签: python python-3.x pandas matplotlib dataframe

我试图绘制列表中每个元素的数量:

x=[1,1,2]
list_pd = pd.DataFrame({'DrawnCards':x})
list_pd.head()
list_pd = list_pd.groupby('DrawnCards')['DrawnCards'].count() 

DrawnCards
1    2
2    1
Name: DrawnCards, dtype: int64

plt.bar(list_pd.DrawnCards)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-117-7f4d4efd9a31> in <module>()
----> 1 plt.bar(list_pd.DrawnCards)

C:\Users\mesme\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   2968             if name in self._info_axis:
   2969                 return self[name]
-> 2970             return object.__getattribute__(self, name)
   2971 
   2972     def __setattr__(self, name, value):

AttributeError: 'Series' object has no attribute 'DrawnCards'

那么我需要将系列类型更改为数据帧吗?我有点困惑于此。

1 个答案:

答案 0 :(得分:1)

尝试直接调用该系列的plot方法。

g = list_pd.groupby('DrawnCards')['DrawnCards'].count()
g.plot(kind='bar')

或者,使用plt.bar

plt.bar(g.index, g.values)