Advertisement
HJ50

plot_performance

Apr 5th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. import matplotlib.pyplot as plot
  2. from random import randint
  3.  
  4. #count items in a list
  5. def count_items(target, my_list):
  6.    
  7.     item_count=0
  8.     for i in my_list:
  9.         if i==target:
  10.             item_count+=1
  11.    
  12.     return item_count
  13.  
  14. #count items in a range (passed as [lower, upper])
  15. def count_in_range(target_range, my_list):
  16.    
  17.     my_count=0
  18.     for i in my_list:
  19.         if i>=target_range[0] and i<=target_range[1] :
  20.             my_count+=1
  21.    
  22.     return my_count
  23.    
  24. #count vowels in a string
  25. def count_vowels_in_string(my_string):
  26.     vowels="aeiou"
  27.     my_count=0
  28.     lc_string=my_string.lower()
  29.     for i in vowels:
  30.         my_count=my_count+lc_string.count(i)
  31.            
  32.     return my_count
  33.    
  34. #data to work with
  35. scores=[]
  36. for i in range(30):
  37.     scores.append(randint(0,10))
  38. names=["fred", "bert", "harry", "fred"]
  39. my_word="supercalifragilisticexpialidociousUMDIDDLEIDDLEIDDLE"
  40.  
  41. #produce a bar chart of scores
  42. print(scores)
  43. performance=[]  #will contain a number for each score
  44. for i in range(11):
  45.     performance.append(count_items(i, scores))
  46.    
  47. plot.bar(range(11), performance, align='center', alpha=0.5)
  48.  
  49. plot.xticks(range(11))
  50. plot.ylabel('Score frequency')
  51. plot.title('Scores on a quiz')
  52.  
  53. plot.show()
  54. plot.savefig(fname="Quiz Chart.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement