I would like to create a histogram and save it to a file without showing it on the screen. The piece of code I have now is showing the figure by default and I cannot find any way to suppress showing the figure. I have tried pyplot.hist(nrs) as well, with the same problem.
import math, time, matplotlib.pyplot as plt, pylab;
import numpy as np;
nrs = [1.0, 2.0, 1.0, 3.0, 4.0]
freq,bins = np.histogram(nrs)
fig = plt.figure(figsize=(5,4), dpi=100);
freq = np.append(freq, [0.0])
graph = fig.add_subplot(111);
x = graph.bar(bins, freq)
fig.savefig( "test.png")
plt.show()orfig.show()anywhere in your code?python script.py. This should work. The only way you could be getting a figure without callingplt.show()is with interactive mode in IPython.Agg). See stackoverflow.com/questions/2801882/… for an example (slightly different issue there, but same solution).plt.ioff()would turn autoploting off.