Advertisement
kenadams53

Bar_chart_matplotlib

Aug 27th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #Bar_chart_matplotlib coded by Ken Adams
  2. #Charting results of counting randomly generates using test scores random.randintand.
  3. #Then plots a bar chart using matplotlib. At the beginning the user is asked how many scores
  4. #they wish to genrate.Note;
  5. #There is educational value because the user will see that the mors scores generated the more near equal
  6. #the frequency distribution becomes.
  7.  
  8. import matplotlib.pyplot as plot
  9. import random
  10. scores = [] # to hold the randomly generarted scores
  11.  
  12. scores_top = int(input(" How many test scores do you want to generate? " ))
  13.  
  14. for x in range (0, scores_top):
  15. scores.append(random.randint(0, 10))# randomly generates 30 scores in the range 10 to 10
  16.  
  17. #print(scores) # for testing
  18. performance = [] # the frequency of scores will ed up here
  19.  
  20. def score_freq(scores_list):# a list of scores is passed to this function
  21. freq = [] # will record the frequency of each score
  22. for x in range(11):# looping 0 to 10
  23.  
  24. score_x = scores_list.count(x) #example for digit '2' conuits the number of 2's scored
  25. #print(str(x) + " " + str(score_x)) # test code
  26. freq.append(score_x)# at the x variable continues freq is built in correct order 0 to 10
  27. return freq # An external list gets its information from freq
  28.  
  29.  
  30. performance = score_freq(scores) # performance now has the frequencies
  31. #print(performance) # for testing
  32.  
  33. # The rest of the code is for plotting
  34. plot.bar(range(11), performance, align='center', alpha=0.5)
  35.  
  36. plot.xticks(range(11))
  37. plot.ylabel('Score frequency')
  38. plot.title('class Test Scores ')
  39.  
  40. plot.show()
  41. plot.savefig(fname="2.10(102) bar Chart.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement