调用pylab.savefig在ipython中不显示 [英] Calling pylab.savefig without display in ipython

查看:84
本文介绍了调用pylab.savefig在ipython中不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在文件中创建图形,而不在IPython笔记本中显示该图形.我不清楚在这方面IPythonmatplotlib.pylab之间的交互.但是,当我调用pylab.savefig("test.png")时,除了保存在test.png中之外,还显示了当前图形.当自动创建大量绘图文件时,这通常是不希望的.或者在需要一个中间文件供其他应用进行外部处理的情况下.

I need to create a figure in a file without displaying it within IPython notebook. I am not clear on the interaction between IPython and matplotlib.pylab in this regard. But, when I call pylab.savefig("test.png") the current figure get's displayed in addition to being saved in test.png. When automating the creation of a large set of plot files, this is often undesirable. Or in the situation that an intermediate file for external processing by another app is desired.

不确定这是笔记本电脑中的matplotlib还是IPython问题.

Not sure if this is a matplotlib or IPython notebook question.

推荐答案

这是一个matplotlib问题,您可以使用不向用户显示的后端来解决此问题,例如'Agg':

This is a matplotlib question, and you can get around this by using a backend that doesn't display to the user, e.g. 'Agg':

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

plt.plot([1,2,3])
plt.savefig('/tmp/test.png')

编辑:如果您不想失去显示图的功能,请关闭

If you don't want to lose the ability to display plots, turn off Interactive Mode, and only call plt.show() when you are ready to display the plots:

import matplotlib.pyplot as plt

# Turn interactive plotting off
plt.ioff()

# Create a new figure, plot into it, then close it so it never gets displayed
fig = plt.figure()
plt.plot([1,2,3])
plt.savefig('/tmp/test0.png')
plt.close(fig)

# Create a new figure, plot into it, then don't close it so it does get displayed
plt.figure()
plt.plot([1,3,2])
plt.savefig('/tmp/test1.png')

# Display all "open" (non-closed) figures
plt.show()

这篇关于调用pylab.savefig在ipython中不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