Advertisement
Antypas

Bar chart and arithmetic mean

Apr 17th, 2020
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. # Bar Chart & Arith Mean using frequencies
  2.  
  3. import matplotlib.pyplot as plot
  4. import random  #just to generate data for testing
  5.  
  6. # Add your count function here
  7. def count(target_grade,scores_list):
  8.     target_hits = 0
  9.     for score in scores_list:
  10.         if score == target_grade:
  11.             target_hits += 1
  12.     return target_hits
  13.  
  14. # Preparing raw data for bar chart: occurrence counts
  15. def bar_chart_counts(scores):
  16.     scores_bar = []
  17.     for x in range(11):
  18.         scores_bar.append(count(x, scores))
  19.     print("Bar chart data: ", scores_bar)
  20.     sum = 0
  21.     for index in range(len(scores_bar)):
  22.         sum += scores_bar[index]
  23.     if sum == len(scores):
  24.         print("total number of data points in bar chart is: ", sum)
  25.         return scores_bar
  26.     else:
  27.         print("Warning: there is something wrong here.")
  28.  
  29. # Produce a bar chart from raw data, original scores
  30. def construct_bar_chart(scores):
  31.     scores_bar = bar_chart_counts(scores) #generate occurrence count
  32.  
  33.     plot.bar(range(11), scores_bar, align='center', alpha=0.5)
  34.  
  35.     plot.xticks(range(11))
  36.     plot.ylabel('Score frequency')
  37.     plot.title('Scores on a quiz')
  38.  
  39.     plot.show()
  40.     plot.savefig(fname="Quiz Chart.png")
  41.  
  42. def frequency_table(scores):
  43.     scores_bar = bar_chart_counts(scores)
  44.     freq_table = []
  45.     for index in range(11):
  46.         freq_table.append([index, scores_bar[index]])
  47.     print(freq_table)
  48.  
  49.     return freq_table
  50.  
  51. def mean_arithmetic_frequency(freq_table):
  52.     sum = 0
  53.     num = 0
  54.     for index in range(11):
  55.         sum = sum + freq_table[index][0] * freq_table[index][1]
  56.         num = num + freq_table[index][1]
  57.     arith_mean_freq = sum/num
  58.  
  59.     return arith_mean_freq
  60. '''
  61. def maximum_frequency(freq_table):
  62.    max = 0
  63.    while index in range(1,12):
  64.        if freq_table[-index][1] == 0:
  65.            max = 0
  66.        else:
  67.            max = freq_table[-index][1]
  68.  
  69.    return max
  70.    print(max)
  71. '''
  72. ####################################
  73. # "Create" a list of scores, randomly
  74. scores = []
  75. for x in range (0, 30):
  76.   scores.append(random.randint(0, 10))
  77. print("list of scores ", scores)
  78.  
  79. #####################################
  80. frequency_table(scores)
  81. # construct_bar_chart(scores)
  82. print("arithmetic mean > ", mean_arithmetic_frequency(frequency_table(scores)))
  83. #print(maximum_frequency(frequency_table(scores)))
  84. construct_bar_chart(scores)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement