简体   繁体   中英

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. 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. But in Jupyter Notebook, it has error: 在此处输入图片说明

I wish someone can help me solve this problem

This is an indication that matplotlib lib/module is not installed. So all you have to do is install this module by running the code below in the cell previous to referring 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.

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. This is optional and not using it should not prevent importing 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() .

在此处输入图片说明

You may also use %matplotlib notebook , which gives you an interactive plot.

在此处输入图片说明

Finally, you may use %matplotlib tk to get a windowed figure like you would in PyCharm.

在此处输入图片说明

All of those options require to have imported matplotlib.pyplot . Importing matplotlib alone is not helpful. Also, if you experience any problems, start a new kernel first (don't try something new in line 27 of your notebook).

if you are using jupyter notebook in anaconda, matplotlib should be installed to the environment.

go to Environments -> the environment you are using -> change the droplist to not installed -> search matplotlib, and install

if you are using Anaconda CMD the use this command,

conda install matplotlib 

If you are using Normal CMD then use command,

pip install matplotlib

or

pip3 install matplotlib

在代码顶部%matplotlib inline添加%matplotlib inline ,它使 matplotlib 以交互方式执行

这段代码对我有用

%pip install matplotlib-inline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM