Mar*_*sse 24 python matplotlib ipython-notebook
我试图将Matplotlib图形保存为iPython笔记本中的文件.
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_axes([1,1,1,1])
ax.plot([1,2])
fig.savefig('test.png')
Run Code Online (Sandbox Code Playgroud)
iPython笔记本中的内嵌视图看起来不错:

文件'test.png'几乎是空的.看起来情节向右上方移动,您可以在角落看到刻度标签'1.0'和'0.0'.

如何从iPython笔记本生成一个看起来像内联视图的文件?
Mar*_*sse 21
问题解决了:'bbox_inches='tight'为savefig 添加参数.
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_axes([1,1,1,1])
plt.plot([1,2])
savefig('test.png', bbox_inches='tight')
Run Code Online (Sandbox Code Playgroud)
我不明白这里发生了什么,但文件现在看起来像iPython笔记本内联文件.好极了.
实际上,savefig它正常工作.当你打电话时add_axes,你的列表指定一个矩形:[left, bottom, width, height].由于图形在两个轴上从0到1,原点位于左下角,因此从图的最右上角开始制作宽度和高度为1的矩形.你可能想做ax = fig.add_axes([0,0,1,1]).
我不确定为什么内联图不会尊重你放置轴的位置.如果我不得不猜测,我会说内联后端自动计算出边界框,并根据它放置内联图.
| 归档时间: |
|
| 查看次数: |
36371 次 |
| 最近记录: |