I have an assignment that I'm working on and am having trouble. Not too familiar with graphics. Any help/guidance would be much appreciated to see if what I have so far is okay and what I should do for the loop part. I will post assignment details and what i have so far.

You are given a dictionary named Stats, which is part of the template provided for this assignment. The template can be found at
http://dbis.ucdavis.edu/courses/ECS10-WQ08/Python/hw6-template.py
The dictionary manages information about the number of students of a particular major enrolled in a class. The dictionary looks as follows:
Stats = { "ECSE":8, "LCSI":6, "ECOM":13, "ABIT":4, "ACSM":3, "AEXH":2, \
"AEXP":9, "BULS":6, "APME":8, "BBMB":3, "EBCL":2, \
"EEEL":9, "LDES":4, "LMAT":3, "LPHY":3, "LPSC":4, "LUHU":4, \
"LUPS":5, "LUSS":2 }
Each pair consists of a string (major code) and a number, which tells you how many students of that major are enrolled in the class. For example, there are 8 students with the major ECSE and 4 students with the major LPSC.
Write a Python program that displays the information contained in the dictionary in the form of a chart analog to the one produced by the program graph01.py presented in class on 3/4/08. Your program should construct a chart like a bar graph, with major name above each bar on the graph.

Approach: Download the following two files in your ECS 10 folder: graphics22.py and hw6-template.py. Both files can be obtained from the assignment Web page. The template provides you with some kind of structure for the program. Take a look at the template! Next, you have to determine an appropriate size and coordinates for your window. For this, you can use the information about how many entries the dictionary has. Once this is in place, you have to
retrieve pairs from the dictionary, pair-by-pair. Note that you can’t do this directly on the dictionary, but you have to convert it to a list of pairs. Then, for each pair, you get the major code and count, print the major code (as string) and the count is used to determine the height of the line.

from random import shuffle
from graphics22 import *


def main(n):
    win = GraphWin("This is a 600 x 400 graph window",600,400)
    win.setBackground('white')

    # Let's define lower left and upper right for the coord.system
    x_min = -1.0      # make sure (0,0) is on the screen
    y_min = -1.0      # make sure (0,0) is on the screen
    x_max = n + 1.0   # give a little extra horizontal space
    y_max = n * 1.1   # give 10% extra vertical space

    # after setCoords all coordinates will be transformed!
    win.setCoords(x_min, y_min, x_max, y_max)

    swapDictionary = {}
    for key, val in Stats.items():
    	swapDictionary[val] = key
    
    for i in range(n):
        x = i 
        y = [i]
        t = Text(Point(x,y+1), str(y))
        t.draw(win)
        line = Line(Point(x,0), Point(x,y))
        line.setFill("blue")
        line.draw(win)
                
    # wait for final mouse click!
    win.getMouse()
    win.close()
    print "Done"

main(20)

I am having trouble with the loop which I believe will actually draw the info from the dictionary to the graph. Something like:

for elem in Stats:
    	print elem, Stats[elem]
        x = i 
        y = L[i]
        t = Text(Point(x,y+1), str(y))
        t.draw(win)
        line = Line(Point(x,0), Point(x,y))
        line.setFill("blue")
        line.draw(win)

This draws a graph, but it looks wrong. The graph starts from 0 and goes to 19, with the number above it. I need the graph to vary in height by how many students are in each major, with the major code above the bar, rather than the number.

To retrieve key and val from the key:val pairs of a dictionary use this ...

stats_dic = {
"ECSE":8, "LCSI":6, "ECOM":13, "ABIT":4, "ACSM":3,
"AEXH":2, "AEXP":9, "BULS":6, "APME":8, "BBMB":3,
"EBCL":2, "EEEL":9, "LDES":4, "LMAT":3, "LPHY":3,
"LPSC":4, "LUHU":4, "LUPS":5, "LUSS":2 }

for key, val in stats_dic.items():
    # test it ...
    print key, val

