Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6388467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:16:56+00:00 2026-05-25T03:16:56+00:00

I am new new in Python/Django and I try to create a website which

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:16:57+00:00Added an answer on May 25, 2026 at 3:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to both Django and Python. This is my first time using
I'm really new to Python and Django. I created a class in Python that
I am new on Django and Python. I want to write a program with
When i try to create a new project in Timepiece the project is added
New to python and django .i want to understand the code of django-registration module
I am new to python and django and i have a question regarding the
I am pretty new to Django and trying to use django-social-auth in my Django
I am writing a python website built on the back of the django framework,
Python 2.7.1 / Django 1.3 I am new to Django Templates and trying to
As a new python and django developer, I'm creating a Django app on Google

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.