Stop pylab overlay stories?

In my code, I try to periodically create a graph and save the graph to a file. The code is as follows:

import pylab as p

def simpledist(speclist,totalbugs,a):
    data = [float(spec.pop)/float(totalbugs) for spec in speclist]
    p.hist(data)
    p.savefig('/Home/s1215235/Documents/python/newfolder/' + str(a) + '.png')

( ais a counter)

However, this means that every new plot that is created continues to overlap with the plots before. How can I tell him that as soon as I save the shape, I want her to start a new shape?

+5
source share
3 answers

To clear the chart, use p.clf

def simpledist(speclist,totalbugs,a):
    data = [float(spec.pop)/float(totalbugs) for spec in speclist]
    p.clf()
    p.hist(data)
    p.savefig('/Home/s1215235/Documents/python/newfolder/' + str(a) + '.png')

Assuming it pis an instance matplotlib.pyplotor figure, also, what @bernie says, this will work too.

@Yann Comment

, .., . , ,

 p.gca().cla()

. !

+8

: danodonovan, , .


, p, , - :

import matplotlib.pyplot as plt
p = plt.figure()

. :

def simpledist(speclist,totalbugs,a):
    data = [float(spec.pop)/float(totalbugs) for spec in speclist]
    p = plt.figure() # let have a new figure why don't we 
    p.hist(data)
    p.savefig('/Home/s1215235/Documents/python/newfolder/' + str(a) + '.png')
+2

hold (doc)

import pylab as p
ax = p.gca()
ax.hold(False)

def simpledist(speclist,totalbugs,a):
    data = [float(spec.pop)/float(totalbugs) for spec in speclist]
    ax.hist(data)
    ax.figure.savefig('/Home/s1215235/Documents/python/newfolder/' + str(a) + '.png')

, .

, , remove .

import pylab as p
ax = p.gca()
# draw a bunch of stuff onto the axse

def simpledist(speclist,totalbugs,a):
    data = [float(spec.pop)/float(totalbugs) for spec in speclist]
    n, bins, h_art = ax.hist(data)
    ax.figure.savefig('/Home/s1215235/Documents/python/newfolder/' + str(a) + '.png')
    for ha in h_art:
        h_a.remove()
    # ax.figure.canvas.draw() # you might need this
+2

All Articles