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

查看:21
本文介绍了调用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 问题,您可以通过使用不向用户显示的后端来解决这个问题,例如'阿格':

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

如果您不想失去显示绘图的能力,请关闭 交互模式,并且只有在您准备好显示绘图时才调用 plt.show() :

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天全站免登陆