matplotlib中没有绘图窗口

dzh*_*lil 84 python ubuntu matplotlib

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

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

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

Pet*_*ter 132

你可以输入

import pylab
pylab.show()
Run Code Online (Sandbox Code Playgroud)

或更好,使用ipython -pylab.


Eri*_*got 37

pylab.show() 工作但阻止(你需要关闭窗口).

更方便的解决方案是pylab.ion()在启动时执行(交互模式):all(相当于pylab)pyplot.*命令立即显示其绘图.有关交互模式的更多信息,请访问官方网站.

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

  • @Steve:我的经验是,这很大程度上取决于后端.在Mac OS X上使用常规Python shell,我不必执行show()(或draw()):plot()打开一个新窗口. (3认同)

lin*_*res 18

试试这个:

import matplotlib
matplotlib.use('TkAgg') 
Run Code Online (Sandbox Code Playgroud)

在导入pylab之前

  • 这应该怎么办? (8认同)

neo*_*bot 10

下面的代码片段适用于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()
Run Code Online (Sandbox Code Playgroud)


Mat*_*yra 9

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

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

import matplotlib
matplotlib.use('Agg')
Run Code Online (Sandbox Code Playgroud)

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

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

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