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来执行此操作?