Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 条形图在列表中打印唯一的元素_Python_Python 3.x_Pandas_Matplotlib_Dataframe - Fatal编程技术网

Python 条形图在列表中打印唯一的元素

Python 条形图在列表中打印唯一的元素,python,python-3.x,pandas,matplotlib,dataframe,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) -------------

我试图用条形图表示列表中每个元素的数量:

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'
x=[1,1,2]
list_pd=pd.DataFrame({'DrawnCards':x})
名单头()
list_pd=list_pd.groupby('DrawnCards')['DrawnCards'].count()
拖鞋
1    2
2    1
名称:DrawnCards,数据类型:int64
plt.bar(列表和局部图纸)
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
---->1 plt.bar(列表和局部图纸)
C:\Users\mesme\Anaconda3\lib\site packages\pandas\core\generic.py in\uuuuu getattr\uuuu(self,name)
2968如果自身信息轴中的名称:
2969返回自我[姓名]
->2970返回对象。\uuuu getattribute\uuuu(self,name)
2971
2972定义设置属性(自身、名称、值):
AttributeError:“Series”对象没有属性“DrawnCards”

那么我是否需要将series类型更改为dataframe?我有点受不了了。

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

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

或者,使用
plt.bar

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

但限制是使用matplotlib绘制图形。@Krishnendu对你有好处
plot
内部调用
matplotlib.pyplot.bar
。是否有任何方法可以显式调用matplotlib来执行此操作?