3

我在 Linux 上运行 IPython QT 控制台的 IPython 中有以下代码。

%pylab inline

Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline].
For more information, type 'help(pylab)'.

fig = figure()

ax = fig.add_axes()

ax = fig.add_axes([0,500, 0, 5000])

ax.plot([1,2,3,44], [4,4,55,55])
Out[5]: [<matplotlib.lines.Line2D at 0x3d8e7d0>]

fig
Out[6]: <matplotlib.figure.Figure at 0x3d25fd0>

fig.show()
/usr/lib/pymodules/python2.7/matplotlib/figure.py:362: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
  "matplotlib is currently using a non-GUI backend, "

一段时间以来,我一直在努力完成这项工作,我尝试使用matplotlib.use()to手动更改后端Qt4AggGTK但没有成功。IPython notebook即使我打电话也会发生这种情况display()

任何想法如何让内联绘图工作?


将 Jakob 的答案标记为答案,但实际上两者都是正确的。我必须用新副本替换 matploblibrc 文件,使用 --pylab=None 启动 IPython QT 控制台,然后在控制台中手动输入 %pylab inline。不知何故,这解决了这个问题。

4

2 回答 2

4

轴对象定义不正确,这会阻止 matplotlib 渲染。
删除第一ax = fig.add_axes()行,并将第二行替换为
ax = fig.add_axes([0, 0, 1, 1]).

add_axes 方法在相对坐标中请求轴的大小,形式为left、bottom、width、height,值在 0 和 1 之间,参见matplotlib tutorial

您也可以尝试fig.add_subplot(111)代替fig.add_axes()fig,ax = subplots()创建您的图形和轴对象。后者假定您已经在 IPython 中填充了交互式命名空间 matplotlib ( %pylab ) 调用。

于 2013-10-23T06:12:51.653 回答
1

看起来您的 matplotlib 构建是在没有 gui 后端的情况下编译的。

当 a) 明确指定(对 web 服务器很方便),或 b) 至少一个 gui 后端所需的库不存在(例如 no TkGtkQt等)时,会执行此操作。

你是如何安装 matplotlib 的?

如果您从源代码编译它,请确保您至少安装了 Tk 的开发库,并且您的 python 安装是使用 Tk 支持编译的(默认情况下)。如果您从发行版的存储库安装它,那么构建该软件包的人在没有 gui 支持的情况下构建了它,您需要从另一个来源安装它。

于 2013-10-22T22:41:45.610 回答