2
votes

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
1
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? - Rory Yorke
@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. :) - jmm

1 Answers

0
votes

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