PYTHON 29
Bar chart By larsborg on 15th April 2020 05:07:57 AM
  1. # Bar Chart
  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. # "Create" a list of scores, randomly
  43. scores = []
  44. for x in range (0, 30):
  45.   scores.append(random.randint(0, 10))
  46. print("list of scores ", scores)
  47.  
  48. #####################################
  49.  
  50. construct_bar_chart(scores)

PasteBin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Lars Jansen Internet Consulting
Need some advise before starting your online business strategy or project?
Get in touch!
Cleanse Your CSS!
On average, about 35% of CSS code is completely unnecessary. We meticulously find and remove this unnecessary CSS code.
Raw Paste

Login or Register to edit or fork this paste. It's free.