使用python3时无法绘制序列类型

时间:2018-07-03 10:43:02

标签: python pandas matplotlib

我正在使用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.

如果您能解决我的问题,我非常感谢。 谢谢。

1 个答案:

答案 0 :(得分:7)

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

菜单>内核>重新启动

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

希望有帮助。