So close. Can get parts of graph to draw sometimes, but missing something critical.

for idx, elem in enumerate(stats_dic):
            print idx, elem, stats_dic[elem]
    
    L = stats_dic[elem]
    for i in stats_dic:
        x = elem
        y = L[elem]
        t = Text(Point(x,y+1), str(y))
        t.draw(win)
        line = Line(Point(x,0), Point(x,y))
        line.setFill("blue")
        line.draw(win)

Figured it out, put need to have the major code above each bar, not how many students in the major. If I fix that, i'm done.

from random import shuffle
from graphics22 import *

Stats = { "ECSE":8, "LCSI":6, "ECOM":13, "ABIT":4, "ACSM":3, "AEXH":2, \
          "AEXP":9, "BULS":6, "APME":8, "BBMB":3, "EBCL":2, \
	  "EEEL":9, "LDES":4, "LMAT":3, "LPHY":3, "LPSC":4, "LUHU":4, \
	  "LUPS":5, "LUSS":2 }

def main(n):
    win = GraphWin("This is a 600 x 400 graph window",600,400)
    win.setBackground('white')

    # Let's define lower left and upper right for the coord.system
    x_min = -1.0      # make sure (0,0) is on the screen
    y_min = -1.0      # make sure (0,0) is on the screen
    x_max = n + 1.0   # give a little extra horizontal space
    y_max = n * 1.1   # give 10% extra vertical space

    L = range(n)
    
    
    # after setCoords all coordinates will be transformed!
    win.setCoords(x_min, y_min, x_max, y_max)

    swapDictionary = {}
    for key, val in Stats.items():
    	swapDictionary[val] = key
    
    for i in range(n):
        x = i 
        y = L[i]
        t = Text(Point(x,y+1), str(y))
        t.draw(win)
        line = Line(Point(x,0), Point(x,y))
        line.setFill("blue")
        line.draw(win)
                
    # wait for final mouse click!
    win.getMouse()
    win.close()
    print "Done"

main(20)

oops. wrong code. here is the updated code.

from random import shuffle
from graphics22 import *

Stats = { "ECSE":8, "LCSI":6, "ECOM":13, "ABIT":4, "ACSM":3, "AEXH":2, \
          "AEXP":9, "BULS":6, "APME":8, "BBMB":3, "EBCL":2, \
	  "EEEL":9, "LDES":4, "LMAT":3, "LPHY":3, "LPSC":4, "LUHU":4, \
	  "LUPS":5, "LUSS":2 }

def main(n):
    win = GraphWin("This is a 600 x 400 graph window",600,400)
    win.setBackground('white')

    # Let's define lower left and upper right for the coord.system
    x_min = -1.0      # make sure (0,0) is on the screen
    y_min = -1.0      # make sure (0,0) is on the screen
    x_max = n + 1.0   # give a little extra horizontal space
    y_max = n * 1.1   # give 10% extra vertical space

    
    
    # after setCoords all coordinates will be transformed!
    win.setCoords(x_min, y_min, x_max, y_max)

    swapDictionary = {}
    for key, val in Stats.items():
    	swapDictionary[val] = key
    
    stats_dic = {"ECSE":8, "LCSI":6, "ECOM":13, "ABIT":4, "ACSM":3,
                    "AEXH":2, "AEXP":9, "BULS":6, "APME":8, "BBMB":3,
                    "EBCL":2, "EEEL":9, "LDES":4, "LMAT":3, "LPHY":3,
                    "LPSC":4, "LUHU":4, "LUPS":5, "LUSS":2 }
   
    L = stats_dic
    for idx, elem in enumerate(stats_dic):
        x = idx
        y = L[elem]
        t = Text(Point(x,y+1), str(y))
        t.draw(win)
        line = Line(Point(x,0), Point(x,y))
        line.setFill("blue")
        line.draw(win)       
        
    
            
    # wait for final mouse click!
    win.getMouse()
    win.close()
    print "Done"

main(19)

Sorry about all the posts

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.