I currently have code like the following:
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)
Unless you are using
ipython --pylab, the figure should only appear on screen if you do ashow()ordraw(). If you don’t want it to be shown on the screen, just make sure you are not doing any of those calls.Alternatively, you can use a non-interactive backend in matplotlib. For example, the
Aggbackend. Just make sure you have the following set in your~/.matplotlib/matplotlibrcfile: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--pylabwith a specific backend. For example: