首页 > 解决方案 > 在 Jupyter Notebook 中导入 matplotlib 时找不到子包

问题描述

我正在尝试在 Jupyter Notebook 中导入 matplotlib.pyplot(版本 3.2.1),但一直出现错误。
值得一提的是,我的笔记本电脑上有 ArcGIS,其中有 Python 2.7 和版本 1 的 Matplotlib,而不是 3。下面列出的步骤在 PyCharm 中运行良好。

import matplotlib.pyplot as plt

ModuleNotFoundError: No module named 'matplotlib.pyplot

我尝试从命令行(使用 Windows)运行 pip uninstall matplotlib 来卸载整个东西,然后再安装回来,但它似乎也没有任何效果。

在我运行一些包含其功能的代码之前,只需导入 matplotlib 即可。然后出现错误,即

import matplotlib

此单元格运行没有错误。然后我尝试展示一些基本的东西:

x = [0, 1, 2, 3, 4]
y = [2, 2, 2, 2, 2]
df = pd.DataFrame([x,y], columns=['a','b','c','d','f'])
df['b'].hist()
matplolib.pyplot.show() 

并获得多个回溯输出

~\Anaconda3\lib\site-packages\pandas\plotting\_core.py in _get_plot_backend(backend)
   1599         # Because matplotlib is an optional dependency and first-party backend,
   1600         # we need to attempt an import here to raise an ImportError if needed.
-> 1601         import pandas.plotting._matplotlib as module
   1602 
   1603         _backends["matplotlib"] = module

ModuleNotFoundError: No module named 'matplotlib.artist'

我也试过这个:

%matplotlib
import matplotlib.pyplot as plt

这导致了另一个错误:

~\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in find_gui_and_backend(gui, gui_select)
    286         # overwritten that.
    287         # WARNING: this assumes matplotlib 1.1 or newer!!
--> 288         backend = matplotlib.rcParamsOrig['backend']
    289         # In this case, we need to find what the appropriate gui selection call
    290         # should be for IPython, so we can activate inputhook accordingly

AttributeError: module 'matplotlib' has no attribute 'rcParamsOrig'

请帮我解决这个问题。

标签: pythonmatplotlibimportjupyter-notebook

解决方案


尝试如下重新安装模块。损坏的安装可能会导致此错误。

pip install --upgrade --force-reinstall matplotlib 

推荐阅读