Skip to main content
 首页 » 编程设计

python之使用 python3 时无法绘制系列类型

2025年05月04日17softidea

我正在使用 Python3,我在处理它时遇到了问题。 我编码如下,我找到了我预期的正确结果。然后我想以图表的形式查看结果。所以我尝试绘制它,但出现意外错误,表明我需要 matplotlib。由于我在第一个代码中导入了 matplotlib,所以我不知道如何解决这个问题。

import matplotlib.pyplot as plt 
%matplotlib inline 
train.Survived[train['Name']=='Mr'].value_counts() 
 
#Result 
0    436 
1     81 
Name: Survived, dtype: int64 

我输入了下面的代码,将它们视为饼状图。

train.Survived[train['Name']=='Mr'].value_counts().plot(kind='pie') 

错误信息如下。

--------------------------------------------------------------------------- 
ImportError                               Traceback (most recent call last) 
<ipython-input-191-7f60a15206ad> in <module>() 
      1 import pandas.plotting 
----> 2 train.Survived[train['Name']=='Mr'].value_counts().plot(kind='pie') 
 
/usr/local/lib/python3.6/site-packages/pandas/plotting/_core.py in __call__(self, kind, ax, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, label, secondary_y, **kwds) 
   2739                            colormap=colormap, table=table, yerr=yerr, 
   2740                            xerr=xerr, label=label, secondary_y=secondary_y, 
-> 2741                            **kwds) 
   2742     __call__.__doc__ = plot_series.__doc__ 
   2743  
 
/usr/local/lib/python3.6/site-packages/pandas/plotting/_core.py in plot_series(data, kind, ax, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, label, secondary_y, **kwds) 
   2000                  yerr=yerr, xerr=xerr, 
   2001                  label=label, secondary_y=secondary_y, 
-> 2002                  **kwds) 
   2003  
   2004  
 
/usr/local/lib/python3.6/site-packages/pandas/plotting/_core.py in _plot(data, x, y, subplots, ax, kind, **kwds) 
   1757                 data = data[y].copy() 
   1758                 data.index.name = y 
-> 1759         plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds) 
   1760     else: 
   1761         if isinstance(data, ABCDataFrame): 
 
/usr/local/lib/python3.6/site-packages/pandas/plotting/_core.py in __init__(self, data, kind, **kwargs) 
   1498         if (data < 0).any().any(): 
   1499             raise ValueError("{0} doesn't allow negative values".format(kind)) 
-> 1500         MPLPlot.__init__(self, data, kind=kind, **kwargs) 
   1501  
   1502     def _args_adjust(self): 
 
/usr/local/lib/python3.6/site-packages/pandas/plotting/_core.py in __init__(self, data, kind, by, subplots, sharex, sharey, use_index, figsize, grid, legend, rot, ax, fig, title, xlim, ylim, xticks, yticks, sort_columns, fontsize, secondary_y, colormap, table, layout, **kwds) 
    105                  table=False, layout=None, **kwds): 
    106  
--> 107         _raise_if_no_mpl() 
    108         _converter._WARN = False 
    109         self.data = data 
 
/usr/local/lib/python3.6/site-packages/pandas/plotting/_core.py in _raise_if_no_mpl() 
     55     # TODO(mpl_converter): remove once converter is explicit 
     56     if not _HAS_MPL: 
---> 57         raise ImportError("matplotlib is required for plotting.") 
     58  
     59  
 
ImportError: matplotlib is required for plotting. 

如果您能解决我的问题,我将不胜感激。 谢谢。

请您参考如下方法:

如果您正在使用 Jupyter(我曾经使用过)并且您已经正确安装和导入了 matplotlib,请尝试在您的笔记本中重新启动内核:

菜单>内核>重启

然后重新运行所有需要的代码。这解决了我的问题。我的想法来自 here .它看起来像具有相同解决方案的其他问题。

希望对您有所帮助。