在IPython Notebook中,matplotlib中的subplots_adjust无法正常工作

4
我有下面这段代码,不起作用:

我有以下代码没有起作用:

import matplotlib.pyplot as plt

# Make the plot
fig, axs = plt.subplots(3, 1, figsize=(3.27, 6))
axs[0].plot(range(5), range(5), label='label 1')
axs[0].plot(range(5), range(4, -1, -1), label='label 2')
axs[0].legend(bbox_to_anchor=(0, 1.1, 1., 0.1), mode='expand', ncol=2, frameon=True, borderaxespad=0.)

# Adjust subplots to make room
fig.subplots_adjust(top=.5)

fig.savefig('test.png', format='png', dpi=300)

可以看到,fig.subplots_adjust根本不起作用。 我正在使用WinPython 3.3.2.3 64位版本,matplotlib版本为1.3.0和CPython 3.3。 这在IPython Notebook中发生。 后端是内联的。 笔记本电脑的输出完整,但输出文件未正确裁剪。 在笔记本和保存的文件中,subplots_adjust命令没有效果。

这是你在笔记本上看到的内容,还是它保存的内容? - tacaswell
@tcaswell 这就是它保存的内容。我在笔记本中看到的很正常 - 我可以看到整个图,但是subplots_adjust根本没有起作用。感谢您提出一个好问题! - Yuxiang Wang
@tcaswell 这是内联的。matplotlib.get_backend() 返回:'module://IPython.kernel.zmq.pylab.backend_inline' - Yuxiang Wang
@tcaswell 非常感谢!我已经相应地编辑了帖子。问题是 - 我试图指定matplotlib.use('Qt4Agg'),即使它是不同的后端,问题仍然存在。 - Yuxiang Wang
你确定在导入pyplot之前调用了use吗? - tacaswell
显示剩余7条评论
1个回答

5

在tcaswell的帮助下,我通过完全关闭IPython Notebook并通过ipython-qtconsole重新运行代码解决了问题。似乎subplots_adjust()在ipython-notebook中的python 3上根本不起作用。我是Python的新手,非常想知道qtconsole和notebook之间在后台方面有什么区别,如果有人有想法的话。

无论如何 - 很高兴解决了这个问题!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接