473,474 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Simple graphics in python assignment

4 New Member
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
7 3299
jlm699
314 Contributor
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
isinc
4 New Member
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
314 Contributor
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
isinc
4 New Member
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
314 Contributor
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
isinc
4 New Member
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
314 Contributor
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

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

Similar topics

5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
13
by: Jason Swett | last post by:
I want to do graphics with C++. Surprisingly, so far nobody has been able to tell me anything helpful. How do I do it? Any input would be greatly appreciated. Jason
12
by: Dr. Zharkov | last post by:
Hello. Inform, please, on what site it is possible to find materials on construction of the three-dimensional graphic of function z=f(x,y) with the help of Visual Basic .NET and GDI+? Beforehand...
7
by: bfowlkes | last post by:
Hi, I am new to the C programming language and programming in general. I am writing a simple roulette program. I have everything working so far but it seems a little plain. What I would like to...
10
theNewb
by: theNewb | last post by:
Two questions on graphics: 1. I'm trying to create the rooftop for a 'house' which needs to be 61 pixels long. In order to center the roof properly, I need to have both base points at 30.5 pixels...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 349 open ( +7) / 3737 closed (+25) / 4086 total (+32) Bugs : 939 open (-12) / 6648 closed (+60) / 7587 total (+48) RFE : 249 open...
7
by: Dustin MacDonald | last post by:
Hi everyone. This is my first time posting to this newsgroup, and although I maintain my netiquette I might've missed something specific to the newsgroup, so hopefully you can avoid flaming me...
41
by: none | last post by:
Hello, IIRC, I once saw an explanation how Python doesn't have "variables" in the sense that, say, C does, and instead has bindings from names to objects. Does anyone have a link? Thanks, ...
13
by: John Dann | last post by:
A Python newbie, but some basic understanding of how classes, objects etc work in eg VB.Net. However, I'm struggling a little to translate this knowledge into the Python context. I'm trying to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.