By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 1,987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

Simple graphics in python assignment

P: 4
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.


Expand|Select|Wrap|Line Numbers
  1. # Put your name and student ID here
  2. # Don't forget to rename this file!
  3. #
  4.  
  5. from graphics22 import *
  6.  
  7. #The dictionary has 19 entries.
  8. Stats = { "ECSE":8, "LCSI":6, "ECOM":13, "ABIT":4, "ACSM":3, "AEXH":2, \
  9.           "AEXP":9, "BULS":6, "APME":8, "BBMB":3, "EBCL":2, \
  10.       "EEEL":9, "LDES":4, "LMAT":3, "LPHY":3, "LPSC":4, "LUHU":4, \
  11.       "LUPS":5, "LUSS":2 }
  12.  
  13.  
  14. # You have to define the window, its size, and an 
  15. # appropriate coordinate system. The number of entries in the 
  16. # dictionary and the maximum count of students gives you an 
  17. # idea of the min and max x/y coordinates
  18. def main(n):
  19.     win = GraphWin("This is a 600 x 400 graph window",600,400)
  20.     win.setBackground('white')
  21.  
  22.     # Let's define lower left and upper right for the coord.system
  23.     x_min = -1.0      # make sure (0,0) is on the screen
  24.     y_min = -1.0      # make sure (0,0) is on the screen
  25.     x_max = n + 1.0   # give a little extra horizontal space
  26.     y_max = n * 1.1   # give 10% extra vertical space
  27.     win.setCoords(x_min, y_min, x_max, y_max)
  28.  
  29.     L = range(n)
  30.     shuffle(L)
  31.  
  32.  
  33. # Now, convert the dictionary to a list of pairs. That's easy...
  34. swapDictionary = {}
  35.  
  36. for key, val in Stats.items():
  37.     swapDictionary[val] = key
  38.  
  39.  
  40.  
  41. # Next, you have a loop that goes through that list, picks a pair
  42. # (major and count) and constructs a vertical line of this pair
  43. # at the correct position in the window. Note that the string (name
  44. # of the major) goes on top of the vertical line. All this is very
  45. # similar to the graph01.py program presented in class.
  46.  
  47.  
  48.  
  49. # wait for final mouse click!
  50. win.getMouse()
  51. win.close()
  52. print "Done"
  53.  
Mar 12 '08 #1
Share this Question
Share on Google+
7 Replies


jlm699
100+
P: 314
A few things:
you must import random and then use random.shuffle(L) instead of just shuffle.
Except... what is the purpose of doing that? It's not used anywhere else in the code...

also, the bottom of your code should be more like this :
Expand|Select|Wrap|Line Numbers
  1.     # wait for a  mouse click and the close the window
  2. if __name__ == '__main__':
  3.     # You can call main() here
  4.     # You need an instance of "win" to refer to here for things like:
  5.     win.getMouse()
  6.     # rest of your code...
  7.  
To get this to run I had to get a reference to "win" by having that main function return an instance of the win and assigning a variable to the main() call...

For the loop I'd suggest something simple like:
Expand|Select|Wrap|Line Numbers
  1. for elem in Stats:
  2.     print elem, Stats[elem]
  3.  
I hope this helps, please don't hesitate to ask further.
Mar 12 '08 #2

P: 4
Thanks for the quick response! Still a little confused how to actually get the program to draw the lines for each major and code. Something like:

Expand|Select|Wrap|Line Numbers
  1.     # Next, you have a loop that goes through that list, picks a pair
  2.     # (major and count) and constructs a vertical line of this pair
  3.     # at the correct position in the window. Note that the string (name
  4.     # of the major) goes on top of the vertical line. All this is very
  5.     # similar to the graph01.py program presented in class.
  6.     for elem in Stats:
  7.         print elem, Stats[elem]
  8.         t = Key(Point(x,y+1), str(y))
  9.         t.draw(win)
  10.         line = Line(Point(x,0), Point(x,y))
  11.         line.setFill("blue")
  12.         line.draw(win) 
Mar 12 '08 #3

jlm699
100+
P: 314
Hmm... you're gettin' there. I don't know where that Key() came from though?

I did it using a Rectangle() with a Text() instance at the top... although I don't know if that's how your professor wants you to do it or not.
Expand|Select|Wrap|Line Numbers
  1. r = Rect(Point(i, 0), Point(i+1, '''number of students'''))
  2. r.setFill(['red','blue','green'][i%3])
  3. r.draw(win)
  4. t = Text(Point(i + 0.5, '''number of students''' + 1))
  5. t.setSize(8)
  6. t.draw(win)
  7.  
Where i is the index of the for loop interation (HINT: look into the Python function enumerate() )
Mar 12 '08 #4

P: 4
Here is what I got. I feel i am pretty close. I just cant get the data from the dictionary to print on the graph. Instead, I get a graph with bars starting at 0 going to 19 with the numbers above each bar. I need the bars to vary according to the number of people in each major with the major code above each bar.

Expand|Select|Wrap|Line Numbers
  1. from random import shuffle
  2. from graphics22 import *
  3.  
  4. Stats = { "ECSE":8, "LCSI":6, "ECOM":13, "ABIT":4, "ACSM":3, "AEXH":2, \
  5.           "AEXP":9, "BULS":6, "APME":8, "BBMB":3, "EBCL":2, \
  6.       "EEEL":9, "LDES":4, "LMAT":3, "LPHY":3, "LPSC":4, "LUHU":4, \
  7.       "LUPS":5, "LUSS":2 }
  8.  
  9. def main(n):
  10.     win = GraphWin("This is a 600 x 400 graph window",600,400)
  11.     win.setBackground('white')
  12.  
  13.     # Let's define lower left and upper right for the coord.system
  14.     x_min = -1.0      # make sure (0,0) is on the screen
  15.     y_min = -1.0      # make sure (0,0) is on the screen
  16.     x_max = n + 1.0   # give a little extra horizontal space
  17.     y_max = n * 1.1   # give 10% extra vertical space
  18.  
  19.     L = range(n)
  20.  
  21.  
  22.     # after setCoords all coordinates will be transformed!
  23.     win.setCoords(x_min, y_min, x_max, y_max)
  24.  
  25.     swapDictionary = {}
  26.     for key, val in Stats.items():
  27.         swapDictionary[val] = key
  28.  
  29.     stats_dic = {"ECSE":8, "LCSI":6, "ECOM":13, "ABIT":4, "ACSM":3,
  30.                     "AEXH":2, "AEXP":9, "BULS":6, "APME":8, "BBMB":3,
  31.                     "EBCL":2, "EEEL":9, "LDES":4, "LMAT":3, "LPHY":3,
  32.                     "LPSC":4, "LUHU":4, "LUPS":5, "LUSS":2 }
  33.     for key, val in stats_dic.items():
  34.         print key, val
  35.  
  36.     for i in range(n):
  37.         x = i 
  38.         y = L[i]
  39.         t = Text(Point(x,y+1), str(y))
  40.         t.draw(win)
  41.         line = Line(Point(x,0), Point(x,y))
  42.         line.setFill("blue")
  43.         line.draw(win)
  44.  
  45.  
  46.  
  47.     # wait for final mouse click!
  48.     win.getMouse()
  49.     win.close()
  50.     print "Done"
  51.  
  52. main(20)
  53.  
Mar 12 '08 #5

jlm699
100+
P: 314
I get a graph with bars starting at 0 going to 19 with the numbers above each bar.

Expand|Select|Wrap|Line Numbers
  1.     L = range(n)
  2.  
  3.     for i in range(n):
  4.         x = i 
  5.         y = L[i]
  6.         t = Text(Point(x,y+1), str(y))
  7.         t.draw(win)
  8.         line = Line(Point(x,0), Point(x,y))
  9.         line.setFill("blue")
  10.         line.draw(win)
  11.  
