简体   繁体   中英

Matplotlib does not display figure

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM