Skip to content

Instantly share code, notes, and snippets.

@AO8
Last active March 29, 2019 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AO8/c89cdb21ba01a2fb4bc22f6f5bf0ba74 to your computer and use it in GitHub Desktop.
Save AO8/c89cdb21ba01a2fb4bc22f6f5bf0ba74 to your computer and use it in GitHub Desktop.
Plot multiple subplots within a single figure to visualize the shape of grade distribution in various IT courses with Python 3 and Matplotlib.
# Corresponding CSV files exported from local SQL Engine.
import csv
from collections import Counter
import matplotlib.pyplot as plt
grades_102 = []
grades_201 = []
grades_206 = []
grades_207 = []
grades_219 = []
grades_220 = []
with open("102.csv") as f1, open("201.csv") as f2, open("206.csv") as f3, open("207.csv") as f4, open("219.csv") as f5, open("220.csv") as f6:
reader1 = csv.DictReader(f1)
reader2 = csv.DictReader(f2)
reader3 = csv.DictReader(f3)
reader4 = csv.DictReader(f4)
reader5 = csv.DictReader(f5)
reader6 = csv.DictReader(f6)
for row in reader1:
grades_102.append(float(row["GPA"]))
for row in reader2:
grades_201.append(float(row["GPA"]))
for row in reader3:
grades_206.append(float(row["GPA"]))
for row in reader4:
grades_207.append(float(row["GPA"]))
for row in reader5:
grades_219.append(float(row["GPA"]))
for row in reader6:
grades_220.append(float(row["GPA"]))
# set plot style
plt.style.use("seaborn")
# set up fig and axs
fig, axs = plt.subplots(2,3)
# IT 102 histogram
axs[0,0].hist(grades_102, bins=15, color="#28a745")
axs[0,0].set_title("IT 102 Grades, B893")
axs[0,0].set_xlabel("Grades", fontsize=8)
axs[0,0].set_ylabel("Frequency", fontsize=8)
axs[0,0].set_ylim(0, 24)
axs[0,0].set_yticks(range(0,24, 2))
# IT 201 histogram
axs[0,1].hist(grades_201, bins=15, color="#28a745")
axs[0,1].set_title("IT 201 Grades, B893")
axs[0,1].set_xlabel("Grades", fontsize=8)
axs[0,1].set_ylabel("Frequency", fontsize=8)
axs[0,1].set_ylim(0, 26)
axs[0,1].set_yticks(range(0,26, 2))
# IT 206 histogram
axs[0,2].hist(grades_206, bins=15, color="#28a745")
axs[0,2].set_title("IT 206 Grades, B893")
axs[0,2].set_xlabel("Grades", fontsize=8)
axs[0,2].set_ylabel("Frequency", fontsize=8)
axs[0,2].set_ylim(0, 26)
axs[0,2].set_yticks(range(0,26, 2))
# IT 207 histogram
axs[1,0].hist(grades_207, bins=15, color="#28a745")
axs[1,0].set_title("IT 207 Grades, B893")
axs[1,0].set_xlabel("Grades", fontsize=8)
axs[1,0].set_ylabel("Frequency", fontsize=8)
axs[1,0].set_ylim(0, 8)
axs[1,0].set_yticks(range(0,8))
# IT 219 histogram
axs[1,1].hist(grades_219, bins=15, color="#28a745")
axs[1,1].set_title("IT 219 Grades, B893")
axs[1,1].set_xlabel("Grades", fontsize=8)
axs[1,1].set_ylabel("Frequency", fontsize=8)
axs[1,1].set_ylim(0, 28)
axs[1,1].set_yticks(range(0,28, 2))
# IT 220 histogram
axs[1,2].hist(grades_220, bins=15, color="#28a745")
axs[1,2].set_title("IT 220 Grades, B893")
axs[1,2].set_xlabel("Grades", fontsize=8)
axs[1,2].set_ylabel("Frequency", fontsize=8)
axs[1,2].set_ylim(0, 7)
axs[1,2].set_yticks(range(0,7))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment