python - Save plot to image file instead of displaying it using Matplotlib -


i writing quick-and-dirty script generate plots on fly. using code below (from matplotlib documentation) starting point:

from pylab import figure, axes, pie, title, show  # make square figure , axes figure(1, figsize=(6, 6)) ax = axes([0.1, 0.1, 0.8, 0.8])  labels = 'frogs', 'hogs', 'dogs', 'logs' fracs = [15, 30, 45, 10]  explode = (0, 0.05, 0, 0) pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=true) title('raining hogs , dogs', bbox={'facecolor': '0.8', 'pad': 5})  show()  # actually, don't show, save foo.png 

i don't want display plot on gui, instead, want save plot file (say foo.png), that, example, can used in batch scripts. how do that?

while question has been answered, i'd add useful tips when using savefig. file format can specified extension:

savefig('foo.png') savefig('foo.pdf') 

will give rasterized or vectorized output respectively, both useful. in addition, you'll find pylab leaves generous, undesirable, whitespace around image. remove with:

savefig('foo.png', bbox_inches='tight') 

Comments

Popular posts from this blog

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -

objective c - Language Translation API for iPhone -

jquery - ajax and php updating mysql -