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

  • Home
  • SEARCH
  • 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 9001535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:26:59+00:00 2026-06-16T00:26:59+00:00

I am trying to do a matplotlib bar chart on django. The data that

  • 0

I am trying to do a matplotlib bar chart on django. The data that will be used, will be queried from my database.

I came across this error when I run my code:

UnboundLocalError at /graph.png local variable ‘topic’ referenced
before assignment

The code for matplotlib part are all correct as I tried running static data before and it worked. However, when I tried quering data from database, it causes this error on views.py. I have done this query many times before on different pages and working fine. But it doesn’t work for this request. Why?

def bar_chart(request):
    #Topic Distribution
    topics = list(topic.objects.filter(subject_id=subj_id).order_by('id').values())

    fig = Figure()
    ax = fig.add_subplot(111)

    N = 5
    ind = np.arange(N)
    width = 0.35

    group_labels = [] #list of x-axis tick labels
    y = [] #list of y-values
    total_marks = 0 #total marks
    for topic in topics:
        t_questions = question.objects.filter(topic_id=topic.id)
        if (len(t_questions) != 0):
            topic_marks = 0 #each topic starts at 0 marks distribution
            for t_question in topic_questions:
                topic_marks += t_question.marks #accumulate the marks
            total_marks += topic_marks
            y.append(topic_marks)
            group_labels.append(topic.title)
    for yval in y:
        yval = yval/total_marks * 100 #convert to percentage


    ax.bar(ind, y, width, color='r')
    ax.set_ylabel('Distribution in %')
    ax.set_title('Topic Distribution')
    ax.set_xticks(ind)
    ax.set_xticklabels(group_labels)
    fig.autofmt_xdate()
    canvas=FigureCanvas(fig)
    response=HttpResponse(content_type='image/png')
    canvas.print_png(response)

    return response
  • 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-06-16T00:26:59+00:00Added an answer on June 16, 2026 at 12:26 am

    The error is here:

    topics = list(topic.objects.filter(subject_id=subj_id).order_by('id').values())
    


    If you are following the common naming conventions for models, yours is probably called Topic, so it should be

    topics = list(Topic.objects.filter(subject_id=subj_id).order_by('id').values())
    

    Otherwise you are likely missing an import.

    It’s caused by the local variable topic defined within the function and overshadowing the global one.

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

Sidebar

Related Questions

Hi I am trying to plot this using matplotlib's step function from matplotlib.pyplot import
I am trying to run demo code from Matplotlib: the legend_picking example . This
I am trying to make a matplotlib figure that will have multiple horizontal boxplots
I'm trying to get the binary data from a matplotlib canvas so I can
I'm trying to reproduce this graph in matplotlib (taken from wikipedia) basically a 2d
I'm trying to install matplotlib on my mac setup. I find that setup.py has
I'm trying to plot data in matplotlib. I would like to hide the upper
I am trying to plot two columns from a text file using python matplotlib
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
I am trying to adapt the underlying structure of plotting code (matplotlib) that is

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.