Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Bar Chart & Arith Mean using frequencies
- import matplotlib.pyplot as plot
- import random #just to generate data for testing
- # Add your count function here
- def count(target_grade,scores_list):
- target_hits = 0
- for score in scores_list:
- if score == target_grade:
- target_hits += 1
- return target_hits
- # Preparing raw data for bar chart: occurrence counts
- def bar_chart_counts(scores):
- scores_bar = []
- for x in range(11):
- scores_bar.append(count(x, scores))
- print("Bar chart data: ", scores_bar)
- sum = 0
- for index in range(len(scores_bar)):
- sum += scores_bar[index]
- if sum == len(scores):
- print("total number of data points in bar chart is: ", sum)
- return scores_bar
- else:
- print("Warning: there is something wrong here.")
- # Produce a bar chart from raw data, original scores
- def construct_bar_chart(scores):
- scores_bar = bar_chart_counts(scores) #generate occurrence count
- plot.bar(range(11), scores_bar, align='center', alpha=0.5)
- plot.xticks(range(11))
- plot.ylabel('Score frequency')
- plot.title('Scores on a quiz')
- plot.show()
- plot.savefig(fname="Quiz Chart.png")
- def frequency_table(scores):
- scores_bar = bar_chart_counts(scores)
- freq_table = []
- for index in range(11):
- freq_table.append([index, scores_bar[index]])
- print(freq_table)
- return freq_table
- def mean_arithmetic_frequency(freq_table):
- sum = 0
- num = 0
- for index in range(11):
- sum = sum + freq_table[index][0] * freq_table[index][1]
- num = num + freq_table[index][1]
- arith_mean_freq = sum/num
- return arith_mean_freq
- '''
- def maximum_frequency(freq_table):
- max = 0
- while index in range(1,12):
- if freq_table[-index][1] == 0:
- max = 0
- else:
- max = freq_table[-index][1]
- return max
- print(max)
- '''
- ####################################
- # "Create" a list of scores, randomly
- scores = []
- for x in range (0, 30):
- scores.append(random.randint(0, 10))
- print("list of scores ", scores)
- #####################################
- frequency_table(scores)
- # construct_bar_chart(scores)
- print("arithmetic mean > ", mean_arithmetic_frequency(frequency_table(scores)))
- #print(maximum_frequency(frequency_table(scores)))
- construct_bar_chart(scores)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement