首页 文章

matplotlib中没有绘图窗口

提问于
浏览
76

我刚刚使用synaptic包系统在Ubuntu 9.10中安装了matplotlib . 但是,当我尝试以下简单示例时

>>> from pylab import plot;
>>> plot([1,2,3],[1,2,3])
[<matplotlib.lines.Line2D object at 0x9aa78ec>]

我没有绘图窗口 . 关于如何让情节窗口显示的任何想法?

10 回答

  • 9

    你可以输入

    import pylab
    pylab.show()
    

    或者更好,使用 ipython -pylab .

  • 122

    pylab.show() 可以阻止(你需要关闭窗口) .

    更方便的解决方案是在启动时执行 pylab.ion() (交互模式):all(相当于pylab) pyplot.* 命令立即显示其绘图 . More information on the interactive mode可以在官方网站上找到 .

    我还第二次使用更方便的 ipython -pylab--pylab ,在较新的版本中),它允许你跳过 from … import … 部分( %pylab 也适用于较新的IPython版本) .

  • -5

    试试这个:

    import matplotlib
    matplotlib.use('TkAgg')
    

    在导入pylab之前

  • 9

    出现任何错误?这可能是一个没有设置后端的问题 . 您可以从Python解释器或主目录中的配置文件( .matplotlib/matplotlibrc )进行设置 .

    要在代码中设置后端,您可以执行此操作

    import matplotlib
    matplotlib.use('Agg')
    

    其中'Agg'是后端的名称 . 哪些后端存在取决于您的安装和操作系统 .

    http://matplotlib.sourceforge.net/faq/installing_faq.html#backends

    http://matplotlib.org/users/customizing.html

  • 35

    下面的代码片段适用于Eclipse和Python shell:

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Come up with x and y
    x = np.arange(0, 5, 0.1)
    y = np.sin(x)
    
    # Just print x and y for fun
    print x
    print y
    
    # Plot the x and y and you are supposed to see a sine curve
    plt.plot(x, y)
    
    # Without the line below, the figure won't show
    plt.show()
    
  • 16

    现代IPython使用“ --matplotlib " argument with an optional backend parameter. It defaults to " auto”,这在Mac和Windows上通常都足够好 . 我没有在Ubuntu或任何其他Linux发行版上测试它,但我希望它可以工作 .

    ipython --matplotlib
    
  • 2

    如果你遇到一个问题,其中 pylab.show() 冻结了IPython窗口(这可能是特定于Mac OS X;不确定),你可以在IPython窗口中使用cmd -c,切换到绘图窗口,它就会爆发 .

    显然,未来对 pylab.show() 的调用不会冻结IPython窗口,只会是第一次调用 . 不幸的是,我发现每次重新安装matplotlib时,绘图窗口/与show()交互的行为都会发生变化,所以这个解决方案可能并不总是成立 .

  • 0

    如果您使用 --pylab 选项启动IPython,则不需要调用 show()draw() . 试试这个:

    ipython  --pylab=inline
    
  • 0

    --pylab 不再适用于Jupyter,但幸运的是我们可以在 ipython_config.py 文件中添加一个调整来获得 pylab 以及 autoreload 功能 .

    c.InteractiveShellApp.extensions = ['autoreload', 'pylab']
    c.InteractiveShellApp.exec_lines = ['%autoreload 2', '%pylab']
    
  • 0

    使用easy_install时的另一种可能性是您需要最新版本的matplotlib . 尝试:

    import pkg_resources
    pkg_resources.require("matplotlib")
    

    在导入matplotlib或其任何模块之前 .

相关问题