首页 > 解决方案 > 无法在 Python 3.7 Notebook 中绘制简单直方图

问题描述

下面是我在尝试从 DF“代码”和列(“年龄”)做直方图时收到的错误代码

code['Age'].plt.hist()
1
code['Age'].plt.hist()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-44-2117f9d17105> in <module>
----> 1 code['Age'].plt.hist()

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   5177             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5178                 return self[name]
-> 5179             return object.__getattribute__(self, name)
   5180 
   5181     def __setattr__(self, name, value):

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

标签: pythonpython-3.7histogram2d

解决方案


直接从 matplotlib 使用 hist 函数:

import matplotlib.pyplot as plt

plt.hist(code['Age'])
plt.show()

这应该有效。你也可以这样做:

import matplotlib.pyplot as plt

code['Age'].hist()
plt.show()

推荐阅读