2

I am using python 3 in Ubuntu 13.04.

This simple example runs without errors, but it doesn't display any plot:

import matplotlib.pyplot as plt

x = [1,2,3,4]
y = [4,3,2,1]

plt.plot(x, y)
plt.show()

I have tried to change the backend from Agg to TkAgg but I get the following error:

Traceback (most recent call last):

File "test2.py", line 1, in

import matplotlib.pyplot as plt

File "/usr/local/lib/python3.2/dist-packages/matplotlib/pyplot.py", line> 98, in

_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()

File "/usr/local/lib/python3.2/dist-packages/matplotlib/backends/init.py", line 28, in pylab_setup

globals(),locals(),[backend_name],0)

File "/usr/local/lib/python3.2/dist-packages/matplotlib/backends/backend_tkagg.py",line 11, in

import matplotlib.backends.tkagg as tkagg   

File "/usr/local/lib/python3.2/dist-packages/matplotlib/backends/tkagg.py",line 2, in

from matplotlib.backends import _tkagg 

ImportError: cannot import name _tkagg

Does anyone have any idea how to solve this?

EDIT:

I have found the answer here. In case someone has the same problem, the solution is to install the tk-dev package on ubuntu, and then reinstall matplotlib. In my case:

sudo apt-get install tk-dev
sudo pip3 uninstall matplotlib
sudo pip3 install matplotlib
2
  • This isn't much help, but your example works fine for me with Ubuntu 13.10 under python2 and python3. This was with whatever the default backend is; I don't know how to change it. I notice a /usr/local/lib/python3.2 in your backtrace---are you using a matplotlib library you've compiled yourself? Commented Mar 30, 2014 at 17:19
  • @Rory Yorke: I've installed it using pip3. I read somewhere that the backend might be a problem, so I changed it in the matplotlibrc file. But now that I installed the right dependencies to use tk as a backend (see my edit), it is working. :) Commented Mar 31, 2014 at 9:57

1 Answer 1

1

I ran into some similar issues with this. It's better to install matplotlib using apt-get instead of pip

sudo apt-get install python3-matplotlib

- via some discussion that I can't find anymore (I'll update the post if I come across it again)

"on Ubuntu (>= 12.10), you may install the dependencies for each package as:"

sudo apt-get build-dep python3-matplotlib
Sign up to request clarification or add additional context in comments.

3 Comments

Wouldn't that install matplotlib on python 2? I am starting out with python, so I've decided to go with python 3.
you're right! sorry, I had meant to change that. It should just be python3-matplotlib Edited now
that was easy peasy .. it looks like it actually replaced the pip3 packages with native compatible ones. Jammy release

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.