python - 在 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'
请帮我解决这个问题。
解决方案
尝试如下重新安装模块。损坏的安装可能会导致此错误。
pip install --upgrade --force-reinstall matplotlib
推荐阅读
- redirect - 在 React App Router 中重定向页面后重新加载 URL
- python-3.x - 如何在不和谐机器人上使用斜杠命令?
- bash - 如何在 bash 中测试变量是否为关联数组?
- python - 如何将for循环更改为while循环
- python - 将 pandas DataFrame() 插入 MySQL 表会引发 ProgrammingError
- java - 无法使用 Java Spring 完成发布
- spring - 如何在spring data jpa派生查询中使用带有嵌入字段的order by子句?
- docker - 无法访问 kubernetes nodeport 服务
- reactjs - 如何制作更小的单选按钮
- blockchain - 你如何访问区块链的原始公共数据,然后从中读取交易账本?