CS 100

Logo

Scientific Computing

Activity 21

Create a new file called plotFrequency.py and save to your cs100/ch4 folder.

  1. Copy and paste the following code into the file.
    import turtle
    import math
       
    #
    def mean(alist):
        mean = sum(alist) / len(alist)
        return mean
       
       
    #
    def frequencyChart(alist):
           
        #
        countdict = {}      
       
        # 
        for item in alist:
            if item in countdict:
                countdict[item] = countdict[item]+1
            else:
                countdict[item] = 1         
           
        # 
        itemlist = list(countdict.keys())
        minitem = 0
        maxitem = len(itemlist)-1
           
        #
        countlist = countdict.values()
        maxcount = max(countlist)
           
        #
        #
        wn = turtle.Screen()
        chartT = turtle.Turtle()
        wn.setworldcoordinates(-1,-1,maxitem+1,maxcount+1)
        chartT.hideturtle()
           
        #
        chartT.up()
        chartT.goto(0,0)
        chartT.down()
        chartT.goto(maxitem,0)
        chartT.up()
           
        # 
        chartT.goto(-1,0)
        chartT.write("0",font=("Helvetica",16,"bold"))
        chartT.goto(-1,maxcount)
        chartT.write(str(maxcount),font=("Helvetica",16,"bold"))
           
        #
        for index in range(len(itemlist)):
            print("Index: ", index)
            chartT.goto(index,-1)
            chartT.write(str(itemlist[index]),font=("Helvetica",16,"bold"))
               
            chartT.goto(index,0)
            chartT.down()
            chartT.goto(index,countdict[itemlist[index]])
            chartT.up()
                       
        # add code to plot mean of occurrences in red
       
        wn.exitonclick()
       
       
    #lst = [7, 11, 9, 18,15, 12]
    lst = [3,3,5,7,1,2,5,2,3,4,6,3,4,6,3,4,5,6,6]
    #lst = [2,3,4,2,3,3]
    #lst = ['a', 'b', 'c', 'd', 'f', 'c', 'b', 'c', 'a', 'd', 'a', 'b', 'b', 'c', 'b', 'a', 'c', 'd']
       
    #
    frequencyChart(lst)
    
  2. Run the program with the following lists (uncomment out / comment out the assignment statements to lst in your file).
    lst = [7,11,9,18,15,12]
    lst = [17,11,28,17,11,17]
    lst = [3,3,5,7,1,2,5,2,3,4,6,3,4,6,3,4,5,6,6]
    lst = ['a', 'b', 'c', 'd', 'f', 'c', 'b', 'c', 'a', 'd', 'a', 'b', 'b', 'c', 'b', 'a', 'c', 'd']
    
  3. Read and understand the code. Write short comments above each section of the program (on the lines marked by #). Use your textbook as well as the online Python resource.

  4. Modify the frequency chart function to draw the mean (mean number of occurrences) as a horizontal red line.

If you finish early

Modify the frequency chart function to:

  1. draw wide bars instead of lines.
  2. have randomly colored bars.

How to submit

Submit your working python file plotFrequency.py to Moodle.