我正在尝试保存一个在 IPython 内联中运行良好但不将包含轴和标题的图形保存到磁盘的图形。
我在 matplotlibrc 中默认使用 TKAgg 后端。
任何想法这里可能出了什么问题?我已经清楚地设置了 xlabel 并且刻度线在 IPython 内联图中可以正常工作。
import matplotlib.pylab as plt
x = [1,2,3,3]
y = map(lambda(x): x * 2, x)
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.set_title("bleh")
ax.set_xlabel("xlabel")
ax.plot(x, y, 'r--')
fig.savefig("fig.png")
原文由 Richard Todd 发布,翻译遵循 CC BY-SA 4.0 许可协议
我在使用 Jupyter notebook 和命令时遇到了同样的问题:%matplotlib notebook。该图在笔记本中正确显示,但在使用 fig.savefig() 保存时没有打印轴和标题。我将 %matplotlib notebook 更改为 %matplotlib inline 并解决了问题。