简体   繁体   English

如何从IPython Notebook将图形导出到文件

[英]How to export figures to files from IPython Notebook

I use the IPython Notebook with the --pylab inline option, since I don't want plots to show up in a different window. 我将IPython Notebook与--pylab inline选项一起使用,因为我不希望将图显示在其他窗口中。 Now I'd like to save the plots I see in the notebook to PDF or PNG files. 现在,我想将在笔记本中看到的图保存为PDF或PNG文件。

Some code examples use 一些代码示例使用

import matplotlib as plt

plt.savefig("figure.png") # save as png

but this does not seem to work in inline mode. 但这似乎在串联模式下不起作用。

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. 当然,我可以简单地保存从浏览器中生成的PNG,但是我想使用一行Python来做到这一点。 I am also interested in PDF export. 我也对PDF导出感兴趣。

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')

if you want png just change it to 'sample.png' . 如果要png只需将其更改为'sample.png'

Note that the savefig() call should be in the same notebook cell as the plotting commands. 请注意, savefig()调用应与绘图命令位于同一笔记本单元中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM