直接将图形保存到磁盘,而无需先在屏幕上渲染 [英] Directly saving a figure to disk without rendering first on the screen

查看:135
本文介绍了直接将图形保存到磁盘,而无需先在屏幕上渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有如下代码:

import os
import numpy as np
import pylab 

import matplotlib.pyplot as plt
import matplotlib.cm as cm    
from matplotlib.patches import Polygon  
import numpy as np

...

# Read my image
img = matplotlib.image.imread(p_image)

# Render it, move the coordinates' origin to the upper left corner
plt.imshow(np.flipud(img), cmap=cm.Greys_r,origin='upper')

# Overlay a polygon
p = Polygon( zip(xs,ys), alpha=0.2)
plt.gca().add_artist(p)

# Save it to disk
plt.savefig(p_image_output)

如何直接将此图保存到磁盘而不先在屏幕上呈现它?(请注意,我希望该图保留传递给)

How can I directly save this figure to disk without rendering it first on the screen? (notice that I would like the figure to keep the properties specified in the three arguments that I pass to imshow)

推荐答案

除非使用ipython --pylab,否则只有在执行show()draw()时,图形才应显示在屏幕上.如果您不希望它显示在屏幕上,只需确保您没有进行任何通话即可.

Unless you are using ipython --pylab, the figure should only appear on screen if you do a show() or draw(). If you don't want it to be shown on the screen, just make sure you are not doing any of those calls.

或者,您可以在matplotlib中使用非交互式后端.例如,Agg后端.只需确保在~/.matplotlib/matplotlibrc文件中设置了以下内容:

Alternatively, you can use a non-interactive backend in matplotlib. For example, the Agg backend. Just make sure you have the following set in your ~/.matplotlib/matplotlibrc file:

backend      : Agg

请记住,使用此后端,您将永远不会在屏幕上看到任何东西.如果使用ipython,则可以通过使用特定后端调用--pylab来保留配置文件并具有交互式后端.例如:

Keep in mind that using this backend you'll never see anything on screen. If you use ipython, you can keep the configuration file and have an interactive backend by calling --pylab with a specific backend. For example:

ipython --pylab=qt

这篇关于直接将图形保存到磁盘,而无需先在屏幕上渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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