You are very close, however you're doing something very wrong here. range(n) where n = 20 yields this: [0, 1, 2, 3, 4, 5, 6, ... 19].
Then in your for loop you're using these numbers instead of the contents of your Stats dictionary. I suggest getting rid of all references to range(n), and in your for loop just try the following:
Expand|Select|Wrap|Line Numbers
  1. for idx, elem in enumerate(stats_dict):
  2.         print idx, elem, stats_dict[elem]
  3.  
After you see the output of that loop you should understand what to fix in the for loop that you currently have.
Mar 13 '08 #6

P: 4
All done, except I need the major codes above each bar on the graph, not the number of students in each major. Guidance?
Expand|Select|Wrap|Line Numbers
  1. from random import shuffle
  2. from graphics22 import *
  3.  
  4. Stats = { "ECSE":8, "LCSI":6, "ECOM":13, "ABIT":4, "ACSM":3, "AEXH":2, \
  5.           "AEXP":9, "BULS":6, "APME":8, "BBMB":3, "EBCL":2, \
  6.       "EEEL":9, "LDES":4, "LMAT":3, "LPHY":3, "LPSC":4, "LUHU":4, \
  7.       "LUPS":5, "LUSS":2 }
  8.  
  9. def main(n):
  10.     win = GraphWin("This is a 600 x 400 graph window",600,400)
  11.     win.setBackground('white')
  12.  
  13.     # Let's define lower left and upper right for the coord.system
  14.     x_min = -1.0      # make sure (0,0) is on the screen
  15.     y_min = -1.0      # make sure (0,0) is on the screen
  16.     x_max = n + 1.0   # give a little extra horizontal space
  17.     y_max = n * 1.1   # give 10% extra vertical space
  18.  
  19.  
  20.  
  21.     # after setCoords all coordinates will be transformed!
  22.     win.setCoords(x_min, y_min, x_max, y_max)
  23.  
  24.     swapDictionary = {}
  25.     for key, val in Stats.items():
  26.         swapDictionary[val] = key
  27.  
  28.     stats_dic = {"ECSE":8, "LCSI":6, "ECOM":13, "ABIT":4, "ACSM":3,
  29.                     "AEXH":2, "AEXP":9, "BULS":6, "APME":8, "BBMB":3,
  30.                     "EBCL":2, "EEEL":9, "LDES":4, "LMAT":3, "LPHY":3,
  31.                     "LPSC":4, "LUHU":4, "LUPS":5, "LUSS":2 }
  32.  
  33.     L = stats_dic
  34.     for idx, elem in enumerate(stats_dic):
  35.         x = idx
  36.         y = L[elem]
  37.         t = Text(Point(x,y+1), str(y))
  38.         t.draw(win)
  39.         line = Line(Point(x,0), Point(x,y))
  40.         line.setFill("blue")
  41.         line.draw(win)       
  42.  
  43.  
  44.  
  45.     # wait for final mouse click!
  46.     win.getMouse()
  47.     win.close()
  48.     print "Done"
  49.  
  50. main(19)
Mar 13 '08 #7

jlm699
100+
P: 314
Expand|Select|Wrap|Line Numbers
  1.     stats_dic = {"ECSE":8, "LCSI":6, "ECOM":13, "ABIT":4, "ACSM":3,
  2.                     "AEXH":2, "AEXP":9, "BULS":6, "APME":8, "BBMB":3,
  3.                     "EBCL":2, "EEEL":9, "LDES":4, "LMAT":3, "LPHY":3,
  4.                     "LPSC":4, "LUHU":4, "LUPS":5, "LUSS":2 }
  5.  
  6.     L = stats_dic
  7.     for idx, elem in enumerate(stats_dic):
  8.         x = idx
  9.         y = L[elem]
  10.         t = Text(Point(x,y+1), str(y))
  11.         t.draw(win)
  12.  
This is simple. Look at what you are assigning to the variable y, and then think about how you reference an element in a dictionary. You have y being the associated value of the dictionary element, which is what is being printed (the class size); however you want the key instead of the element (the class abbreviation). So simply swap str(y) (the value) for the key (what is used to access the value from the dictionary).
Mar 14 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.