[英]ModuleNotFoundError: No module named 'matplotlib.pyplot'
When making a plot, I used both Jupyter Notebook and Pycharm with the same set of code and packages.在制作绘图时,我使用了 Jupyter Notebook 和 Pycharm 以及相同的代码和包。 The code is:代码是:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt # as in Pycharm
import matplotlib as plt # as in Jupyter
df = pd.read_csv("/home/kunal/Downloads/Loan_Prediction/train.csv")
df['ApplicantIncome'].hist(bins=50)
plt.show() #this only in Pycharm not in Jupyter.
In Pycharm, the code works well.在 Pycharm 中,代码运行良好。 But in Jupyter Notebook, it has error:但是在 Jupyter Notebook 中,它有错误:
I wish someone can help me solve this problem我希望有人能帮我解决这个问题
This is an indication that matplotlib lib/module is not installed.这表明未安装matplotlib lib/module。 So all you have to do is install this module by running the code below in the cell previous to referring matplotlib:因此,您所要做的就是通过在引用 matplotlib 之前的单元格中运行以下代码来安装此模块:
!pip install matplotlib
Hope it helps!希望有帮助!
I had the same problem and found a solution!我遇到了同样的问题并找到了解决方案! Matplotlib was installed on another python installation I have. Matplotlib 安装在我的另一个 python 安装上。
Put the following snippet in a cell and execute it, and you should be good to go:将以下代码段放在一个单元格中并执行它,你应该很高兴:
import sys
!{sys.executable} -m pip install matplotlib
You don't need to use %matplotlib inline
as other answers here suggest.您不需要像这里的其他答案建议的那样使用%matplotlib inline
。 This is optional and not using it should not prevent importing pyplot.这是可选的,不使用它不应该阻止导入 pyplot。
What should work is the following:什么应该工作如下:
You may decide to use %matplotlib inline
in which case you don't have to call plt.show()
.您可以决定使用%matplotlib inline
在这种情况下您不必调用plt.show()
。
You may also use %matplotlib notebook
, which gives you an interactive plot.您也可以使用%matplotlib notebook
,它为您提供交互式绘图。
Finally, you may use %matplotlib tk
to get a windowed figure like you would in PyCharm.最后,您可以像在 PyCharm 中一样使用%matplotlib tk
来获取窗口图形。
All of those options require to have imported matplotlib.pyplot
.所有这些选项都需要导入matplotlib.pyplot
。 Importing matplotlib
alone is not helpful.单独导入matplotlib
没有帮助。 Also, if you experience any problems, start a new kernel first (don't try something new in line 27 of your notebook).此外,如果您遇到任何问题,请先启动一个新内核(不要在笔记本的第 27 行尝试新的东西)。
if you are using jupyter notebook in anaconda, matplotlib should be installed to the environment.如果您在 anaconda 中使用 jupyter notebook,则应将 matplotlib 安装到环境中。
go to Environments -> the environment you are using -> change the droplist to not installed -> search matplotlib, and install转到环境 -> 您正在使用的环境 -> 将下拉列表更改为未安装 -> 搜索 matplotlib,然后安装
if you are using Anaconda CMD the use this command,如果您使用的是 Anaconda CMD,请使用此命令,
conda install matplotlib
If you are using Normal CMD then use command,如果您使用的是普通 CMD,则使用命令,
pip install matplotlib
or或
pip3 install matplotlib
在代码顶部%matplotlib inline
添加%matplotlib inline
,它使 matplotlib 以交互方式执行
这段代码对我有用
%pip install matplotlib-inline
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.