I am currently practicing matplotlib. This is the first example I practice.
#!/usr/bin/python
import matplotlib.pyplot as plt
radius = [1.0, 2.0, 3.0, 4.0]
area = [3.14159, 12.56636, 28.27431, 50.26544]
plt.plot(radius, area)
plt.show()
When I run this script with python ./plot_test.py, it shows plot correctly. However, I run it by itself, ./plot_test.py, it throws the followings:
Traceback (most recent call last):
File "./plot_test.py", line 3, in <module>
import matplotlib.pyplot as plt
ImportError: No module named matplotlib.pyplot
Does python look for matplotlib in different locations?
The environment is:
Mac OS X 10.8.4 64bit
built-in python 2.7
numpy, scipy, matplotlib is installed with:
sudo port install py27-numpy py27-scipy py27-matplotlib \
py27-ipython +notebook py27-pandas py27-sympy py27-nose
pip will make your life easy!
Step 1: Install pip - Check if you have pip already simply by writing pip in the python console. If you don't have pip, get a python script called get-pip.py , via here: https://pip.pypa.io/en/latest/installing.html or directly here: https://bootstrap.pypa.io/get-pip.py (You may have to use Save As ..)
Step 2: Take note of where the file got saved and cd the directory from command prompt. Run the get-pip.py script to install pip.
You can write in cmd this line within quotes: "python .\get-pip.py"
Step 3: Now in cmd type: pip install matplotlib
And you should be through.
You have two pythons installed on your machine, one is the standard python that comes with Mac OSX and the second is the one you installed with ports (this is the one that has matplotlib installed in its library, the one that comes with macosx does not).
/usr/bin/python
Is the standard mac python and since it doesn't have matplotlib you should always start your script with the one installed with ports.
If python your_script.py works then change the #! to:
#!/usr/bin/env python
Or put the full path to the python interpreter that has the matplotlib installed in its library.
If you are using Python 2, just run
sudo apt-get install python-matplotlib
The best way to get matplotlib is :
pip install matplotlib
cause the previous way may give you a old version of matplotlib
This worked for me, inspired by Sheetal Kaul
pip uninstall matplotlib
python3 -m pip install matplotlib
I knew it installed in the wrong place when this worked:
python2.7
import matplotlib
use pip3 install matplotlib to install matlplot lib.
By default, pip will install those package for 2.7 as it the native one.
using pip3 makes it specific for python 3, and make sure you have only one version of python 3
First check the version of Python
For python2:
sudo apt-get install python-matplotlib
For python3:
sudo apt-get install python3-matplotlib
If you mismatch the Matplotlib installation and the Python version you will get the no-module-error because no module for that version exits.
If you using Anaconda3
Just put
conda install -c conda-forge matplotlib
You can install the matplotlib package in python 3 by doing this
python3 -m pip install matplotlib --user
It's working for me.
If you have pip installed and Python configured to the path variables, Just run this command in the terminal.
pip install matplotlib
So I used python3 -m pip install matplotlib then import matplotlib.pyplot as plt and it worked.
I had a similar problem, using pip3 and all these things worked for installing matplotlib but not pyplot. This solved it for me:
import matplotlib as plt
from matplotlib import pyplot as pllt
I had a similar issue that I resolved and here is my issue:
I set everything up on python3 but I was using python to call my file for example:
I was typing "python mnist.py" ...since I have everything on python3 it was thinking I was trying to use python 2.7
The correction:
"python3 mnist.py" - the 3 made all the difference
I'm by no means an expert in python or pip, but there is definitely a difference between pip and pip3 (pip is tied to python 2.7) (pip3 is tied to python 3.6)
so when installing for 2.7 do: pip install
when installing for 3.6 do: pip3 install
and when running your code for 2.7 do: python
when running your code for 3.6 do: python3
I hope this helps someone!
I bashed my head on this for hours until I thought about checking my .bash_profile. I didn't have a path listed for python3 so I added the following code:
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH
And then re-installed matplotlib with sudo pip3 install matplotlib. All is working beautifully now.
The file permissions on my virtual environment directory and my project directory were not correct and, thus, would not allow me to install the proper packages. I upadated them by running:
sudo chown user:user -R [project folder]
sudo chown user:user -R [environment folder]
In the above your should use your own usernames in place of "user". The -R recurses through all subfolders and files.
Working on the django project and faced same problem,
This is what I did.
Check if you have matplotlib already simply by writing pip show matplotlib in the python terminal.
If dont, run the command pip install matplotlib.(make sure you have pip downloaded)
Run the command pip show matplotlib again.
If the library is successfully downloaded, you can run your project (py manage.py runserver)
If you are in a venv, don't install these libs within the venv.
Installing them from outside helped me.
For me, with PyCharm and Python 3.8 on WIN10 environment, the solution was inside the IDE of PyCharm itself.
From the IDE and Project Setting (the gear on the top right corner), I've chosen Settings -->Project:Python-->Python Interpreter and you can see all the packages installed (and matplotlib wasn't present in the Package list).
Click on the + icon in the upper left corner for Package installation, and next will be opening a new window with the available packages for PyCharm. In the find box was sufficient to type matplotlib, select it and click on Install Package.
Here a screen-shot of PyCharm environment (as requested on 21/08/2021) :
If you experienced this error ModuleNotFoundError: No module named 'matplotlib' while using venv and running pytest like me, you can check by comparing the path type pytest shows and the path type python shows. In my case pytest wasn't under venv/bin directory like python, I installed pytest by pip and reactivated venv. See https://stackoverflow.com/a/54597424/3622415
I have installed matplotlib using both pip and conda but experienced this error ModuleNotFoundError: No module named 'matplotlib' .
I fix by following.
Because It might have an old version of Jupyter notebook, so i try this command to install it in the current kernel.
import sys
!{sys.executable} -m pip install seaborn
In the New Jupyter version (2019) can be installed simply as:
%pip install matplotlib
I solved by conda. Once installed miniconda or anaconda, type
conda install matplotlib
then, when prompted, type
y
in sublime text you can set
"cmd": ["python3", "-u", "$file"]
in tools > buildsystem > new build system
it worked for me
I am new to python and I'm following a tutorial using python3. I installed plotly and I can see it in the pip list and pip3 list and python help("modules"). However, when I tried to import the module (from plotly import offline) I got the error:
ModuleNotFoundError: No module named 'plotly'.
Tried to reinstall but it did not work.
I can import other modules in the list, like pygame or matplotlib without a problem.
It is usually a good idea to create a virtual environment to install additional modules. This will create a sealed Python environment with only the packages you specify. This usually fixes such errors, as Python sometimes installs packages where they can later not be found anymore. It is also a good idea to not use pip, as it might point to a pip from a different python version. Also it makes sense to upgrade pip before installing packages.
Try the following:
python3 -m venv env
. env/bin/activate (on Linux)
.\env\Scripts\activate (on Windows)
Check which packages we have:
python -m freeze
Here make sure this does not show anything except pkg-resources==0.0.0. Otherwise your PYTHONPATH might be mesed up.
Upgrade pip and install your package:
python -m pip install --upgrade pip
python -m pip install plotly
Run python and import your package:
python
>>> import plotly
If I typ this:
pip install plotly
in a CMD and this:
>>> from plotly import offline
In the interpreter it workes fine,
try uninstalling and reinstalling plotly:
pip uninstall plotly
Enter
y when it says Proceed (y/n)?
And install again with
pip install plotly
I had the same problem; after uninstalling/installing plotly, use venv, I realised (after 2h) that my script name was "plotly.py" => that was the cause of the error
I've been attempting to install Matplotlib for a graphing project in Python. In accordance with recommendation from the Matplotlib website, I installed Anaconda as a pre-packaged python distributor. Anaconda seems to have installed correctly.
To install matplotlib, I typed in the command line:
pip install matplotlib
Which brings up multiple messages stating: "Requirement already satisfied."
When in my python script I typed:
import matplotlib.pyplot as plt
I received an error message stating:
ImportError: No module named matplotlib.pyplot
I'm using an old Windows XP operating system.
I've looked everywhere for help, and have tried installing matplotlib numerous times via the command line! Any help would be greatly appreciated...
Thank you!!
Make sure your version of pip corresponds to your version of python. One way to do this is the following:
python -m pip install matplotlib
The -m for module means that it will look in the site-packages for that python for the pip module.
You can also do:
>>> import sys
>>> print("\n".join(sys.path))
to list the path as understood by python, then check whether matplotlib is indeed on one of the listed paths (usually site-packages).
To find the locations of pip and python use the following on the Windows console:
where python
where pip
From the path you should be able to determine whether pip and python are from the same package. If not, uninstall one of the python installations, or at least remove it from the PATH variable.
Okay so I tried to install matplotlib and python earlier from the command line. Turns out i probably already had python installed but it was not make(ing) plots. Then that didn't work so i tried to, from the software center install matplotlib. So my software center says it is installed. However, when i am in python it still can not find matplotlib. Also when i am not in python and try to run commands like
python setup.py clean
It says python cannot find setup.py
This has been going on for hours and i am a beginner linux user and it is horrible
From the command line you can install matplotlib as follows:
$ sudo apt-get install python-matplotlib
Once it's installed you can access it from inside python like this:
>>> import matplotlib
>>> help(matplotlib)
If you want more control over the version of python you're using, I recommend using Anaconda. Very easy to install and use.
If you want even more control you can build from source. That post is specifically about setting up IPython but the python build step are the same.
I have pip installed matplotlib in my virtualenv and I am trying to plot a simple graph. I use Eclipse and PyDev. When I run the script from Eclipse it doesn't display any graph at all. I have tried the suggestions proposed in other questions such as adding plt.ion() but that doesn't work either. I have also tried the same code in the console and again nothing. Is this a problem with my configuration? If so how can I fix it?
The failing code is:
import matplotlib.pyplot as plt
radius = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
area = [3.14159, 12.56636, 28.27431, 50.26544, 78.53975, 113.09724]
plt.plot(radius, area)
plt.show()
The code is not the problem. It works just fine out of virtualenv. The problem is when I use it in virtualenv. The problem is somehow related to my configuration. And just to clear things up I DON'T GET ANY error messages. It's just that the window wouldn't show up.
Thanks
Your code works inside my virtualenv on OSX 10.7 with Python 2.7:
What version of Python are you using inside your virtualenv? My guess is that either you have not installed a matplotlib dependency or your installation of an installed dependency was not properly performed. On Python 2.7 here is what I did to install matplotlib. Can you try these steps in a new virtualenv and see if it works for you?
pip install numpy
pip install scipy
easy_install matplotlib
I had the same issue, and installing matplotlib using easy_install instead of pip did not solve it. In the end, I found out that the problem was simply that matplotlib could not find any backend for plotting.
I solved it by doing the following (I am using Debian wheezy):
pip uninstall matplotlib
sudo apt-get install tcl-dev tk-dev
pip install matplotlib
First off, you might want to check out:
http://matplotlib.org/faq/installing_faq.html#matplotlib-compiled-fine-but-nothing-shows-up-when-i-use-it
To see what's going wrong, check out the matplotlib using git instead of either pip or easy_install. We're going to do a more manual install:
git clone git#github.com:matplotlib/matplotlib.git
cd matplotlib
python setup.py
This will print out the configuration of what pip or easy_install would have done. Look through the "OPTIONAL BACKEND DEPENDENCIES" and make sure that some of the ones that produce windows are enabled (Tkinter, Gtk+, Mac OS X native, Qt, Cairo, etc.). If you see that none of these are available, then you need to install some of these libraries for your operating system before you continue installing matplotlib.
After installing say, Tk (on ubuntu: sudo apt-get install tcl-dev tk-dev), then when you re-run
python setup.py
you will see that the Tk backend is enabled. Proceeding with
python setup.py build && python setup.py install
should get you up and running... but at that point you might even just delete the whole git clone directory and go back to installing with pip.
If you have a version of matplotlib installed on your system version of python, then you can link to the system version of matplotlib. On my machine I did the following:
cd $VIRTUAL_ENV/lib/python2.7/site-packages
ln -s /usr/lib/pymodules/python2.7/matplotlib .
ln -s /usr/lib/pymodules/python2.7/matplotlib-1.1.1rc .
This avoids many of the problems with getting matplotlib to work in the virtualenv but limits you to using the system version of matplotlib (which on this machine is not too bad).
This method also allows you to use the --no-site-packages, but still have matplotlib work for you.
I am using Ubuntu 12.04 and Python 2.7.3 on my computer and
when I use the matplotlib 1.2.0 in my virtualenv, the show() did not work until I upgrade it to the 1.2.1... All the bugs I had previous went away...
By doing this you will install all the dependencies of the matplotlib:
sudo apt-get build-dep python-matplotlib
To get the latest version of the matplotlib you can use:
pip install matplotlib
or upgrade it:
pip install matplotlib --upgrade