-1

I'm trying to make it so the table just represents that grade achieved once on average (index 2 in the nested list), right now, if I add more entries to the list (say, 100), it turns it into a 2D bar chart with every grade achieved. How can I make it to for example if a number of students achieved grade '1', that they will be grouped into that grade, and so forth?

import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt

master_list = [['Q34345678', "Charles Lob Miester Taylor To", 4, 'OM555'], ['Q54345678', "Tob", 9, 'COM363'],
               ['Q01245678', "Pob", 10, 'COM712'], ['Q98745678', "Cob", 3, 'COM343'], ['Q64345678', "Fob", 2, 'COM098'],
               ['Q84245678', "Bob", 1, 'COM712'],['Q34345678', " Taylor", 4, 'OM555'],['Q54345678', "Lauren", 9, 'COM363'],
               ['Q01245678', "Amy", 10, 'COM712'], ['Q98745678', "Nicola", 3, 'COM343'], ['Q64345678', "Yasmin", 2, 'COM098'],
               ['Q84245678', "Liv", 1, 'COM712'],]


#names = list(f[1] for f in master_list)
grades = list(f[2] for f in master_list)

y_pos = np.arange(len(master_list))

plt.figure(figsize=(20,10))
plt.bar(y_pos, grades, align='center', alpha=0.6)
#plt.xticks(y_pos,grades)
plt.ylabel('Grade')
plt.xlabel('Percent of students with achieved grade')
plt.title('2021 STUDENT CLASS')

plt.show()

cyberct
  • 25
  • 3
  • I am not sure what you want to achieve. Are you talking about a histogram? – Mr. T Jan 06 '21 at 15:11
  • 1
    Does this answer your question? [How to plot a histogram using Matplotlib in Python with a list of data?](https://stackoverflow.com/questions/33203645/how-to-plot-a-histogram-using-matplotlib-in-python-with-a-list-of-data) – Reti43 Jan 06 '21 at 15:15

2 Answers2

0

Just modify your code by a bit to get the percentage into the height variable of plt.bar():

names = list(f[1] for f in master_list)
grades = list(f[2] for f in master_list)
y_pos = np.arange(len(master_list))

g = {}
for i in grades:
    try:
        g[i] += 1
    except:
        g[i] = 1
h = []
for i in y_pos:
    try:
        if g[i]:
            h.append(g[i])
    except:
        h.append(0)


plt.figure(figsize=(20,10))
plt.bar(y_pos, h, align='center', alpha=0.6)
#plt.xticks(y_pos,grades)
plt.ylabel('Grade')
plt.xlabel('Percent of students with achieved grade')
plt.title('2021 STUDENT CLASS')

plt.show()
Rahul Vishwakarma
  • 1,446
  • 2
  • 7
  • 22
0

If I understand you correctly, I think the following will give you what you need.

import matplotlib.pyplot as plt
import numpy as np

master_list = [['Q34345678', "Charles Lob Miester Taylor To", 4, 'OM555'], ['Q54345678', "Tob", 9, 'COM363'],
               ['Q01245678', "Pob", 10, 'COM712'], ['Q98745678', "Cob", 3, 'COM343'], ['Q64345678', "Fob", 2, 'COM098'],
               ['Q84245678', "Bob", 1, 'COM712'],['Q34345678', " Taylor", 4, 'OM555'],['Q54345678', "Lauren", 9, 'COM363'],
               ['Q01245678', "Amy", 10, 'COM712'], ['Q98745678', "Nicola", 3, 'COM343'], ['Q64345678', "Yasmin", 2, 'COM098'],
               ['Q84245678', "Liv", 1, 'COM712'],]

#names = list(f[1] for f in master_list)
grades = list(f[2] for f in master_list)

plt.figure(figsize=(20,10))
plt.hist(grades, bins=np.arange(min(grades), max(grades)+2), rwidth=0.8, align='left')
plt.xticks(range(11)) # grades from 0 through 10
bincount = np.bincount(grades) # get the distribution in integer bins...
plt.yticks(range(max(np.bincount(grades) + 1))) # ...in order to make neat ticks
plt.ylabel('Frequency')
plt.xlabel('Grade')
plt.title('2021 STUDENT CLASS')

plt.show()