如何从IPython Notebook将图形导出到文件 [英] How to export figures to files from IPython Notebook
问题描述
我将IPython Notebook与--pylab inline
选项一起使用,因为我不希望将图显示在其他窗口中.现在,我想将在笔记本中看到的图保存为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将图形导出到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!