2

I am new new in Python/Django and I try to create a website which generates plots. To generate plots I use matplotlib.

When I run the page first time everything is ok, but after refresh in result I get wrong plot (composed of new and old plot).

Here the code:

    from pylab import *

    ...


    #prepare values and labels
    values = []
    labels = []
    values.append(float(user.playcount)/sum*100)
    labels.append(user.name)

    for friend in friends:
        friend = friend.getInfo()
        values.append(float(friend.playcount)/sum*100)
        labels.append(friend.name)

    # create figure
    figure(1)

    # set some parameters
    params = { 'axes.labelsize': 6, 'text.fontsize': 6, 'font.size': 6, 'legend.fontsize': 6, 'xtick.labelsize': 6, 'ytick.labelsize': 6,}        
    rcParams.update(params)

    # draw, add legend and save
    pie(values, labels=labels, shadow=False)
    l = legend(loc='lower center', ncol= 5, bbox_to_anchor=(0.5, -0.25))
    l.get_frame().set_alpha(0.0)
    savefig('media/images/3.png', dpi=100, transparent=True)

    #close(1)

If I uncomment close(1) instruction there will problem with refresh page because application will get stuck on figure() instruction.

Could anyone tell me where is the problem? I think there is something wrong with figure disposing but I don't have idea what exactly.

Yann
  • 33,811
  • 9
  • 79
  • 70
mchrobok
  • 1,947
  • 2
  • 20
  • 22

2 Answers2

4

I think you would benefit from using the cla() axes method, which clears the axes. You can do this at the top of your script before anything is done; even before the first plot. I would use this instead of close(), because then the second time around the figure will not exist. So I would replace the figure(1) line with

figure(1)
figure(1).gca().cla()

This sets the figure to figure 1, and then gets the current axes (gca()) and then clears it with cla(). Here's a script I ran independantly of django, that worked for me and emulates what I think you are trying to do:

from pylab import *

#prepare values and labels
values = [34,17,29,6,14]
labels = ["john","jane","jim","jason","judy"]

# create figure
figure(1)
figure(1).gca().cla()

# set some parameters
params = { 'axes.labelsize': 6, 'text.fontsize': 6, 'font.size': 6, 
           'legend.fontsize': 6, 'xtick.labelsize': 6, 
           'ytick.labelsize': 6,}        
rcParams.update(params)

# draw, add legend and save
pie(values, labels=labels, shadow=False)
l = legend(loc='lower center', ncol= 5, bbox_to_anchor=(0.5, -0.25))
l.get_frame().set_alpha(0.0)
savefig('3.png', dpi=100, transparent=True)


#close(1)

values2 = [24,27,29,16,4]
labels2 = ["dave","donna","derrick","dotty","drew"]

figure(1)
figure(1).gca().cla()

# draw, add legend and save
pie(values2, labels=labels2, shadow=False)
l = legend(loc='lower center', ncol= 5, bbox_to_anchor=(0.5, -0.25))
l.get_frame().set_alpha(0.0)
savefig('4.png', dpi=100, transparent=True)

#show()

You probably can get away with only one line; just having this worked for me:

figure(1).gca().cla()

But it's a bit clearer perhaps the other way.

Yann
  • 33,811
  • 9
  • 79
  • 70
  • Thanks, I use your solution and it works but I also add some code to clear figure clf() and legend. – mchrobok Aug 27 '11 at 13:24
0

According to Cristian Ciupitu's answer to this similar question, use figure() instead of figure(1) should solve the problem.

Community
  • 1
  • 1
herrlich10
  • 6,212
  • 5
  • 31
  • 35
  • 1
    Currently, this answer is listed as low quality. Try to put a little bit explanation why this approach works. – Andrew T. Aug 08 '14 at 07:00