emeyeraway
Posts: 146
Joined: Tue Mar 24, 2015 7:11 am

matplotlib and Python 3

Thu Aug 04, 2016 4:26 pm

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.

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: matplotlib and Python 3

Thu Aug 04, 2016 9:33 pm

Code: Select all

sudo pip3 install matplotlib
There are 10 types of people: those who understand binary and those who don't.

emeyeraway
Posts: 146
Joined: Tue Mar 24, 2015 7:11 am

Re: matplotlib and Python 3

Thu Aug 04, 2016 10:14 pm

If I do 'sudo pip3 install matplotlib' I get the message 'sudo: pip3: command not found'

SonOfAMotherlessGoat
Posts: 690
Joined: Tue Jun 16, 2015 6:01 am

Re: matplotlib and Python 3

Fri Aug 05, 2016 12:13 am

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

dgordon42
Posts: 795
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: matplotlib and Python 3

Fri Aug 05, 2016 11:22 am

If you are running Jessie, matplotlib for Python3 is in the repositories.

Code: Select all

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-matplotlib
Follow the prompts, and this should get it for you.

Hope this helps,
Dave.

emeyeraway
Posts: 146
Joined: Tue Mar 24, 2015 7:11 am

Re: matplotlib and Python 3

Fri Aug 05, 2016 6:22 pm

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'

fruitoftheloom
Posts: 25303
Joined: Tue Mar 25, 2014 12:40 pm
Location: Delightful Dorset

Re: matplotlib and Python 3

Fri Aug 05, 2016 6:59 pm

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

dgordon42
Posts: 795
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: matplotlib and Python 3

Fri Aug 05, 2016 8:03 pm

emeyeraway wrote:I'm running wheezy, and when I follow Dave's suggestion, I get 'unable to locate package python3-matplotlib'
That's why I started by saying "If you are running Jessie"!

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
I've just tried this on an old model B running Wheezy, and it comes back with lots of errors about outdated packages.

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.

emeyeraway
Posts: 146
Joined: Tue Mar 24, 2015 7:11 am

Re: matplotlib and Python 3

Sat Aug 06, 2016 5:45 pm

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?)

fruitoftheloom
Posts: 25303
Joined: Tue Mar 25, 2014 12:40 pm
Location: Delightful Dorset

Re: matplotlib and Python 3

Sat Aug 06, 2016 7:27 pm

emeyeraway 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?)
viewtopic.php?f=66&t=121880
The information is out there....you just have to let it in.

My other Linux machine is a ChromeBox

emeyeraway
Posts: 146
Joined: Tue Mar 24, 2015 7:11 am

Re: matplotlib and Python 3

Sun Aug 07, 2016 11:12 pm

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.

dgordon42
Posts: 795
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: matplotlib and Python 3

Mon Aug 08, 2016 10:42 am

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.
Odd, do you get any error messages when trying to run matplotlib in Python3?
Can you post the output from the following commands:

Code: Select all

apt-cache policy python3
apt-cache policy python3-matplotlib
Do you run your Pi on a monitor, or access it "headlessly" from another computer?

Thanks,
Dave.

emeyeraway
Posts: 146
Joined: Tue Mar 24, 2015 7:11 am

Re: matplotlib and Python 3

Mon Aug 08, 2016 2:33 pm

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.

dgordon42
Posts: 795
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: matplotlib and Python 3

Mon Aug 08, 2016 3:16 pm

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.

emeyeraway
Posts: 146
Joined: Tue Mar 24, 2015 7:11 am

Re: matplotlib and Python 3

Mon Aug 08, 2016 4:31 pm

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!

dgordon42
Posts: 795
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: matplotlib and Python 3

Mon Aug 08, 2016 4:47 pm

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.

emeyeraway
Posts: 146
Joined: Tue Mar 24, 2015 7:11 am

Re: matplotlib and Python 3

Mon Aug 08, 2016 5:56 pm

Code: Select all

import matplotlib.pyplot as plt
x=[1,2,3,4]
y=[2,4,6,8]
plt.plot(x,y)
plt.show()

dgordon42
Posts: 795
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: matplotlib and Python 3

Mon Aug 08, 2016 8:08 pm

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:

Code: Select all

python3
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.

emeyeraway
Posts: 146
Joined: Tue Mar 24, 2015 7:11 am

Re: matplotlib and Python 3

Mon Aug 08, 2016 8:48 pm

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.

dgordon42
Posts: 795
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: matplotlib and Python 3

Mon Aug 08, 2016 9:52 pm

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.
An up to date SD card should run on any Pi.
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.
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.

After you run your program in IDLE3, are you left with a restart line, and two prompts like this?

Code: Select all

>>> ========== RESTART ==========
>>>
>>>
You could try a re-installation of Python3 and matplotlib with:

Code: Select all

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install --reinstall python3
sudo apt-get install --reinstall python3-matplotlib
and reboot.

Dave.

emeyeraway
Posts: 146
Joined: Tue Mar 24, 2015 7:11 am

Re: matplotlib and Python 3

Mon Aug 08, 2016 11:41 pm

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!

julien.launay
Posts: 51
Joined: Wed Mar 30, 2016 5:22 am

Re: matplotlib and Python 3

Tue May 09, 2017 6:25 am

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

dgordon42
Posts: 795
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: matplotlib and Python 3

Tue May 09, 2017 10:07 am

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:

Code: Select all

uname -a
apt-cache policy python-matplotlib
apt-cache policy python3-matplotlib
Dave.

julien.launay
Posts: 51
Joined: Wed Mar 30, 2016 5:22 am

Re: matplotlib and Python 3

Tue May 09, 2017 11:21 am

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

dgordon42
Posts: 795
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: matplotlib and Python 3

Tue May 09, 2017 2:52 pm

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:

Code: Select all

sudo find / -name matplotlib*
For Python2, what happens if you start an interactive Python terminal and enter:

Code: Select all

import matplotlib.pyplot as plt
Dave.

Return to “Beginners”