Error when importing matplotlib in Python - python

I am new to python and am just getting started. I have a Jupyter Notebook from my university and have to plot something using matplotlib. All I find on the web is this:
import numpy as np
import matplotlib.pyplot as plt
but when I try that, it tells me
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-12-e0e1492b7973> in <module>
1 import numpy as np
----> 2 import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
Any help and further tips are very welcome.

matplotlib has to be installed
pip3 install matplotlib

since you have the jupyter tag, installing it with the Anaconda Prompt is an option
conda install matplotlib

Related

ModuleNotFoundError while importing 'emoji' in Jupyter

I get an error when I import the emoji into my Jupyter notebook.
This is the error I'm getting.
ModuleNotFoundError Traceback (most recent call last)
/var/folders/8v/gry0pxmn7tq64rhkjv504zr00000gn/T/ipykernel_12578/2329533640.py in <module>
2 import pandas as pd
3 import numpy as np
----> 4 import emoji
5 from collections import Counter
6 import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'emoji'
I am using MacOs. How do I solve this?
Did you install the library?
For the installation process via pip:
pip install emoji --upgrade
or via git:
git clone https://github.com/carpedm20/emoji.git
cd emoji
python setup.py install
That being done, you can import emoji into your code.
For further information you can read the installation process on the documentation here.

No module named cv2 in only one jupyter notebook

Before someone says that this is a repeated question and I should just install it, hear my case:
I am running a jupyter notebook from a conda environment in which opencv is already installed
I have had no problem using opencv so far in this environment.
I have downloaded a jupyter notebook, I have also created my own.
In my own newly created notebook I put
import numpy as np
import cv2
import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
#%matplotlib qt
%matplotlib inline
it runs without problems.
Then I open the other notebook (which is situated in the same directory as the previous one)
and I do
import numpy as np
import cv2
import glob
import matplotlib.pyplot as plt
%matplotlib qt
and I get
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-4-b35e53327fbb> in <module>
1 import numpy as np
----> 2 import cv2
3 import glob
4 import matplotlib.pyplot as plt
5 get_ipython().run_line_magic('matplotlib', 'qt')
ModuleNotFoundError: No module named 'cv2'
Why? It was running in the other notebook!
See if both notebooks are running in the same virtual environment.
You can type conda activate <env_name> before running the notebook with jupyter notebook command.

scikit-learn not importing in jupyter notebook

I installed scikit-learn both using pip and conda commandas. Whenever I check the package, it shows that it is already installed but, whenever I try import it, it shows error.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
%matplotlib inline
ERROR:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-33-82d3fc6531ea> in <module>
3 import matplotlib.pyplot as plt
4 import seaborn as sns
----> 5 from sklearn.model_selection import train_test_split
6 get_ipython().run_line_magic('matplotlib', 'inline')
ModuleNotFoundError: No module named 'sklearn'
I have tried reinstalling it multiple times but it shows the same error
Make sure that if you install the sklearn package in the python version that you are working. For suppose if your system has two versions of python installed in it like both python 2 and python 3 then you have installed sklearn package in python 2 and executing your code in python 3 or vice-versa.
I think you don't have latest version of sklearn because in old version of sklearn
below code was not working.
from sklearn.model_selection import train_test_split
Please update your sklearn by following command
conda update scikit-learn
I got the same error while installing these.
What you can do is install all the packages using the command
conda install pandas
and all other packages similarly or all in once
conda install pandas numpy matplotlib scikit-learn
in your project directory.
And now you can open your jupyter notbook using command
jupyter notebook
in the same environment or I should say same project directory.

How can I reinstall Matplotlib in my Anaconda Navigator to use in my Jupyter Notebook?

I installed Python and Jupyter through Anaconda. I tried to reinstall Matplotlib and, after this, probably this installation created some error when I try to import Matplotlib. Always when I try to import Matplotlib I have the error below.
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-15-a0d2faabd9e9> in <module>
----> 1 import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
I tried installing Matplotlib again through the Anaconda Navigator. I open it\ go to 'Environments'\ base(root)\ all.
Then, I select Matplotliband click 'Apply'. It does the installation. But afterwards, Matplotlib is still giving the same error.
import matplotlib.pyplot as plt
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-16-a0d2faabd9e9> in <module>
----> 1 import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
I would expect to get the import done, and start working with Matplotlib to create plots, but I only get this error.
If you have mapped your command line interpreter to python, open command prompt (Windows) or Terminal (Mac) and type:
conda install -c conda-forge matplotlib
This will automatically install matplotlib for you. You can search other packages on https://anaconda.org/ for future downloads.

Jupyter Notebook: ModuleNotFoundError: No module named 'matplotlib.pylot'

I've just received the following error message from inside Jupyter Notebooks.
*---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-e12bc46e5dd0> in <module>
----> 1 import matplotlib.pylot as plt
ModuleNotFoundError: No module named 'matplotlib.pylot'*
But FYI I've already installed matplotlib and I get the following message when I type "pip3 install matplotlib" into Terminal:
*Requirement already satisfied: matplotlib in /Users/mattbrown/miniconda3/lib/python3.7/site-packages (3.0.3)*
You just misspelled the lib name, it’s pyplot, not pylot ;)
Typo, use
import matplotlib.pyplot as plt
instead of
import matplotlib.pylot as plt
I'm so embarrassed! That did the trick. Fixed the typo and went with:
import matplotlib.pyplot as plt

Categories