简体   繁体   中英

Bar plot unique elements in a list

I am trying to bar plot the number of each elements in the list:

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'

So do i need to change the series type to dataframe ? I am bit stuck on this.

Try calling the plot method of the series directly.

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

Alternatively, with plt.bar :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM