问题描述
我使用带有 --pylab inline
选项的 IPython Notebook,因为我不希望绘图显示在不同的窗口中.现在我想将我在笔记本中看到的图保存为 PDF 或 PNG 文件.
I use the IPython Notebook with the --pylab inline
option, since I don't want plots to show up in a different window. Now I'd like to save the plots I see in the notebook to PDF or PNG files.
一些代码示例使用
import matplotlib as plt
plt.savefig("figure.png") # save as png
但这似乎不适用于内联模式.
but this does not seem to work in inline mode.
当然,我可以简单地保存从浏览器生成的 PNG,但我想用一行 Python 来做到这一点.我也对 PDF 导出感兴趣.
Of course I could simply save the PNG that is generated out of the browser, but I'd like to do this with a line of Python. I am also interested in PDF export.
推荐答案
试试这个(注意文件被保存到默认的笔记本文件夹):
try this (note that the files get saved to the default notebook folder):
plot(range(80))
xlabel('foo')
ylabel('bar')
legend(['myline'])
axis([0, 80, 0, 120])
savefig('sample.pdf')
如果你想要 png
只需将其更改为 'sample.png'
.
if you want png
just change it to 'sample.png'
.
请注意,savefig()
调用应该与绘图命令位于相同的笔记本单元中.
Note that the savefig()
call should be in the same notebook cell as the plotting commands.
这篇关于如何将数字从 IPython Notebook 导出到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!