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

2024-04-25 19:56:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我用的是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

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

^{pr2}$

错误信息如下。在

---------------------------------------------------------------------------
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.

如果你能解决我的问题,我真的很感激。 谢谢。在


Tags: inpycorepandasdatamatplotliblibpackages
1条回答
网友
1楼 · 发布于 2024-04-25 19:56:35

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

“菜单”>;“内核”>;“重新启动”

然后重新运行所有需要的代码。这解决了我的问题。我是从here那里得到这个主意的。这看起来像是用同样的方法解决的另一个问题。在

希望有帮助。在

相关问题 更多 >