我在 IPython 中有以下代码,在 Linux 上运行 IPython QT 控制台。
%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()
将后端手动更改为 Qt4Agg
, GTK
等没有运气。即使在我调用 display()
时,这也会发生在 IPython notebook
中。
有什么想法可以使内联绘图正常工作吗?
将 Jakob 的回答标记为答案,但实际上两者都是正确的。我必须用新副本替换 matploblibrc 文件,使用 --pylab=None 启动 IPython QT 控制台,然后在控制台中手动输入 %pylab inline。这以某种方式解决了问题。
最佳答案
轴对象定义不正确,导致 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) 调用。
关于matplotlib - IPython 绘图内联不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19528486/