bobhig

Bar Chart Plotting

Feb 8th, 2019
99
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #count frequency distribution in a range of scores
  2.  
  3. import random
  4. import matplotlib.pyplot as plot
  5.  
  6. # count number of specified items in a list
  7.  
  8. def count(value, data):
  9.     total = 0
  10.     for item in data:
  11.         if item==value:
  12.             total += 1
  13.     return total
  14.  
  15. scores = []
  16. score_frequency = []
  17.  
  18. # seed the scores list
  19. for x in range (0, 30):
  20.   scores.append(random.randint(0, 10))
  21.  
  22. # now count frequency of each score and append to frequency list
  23.  
  24. for x in range (0, 11):
  25.     score_frequency.append(count(x,scores))
  26.  
  27. # plot bar chart
  28.  
  29. plot.bar(range(11), score_frequency, align='center', alpha=0.5)
  30.  
  31. plot.xticks(range(11))
  32. plot.ylabel('Score frequency')
  33. plot.title('Scores on a quiz')
  34. plot.savefig('Quiz_Chart.png')
  35. plot.show()
RAW Paste Data