Data exploration made easy — subplots in Matplotlib

Import matplotlib.pyplot as plt%matplotlib inline
subjects = list(set(scores['Subject']))
passing = []
notpassing = []
y = {}
for i in subjects:
x = scores[scores['Subject'] == i]
passing.append(len(x[x['Passing'] == 'Passing']))
notpassing.append(len(x[x['Passing'] == 'Did not pass']))

y['subjects'] = subjects
y['passing'] = passing
y['notpassing'] = notpassing

df = pd.DataFrame.from_dict(y)
df = df.set_index('subjects')

df.plot(kind='bar',stacked=True)
plt.title('Count of students passing')
plt.show();
Single bar chart
Fig, axs = plt.subplots(n_rows, n_columns, figsize=(14,24))
plt.subplots_adjust(wspace=0.6, hspace=0.6)
schools = list(set(scores['High School']))
frpl = list(set(scores['FRPL']))
gender = list(set(scores['Gender']))
ethnicity = list(set(scores['Ethnicity']))

metrics = {'High School': schools, 'FRPL': frpl, 'Gender': gender,
'Ethnicity': ethnicity}
a = 0

for i, j in enumerate(metrics.keys()):
passing = []
notpassing = []
y = {}
z = metrics[j]
for k in z:
x = scores[scores[j] == k]
passing.append(len(x[x['Passing'] == 'Passing']))
notpassing.append(len(x[x['Passing'] == 'Did not pass']))

y[j] = z
y['passing'] = passing
y['notpassing'] = notpassing

N = len(passing)
ind = np.arange(N)
ticks = list(range(N))

df = pd.DataFrame.from_dict(y)
df = df.set_index(j)

axs[a,0].bar(ind, df['passing'], width=0.6)
axs[a,0].bar(ind, df['notpassing'], bottom=df.passing,width=0.6)
axs[a,0].set_title('Count of test takers')
axs[a,0].set_xlabel(j)
axs[a,0].set_xticks(ticks)
axs[a,0].set_xticklabels(z, fontdict = {'rotation': 45})

df['passrate'] = (df['passing'] / (df['passing'] + df['notpassing']))*100

axs[a,1].bar(ind, df['passrate'], width=0.6)
axs[a,1].set_title('Percentage of students passing')
axs[a,1].set_xlabel(j)
axs[a,1].set_xticks(ticks)
axs[a,1].set_xticklabels(z, fontdict = {'rotation': 45})
a += 1

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store