matplotlib.pyplot在python3中执行savefig()而不是show(),但在2.7中工作

时间:2014-06-24 18:43:58

标签: python matplotlib

这是我的第二篇文章,因为我正在学习python并在matplotlib上尝试过我的手。我在matplot网站上使用了以下示例,如果我从3文件中的shebang行中移除python之后的plotexample.py,它可以正常工作(即显示图表)。 我在Ubuntu 12.04上,并在终端上运行以下命令:

$chmod +x plotexample.py
$./plotexample.py

#!/usr/bin/env python3

import matplotlib
from numpy.random import randn
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

def to_percent(y, position):
    # Ignore the passed in position. This has the effect of scaling the default
    # tick locations.
    s = str(100 * y)

    # The percent symbol needs escaping in latex
    if matplotlib.rcParams['text.usetex'] == True:
        return s + r'$\%$'
    else:
        return s + '%'

x = randn(5000)

# Make a normed histogram. It'll be multiplied by 100 later.
plt.hist(x, bins=50, normed=True)

# Create the formatter using the function to_percent. This multiplies all the
# default labels by 100, making them all percentages
formatter = FuncFormatter(to_percent)

# Set the formatter
plt.gca().yaxis.set_major_formatter(formatter)
plt.savefig("test.pdf") 
plt.show()

我看了一些其他帖子并从那些帖子中读取,运行如下:

>>> import matplotlib
>>> matplotlib.get_backend()
'TkAgg'

我还从软件包库中安装了tk-dev(8.5.0-2),并重试了所有内容,但还没有运气。如何解决show()问题,因为我不想每次都savefig()。{/ p>

我也运行了以下内容以确保我有正确的安装(或者我认为)并且安装正常但是没有解决问题

sudo pip3 install matplotlib

当我等待回复时,我也在python提示符下尝试了以下内容:

>>> import matplotlib
>>> matplotlib.use('GTKAgg')
>>> 

所以,我编辑了我的文件内容:

#!/usr/bin/env python3

import matplotlib
matplotlib.use('GTKAgg')
from numpy.random import randn
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

但是在运行程序时,我收到以下错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.3.1-py3.2-linux-i686.egg/matplotlib/backends/backend_gtk.py", line 12, in <module>
    import gobject
ImportError: No module named gobject

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./plotexample.py", line 6, in <module>
    import matplotlib.pyplot as plt
  File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.3.1-py3.2-linux-i686.egg/matplotlib/pyplot.py", line 98, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.3.1-py3.2-linux-i686.egg/matplotlib/backends/__init__.py", line 28, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.3.1-py3.2-linux-i686.egg/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
    from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
  File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.3.1-py3.2-linux-i686.egg/matplotlib/backends/backend_gtk.py", line 16, in <module>
    raise ImportError("Gtk* backend requires pygtk to be installed.")
ImportError: Gtk* backend requires pygtk to be installed.

我做了一些googl'ing并发现Python3没有pygtk。那是对的吗。我如何解决这个问题,或者如果我想让show()工作

,我是否会使用旧版本的Python

我在安装tk-dev之后运行sudo pip3 install matplotlib,删除python3-tk并重新安装python3-tk。

 Requirement already satisfied (use --upgrade to upgrade): matplotlib in /usr/local/lib/python3.2/dist-packages/matplotlib-1.3.1-py3.2-linux-i686.egg
    Requirement already satisfied (use --upgrade to upgrade): numpy>=1.5 in /usr/lib/python3/dist-packages (from matplotlib)
    Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /usr/local/lib/python3.2/dist-packages/python_dateutil-2.2-py3.2.egg (from matplotlib)
    Requirement already satisfied (use --upgrade to upgrade): tornado in /usr/local/lib/python3.2/dist-packages (from matplotlib)
    Requirement already satisfied (use --upgrade to upgrade): pyparsing>=1.5.6,!=2.0.0 in /usr/local/lib/python3.2/dist-packages/pyparsing-2.0.2-py3.2.egg (from matplotlib)
    Requirement already satisfied (use --upgrade to upgrade): nose in /usr/local/lib/python3.2/dist-packages (from matplotlib)
    Requirement already satisfied (use --upgrade to upgrade): six in /usr/local/lib/python3.2/dist-packages/six-1.7.2-py3.2.egg (from python-dateutil->matplotlib)
    Cleaning up...

0 个答案:

没有答案