Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Bar_chart_matplotlib coded by Ken Adams
- #Charting results of counting randomly generates using test scores random.randintand.
- #Then plots a bar chart using matplotlib. At the beginning the user is asked how many scores
- #they wish to genrate.Note;
- #There is educational value because the user will see that the mors scores generated the more near equal
- #the frequency distribution becomes.
- import matplotlib.pyplot as plot
- import random
- scores = [] # to hold the randomly generarted scores
- scores_top = int(input(" How many test scores do you want to generate? " ))
- for x in range (0, scores_top):
- scores.append(random.randint(0, 10))# randomly generates 30 scores in the range 10 to 10
- #print(scores) # for testing
- performance = [] # the frequency of scores will ed up here
- def score_freq(scores_list):# a list of scores is passed to this function
- freq = [] # will record the frequency of each score
- for x in range(11):# looping 0 to 10
- score_x = scores_list.count(x) #example for digit '2' conuits the number of 2's scored
- #print(str(x) + " " + str(score_x)) # test code
- freq.append(score_x)# at the x variable continues freq is built in correct order 0 to 10
- return freq # An external list gets its information from freq
- performance = score_freq(scores) # performance now has the frequencies
- #print(performance) # for testing
- # The rest of the code is for plotting
- plot.bar(range(11), performance, align='center', alpha=0.5)
- plot.xticks(range(11))
- plot.ylabel('Score frequency')
- plot.title('class Test Scores ')
- plot.show()
- plot.savefig(fname="2.10(102) bar Chart.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement