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
Community
  • 1
  • 1
jmm
  • 131
  • 1
  • 5
  • 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 Mar 30 '14 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](http://matplotlib.org/users/customizing.html#customizing-matplotlib). But now that I installed the right dependencies to use tk as a backend (see my edit), it is working. :) – jmm Mar 31 '14 at 09:57

1 Answers1

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
Ben
  • 6,986
  • 6
  • 44
  • 71
  • Wouldn't that install matplotlib on python 2? I am starting out with python, so I've decided to go with python 3. – jmm Mar 31 '14 at 09:51
  • you're right! sorry, I had meant to change that. It should just be `python3-matplotlib` Edited now – Ben Mar 31 '14 at 16:32
  • that was easy peasy .. it looks like it actually replaced the pip3 packages with native compatible ones. Jammy release – Gautam Aug 30 '23 at 15:25