开发者

How to generate several matplotlib charts at a time?

开发者 https://www.devze.com 2023-01-25 07:47 出处:网络
I would like to generate several charts and save them as .png files. But it seems matplotlib is overlapping the next chart on the previous one :

I would like to generate several charts and save them as .png files. But it seems matplotlib is overlapping the next chart on the previous one :

def do_pie(fic,data):
    import pylab    

    # make a square figure and axes
    pylab.figure(1, figsize=(6,6))
    ax = pylab.axes([0.1, 0.1, 0.8, 0.8])

    pylab.pie(data,labels=data)
    pylab.title(fic, bbox={'facecolor':'0.8', 'pad':5})
    pylab.savefig('%s.png' % fic,dpi=100)

do_pie('tarte',[10,20,30])
do_pie('gateau',[33,44])

This script generate 2 PNG files. tarte.png is correct, but gateau.png is getting some informations about tarte.png like 10, 20 and 30 that should not be di开发者_如何学Gosplayed.

So how to start a new chart from scratch ?


Just close the figure object after you save it.

def do_pie(fic,data):
    import pylab    
    # make a square figure and axes
    pylab.figure(1, figsize=(6,6))
    ax = pylab.axes([0.1, 0.1, 0.8, 0.8])
    pylab.pie(data,labels=data)
    pylab.title(fic, bbox={'facecolor':'0.8', 'pad':5})
    pylab.savefig('%s.png' % fic,dpi=100)
    pylab.close()


For example, you can store figure instance in a dict and at the end of your program, output all figures:

figures = dict()

def do_pie(fic,data):
    import pylab    

    # make a square figure and axes
    figures[fic] = pylab.figure(figsize=(6,6))
    ax = pylab.axes([0.1, 0.1, 0.8, 0.8])

    pylab.pie(data,labels=data)
    pylab.title(fic, bbox={'facecolor':'0.8', 'pad':5})

do_pie('tarte',[10,20,30])
do_pie('gateau',[33,44])

for fig in figures:
    figures[fig].savefig('%s.png' % fic, dpi=100)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号