-
- Posts: 146
- Joined: Tue Mar 24, 2015 7:11 am
matplotlib and Python 3
I can use matplotlib when I'm in Python 2, but if I try to import it when I'm using Python 3, I get a "no module named matplotlib..." This forum suggests I need to do "sudo apt-get install python3-matplotlib", but when I try this, I get "Unable to locate package..." How can I import matplotlib in Python 3?
I'm using Python 2.7 and 3.2, and a Raspi 2.
I'm using Python 2.7 and 3.2, and a Raspi 2.
Re: matplotlib and Python 3
Code: Select all
sudo pip3 install matplotlib
There are 10 types of people: those who understand binary and those who don't.
-
- Posts: 146
- Joined: Tue Mar 24, 2015 7:11 am
Re: matplotlib and Python 3
If I do 'sudo pip3 install matplotlib' I get the message 'sudo: pip3: command not found'
-
- Posts: 690
- Joined: Tue Jun 16, 2015 6:01 am
Re: matplotlib and Python 3
Probably could have Googled that error message and got your problem solved, but here's the spoon-feed:
Code: Select all
sudo apt install python3-pip
Account Inactive
Re: matplotlib and Python 3
If you are running Jessie, matplotlib for Python3 is in the repositories.
Follow the prompts, and this should get it for you.
Hope this helps,
Dave.
Code: Select all
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-matplotlib
Hope this helps,
Dave.
-
- Posts: 146
- Joined: Tue Mar 24, 2015 7:11 am
Re: matplotlib and Python 3
When I do 'sudo apt-get install python3-pip', I am told that my python3-pip is already the newest version. (I do try Googling, but I am not yet geeky enough to understand most of what I find.)
I'm running wheezy, and when I follow Dave's suggestion, I get 'unable to locate package python3-matplotlib'
I'm running wheezy, and when I follow Dave's suggestion, I get 'unable to locate package python3-matplotlib'
-
- Posts: 25303
- Joined: Tue Mar 25, 2014 12:40 pm
- Location: Delightful Dorset
Re: matplotlib and Python 3
emeyeraway wrote:When I do 'sudo apt-get install python3-pip', I am told that my python3-pip is already the newest version. (I do try Googling, but I am not yet geeky enough to understand most of what I find.)
I'm running wheezy, and when I follow Dave's suggestion, I get 'unable to locate package python3-matplotlib'
Raspbian Jessie is the current version, Raspbian Wheezy is end of life and does not have the packages in its Repository.
.
The information is out there....you just have to let it in.
My other Linux machine is a ChromeBox
My other Linux machine is a ChromeBox
Re: matplotlib and Python 3
That's why I started by saying "If you are running Jessie"!emeyeraway wrote:I'm running wheezy, and when I follow Dave's suggestion, I get 'unable to locate package python3-matplotlib'
The Wheezy repositories are old and the packages it contains are not being updated. Even the version of pip in Wheezy is old and outdated. It's missing many of the commands of the up to date version, such as 'list' and 'show', and it's why you get a 'pip3 not found' error.
If you want to try to load matplotlib for Python3 using pip on Wheezy, you can try:
Code: Select all
pip-3.2 install matplotlib
As Fruity says, Jessie is the way to go, and the best way to get there is to start with a fresh copy from here.
Hope this helps,
Dave.
-
- Posts: 146
- Joined: Tue Mar 24, 2015 7:11 am
Re: matplotlib and Python 3
Thanks to all who responded to my pleas. I will try to upgrade to jesse without catastrophe. (How can I stay a Beginner for such a long time that my software gets out of date?)
-
- Posts: 25303
- Joined: Tue Mar 25, 2014 12:40 pm
- Location: Delightful Dorset
Re: matplotlib and Python 3
viewtopic.php?f=66&t=121880emeyeraway wrote:Thanks to all who responded to my pleas. I will try to upgrade to jesse without catastrophe. (How can I stay a Beginner for such a long time that my software gets out of date?)
The information is out there....you just have to let it in.
My other Linux machine is a ChromeBox
My other Linux machine is a ChromeBox
-
- Posts: 146
- Joined: Tue Mar 24, 2015 7:11 am
Re: matplotlib and Python 3
I started with a new 32GB SD, and downloaded the new version with jessie instead of wheezy. Now I am able to run "sudo apt-get install python3-matplotlib" successfully. (I also ran the same thing without the "3" for use with python 2.)
Now, when I do the identical simple plotting routine in python 2 and 3, it works as expected in python 2, but no plot is produced in python 3.
So I'm still missing something in python 3. Googling the matter hasn't helped. Nothing I've found there goes beyond what I find here on the forum.
Now, when I do the identical simple plotting routine in python 2 and 3, it works as expected in python 2, but no plot is produced in python 3.
So I'm still missing something in python 3. Googling the matter hasn't helped. Nothing I've found there goes beyond what I find here on the forum.
Re: matplotlib and Python 3
Odd, do you get any error messages when trying to run matplotlib in Python3?emeyeraway wrote:Now, when I do the identical simple plotting routine in python 2 and 3, it works as expected in python 2, but no plot is produced in python 3.
Can you post the output from the following commands:
Code: Select all
apt-cache policy python3
apt-cache policy python3-matplotlib
Thanks,
Dave.
-
- Posts: 146
- Joined: Tue Mar 24, 2015 7:11 am
Re: matplotlib and Python 3
Here's the ouput from those commands:
pi@raspberrypi:~ $ apt-cache policy python3
python3:
Installed: 3.4.2-2
Candidate: 3.4.2-2
Version table:
*** 3.4.2-2 0
500 http://mirrordirector.raspbian.org/raspbian/ jessie/main armhf Packages
100 /var/lib/dpkg/status
pi@raspberrypi:~ $ apt-cache policy python3-matplotlib
python3-matplotlib:
Installed: 1.4.2-3.1
Candidate: 1.4.2-3.1
Version table:
*** 1.4.2-3.1 0
500 http://mirrordirector.raspbian.org/raspbian/ jessie/main armhf Packages
100 /var/lib/dpkg/status
I am using a monitor with my Raspi.
pi@raspberrypi:~ $ apt-cache policy python3
python3:
Installed: 3.4.2-2
Candidate: 3.4.2-2
Version table:
*** 3.4.2-2 0
500 http://mirrordirector.raspbian.org/raspbian/ jessie/main armhf Packages
100 /var/lib/dpkg/status
pi@raspberrypi:~ $ apt-cache policy python3-matplotlib
python3-matplotlib:
Installed: 1.4.2-3.1
Candidate: 1.4.2-3.1
Version table:
*** 1.4.2-3.1 0
500 http://mirrordirector.raspbian.org/raspbian/ jessie/main armhf Packages
100 /var/lib/dpkg/status
I am using a monitor with my Raspi.
Re: matplotlib and Python 3
Those apt-cache policy outputs look good.
Do you run your Pi from the command line, or are you using the GUI?
Do you see any errors when matplotlib does not output with Python3?
Thanks,
Dave.
Do you run your Pi from the command line, or are you using the GUI?
Do you see any errors when matplotlib does not output with Python3?
Thanks,
Dave.
-
- Posts: 146
- Joined: Tue Mar 24, 2015 7:11 am
Re: matplotlib and Python 3
I use the GUI when running python programs.
No error message with python 3; it just does not show anything until the >>> appears indicating it's all done.
Thanks for hanging in there with me!
No error message with python 3; it just does not show anything until the >>> appears indicating it's all done.
Thanks for hanging in there with me!
Re: matplotlib and Python 3
Can you post the Python3 code that's not working?
Use code tags when posting code, click the "code" button above the post editing window, and paste the code between the tags,
Thanks,
Dave.
Use code tags when posting code, click the "code" button above the post editing window, and paste the code between the tags,
Thanks,
Dave.
-
- Posts: 146
- Joined: Tue Mar 24, 2015 7:11 am
Re: matplotlib and Python 3
Code: Select all
import matplotlib.pyplot as plt
x=[1,2,3,4]
y=[2,4,6,8]
plt.plot(x,y)
plt.show()
Re: matplotlib and Python 3
Not sure what you problem is, as this code works for me.
I've tried running it as a file from the terminal in Python and Python3, running it as a file in IDLE and IDLE3, and hand typing it line by line into both the Python and the Python3 interpreter and it works every time. I've also tried it with a monitor, and headless via a windows PC, works every time!
Do you use IDLE3, or a text editor on the LXTerminal?
Try opening an LXTerminal, start the Python3 interpreter with:and type in the code line by line.
You will get a message about a matplotlib object after the third line, that is normal. After you enter the fourth line, a new window should open with the plot.
Try it, and report any error messages that appear on the terminal
Thanks,
Dave.
I've tried running it as a file from the terminal in Python and Python3, running it as a file in IDLE and IDLE3, and hand typing it line by line into both the Python and the Python3 interpreter and it works every time. I've also tried it with a monitor, and headless via a windows PC, works every time!
Do you use IDLE3, or a text editor on the LXTerminal?
Try opening an LXTerminal, start the Python3 interpreter with:
Code: Select all
python3
You will get a message about a matplotlib object after the third line, that is normal. After you enter the fourth line, a new window should open with the plot.
Try it, and report any error messages that appear on the terminal
Thanks,
Dave.
-
- Posts: 146
- Joined: Tue Mar 24, 2015 7:11 am
Re: matplotlib and Python 3
In general, I start by entering "sudo idle3" in the terminal window. Then, when the blank python page opens, I click on "File", drag to "Open" and choose the file I want to run.
I did what you suggested in the terminal window, noticed the line that appeared as you said it would, after "plt.plot(x,y)", but after entering "plt.show()" nothing happens, except that the ">>>" appears. No plot, no error messages.
If I do exactly the same thing after entering "python" instead of "python3" everything works as it should.
Might this be relevant?: Instead of starting with a blank SD, I took the one that I had used in my Raspi Zero, because it was new enough so that jessie was on it, not wheezy, and it was hardly used at all. I did manage to connect it to my wireless router, but after that, I just put it away until I took its SD card for my Raspi 2B. This was less than a month ago.
I did what you suggested in the terminal window, noticed the line that appeared as you said it would, after "plt.plot(x,y)", but after entering "plt.show()" nothing happens, except that the ">>>" appears. No plot, no error messages.
If I do exactly the same thing after entering "python" instead of "python3" everything works as it should.
Might this be relevant?: Instead of starting with a blank SD, I took the one that I had used in my Raspi Zero, because it was new enough so that jessie was on it, not wheezy, and it was hardly used at all. I did manage to connect it to my wireless router, but after that, I just put it away until I took its SD card for my Raspi 2B. This was less than a month ago.
Re: matplotlib and Python 3
An up to date SD card should run on any Pi.emeyeraway wrote:Might this be relevant?: Instead of starting with a blank SD, I took the one that I had used in my Raspi Zero, because it was new enough so that jessie was on it, not wheezy, and it was hardly used at all. I did manage to connect it to my wireless router, but after that, I just put it away until I took its SD card for my Raspi 2B. This was less than a month ago.
This procedure should work (it works for me), although you should just use "idle3" unless you really need "sudo". It's not needed for things like GPIO access anymore, although you will find it in older tutorials.emeyeraway wrote:In general, I start by entering "sudo idle3" in the terminal window. Then, when the blank python page opens, I click on "File", drag to "Open" and choose the file I want to run.
After you run your program in IDLE3, are you left with a restart line, and two prompts like this?
Code: Select all
>>> ========== RESTART ==========
>>>
>>>
Code: Select all
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install --reinstall python3
sudo apt-get install --reinstall python3-matplotlib
Dave.
-
- Posts: 146
- Joined: Tue Mar 24, 2015 7:11 am
Re: matplotlib and Python 3
Yes, after I try the code in python 3, the screen looks exactly like that: Restart with two >>> underneath.
I did exactly what you suggested about reinstalling; rebooted, but still the same results!
At this point, getting python 3 to run is pretty much academic; I can always just use python 2 for any plotting. It just bugs me that something the should work doesn't, and I haven't been able to solve the dilemma. I do appreciate all the time and thought you have put in trying to help; Thank You!
I did exactly what you suggested about reinstalling; rebooted, but still the same results!
At this point, getting python 3 to run is pretty much academic; I can always just use python 2 for any plotting. It just bugs me that something the should work doesn't, and I haven't been able to solve the dilemma. I do appreciate all the time and thought you have put in trying to help; Thank You!
-
- Posts: 51
- Joined: Wed Mar 30, 2016 5:22 am
Re: matplotlib and Python 3
Hello,
I try everything to install,
but still have the import error: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package.
So do you have a solution?
thanks
Julien
I try everything to install,
but still have the import error: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package.
So do you have a solution?
thanks
Julien
Re: matplotlib and Python 3
First, make sure that you do not have a file called "matplotlib" in your working directory when you run your Python program. If you do, rename it.
If you still have the same problem, post the results of these three commands:
Dave.
If you still have the same problem, post the results of these three commands:
Code: Select all
uname -a
apt-cache policy python-matplotlib
apt-cache policy python3-matplotlib
-
- Posts: 51
- Joined: Wed Mar 30, 2016 5:22 am
Re: matplotlib and Python 3
Hello,
I had my program named matplotlib.py. I rename it.
But still hav problems:
on python3: error bad magic number in 'matplotlib'
and if I use python 2.7: Import error: no module named pyplot!??
4.9.24-V7+ #993 SMP Wed Apr 26 BST 2017 armv71 CNU/linux
version 1.4.2-3.1 for python and python3
A try to purge and re-install but always error.
thanks to your help.
Julien
I had my program named matplotlib.py. I rename it.
But still hav problems:
on python3: error bad magic number in 'matplotlib'
and if I use python 2.7: Import error: no module named pyplot!??
4.9.24-V7+ #993 SMP Wed Apr 26 BST 2017 armv71 CNU/linux
version 1.4.2-3.1 for python and python3
A try to purge and re-install but always error.
thanks to your help.
Julien
Re: matplotlib and Python 3
For Python3, I think you still have a file called "matplotlib" that Python3 is finding in it's path before it finds the real thing.
Report the output of:
For Python2, what happens if you start an interactive Python terminal and enter:
Dave.
Report the output of:
Code: Select all
sudo find / -name matplotlib*
Code: Select all
import matplotlib.pyplot as plt