This is written for Course MA2103 year 2023¶
In [1]:
import numpy as np ## basic array computing module
import matplotlib.pyplot as plt ## All plottings are done with this module
from matplotlib import cm ## for getting proper color maps
from matplotlib import colors as cls ## loading all colors
from numpy.random import chisquare as chisq ## this distribution is loaded
## For this exaple,
plt.rcParams["figure.figsize"] = [12,8] ## overriding default figure size!
## good to have big proper figure
my_colors=['aquamarine','black','peru', 'indigo', 'chocolate','darkolivegreen',
'dimgrey', 'mediumpurple', 'midnightblue', 'darkslategray', 'moccasin',
'lightseagreen', 'darkslateblue', 'crimson', 'fuchsia','khaki',
'maroon', 'sienna', 'fuchsia', 'slateblue'] ## Better have my own names of colors if needed
#### First we need a data for the making histogram
#### use chisquare and then cook it up a bit!
Nstudents=200 ## Number of students
mean=3 ## This is mean value for the chisquare distribution
## you could change this value to get different distribution!
### Here correcting the paper to give marks!
marks=3+np.floor(chisq(mean, Nstudents))
## 3 marks are grace marks for attending exam + Chisqare around mean 2
idx,=np.where(marks> 10 )
marks[idx]=3+np.floor(chisq(mean, len(idx)))
### ensture no body get more than 10:
### if more than 10 replace it with another chiqure
idx,=np.where(marks> 9 )
marks[idx]=3+np.floor(chisq(mean, len(idx)))
#### Cookup again nobody get more than 9; if so replace with another chisque
idx,=np.where(marks> 9 )
marks[idx]=9
#### er ... Somebody is still getting more than 9; OK fix them to 9!
### Now we have all marks between 3 and 9 !
#### Median is 6. But we can calculate median
### Now that we have cooked up thet data, we can start analysing it!
min_mark=np.amin(marks) ## find the minima
max_mark=np.amax(marks) ## find the maxima
median=min_mark+0.5*(max_mark-min_mark) ## median
mean=np.mean(marks) ## mean
#### Now lets make plot of marks vs students ID
fig1=plt.figure(1) ### create figure usually not needed, but we are doing it in style
plt.plot(marks,ls='None', marker='o', color='peru', ms=15, label='Marks') ### marks are plotted as serial number ,
### ls ='line style ' o
### color = 'peru' ! what colour is this ?
### marker size = ms = 15 big marker
plt.axhline(y=mean, color='slateblue',ls='--' ,lw=4, label='Mean')
### We draw a horizontal line with mean
### ls -> line style
### lw -> line width
plt.axhline(y=median, color='fuchsia',ls='--' ,lw=4, label='Median')
plt.xlabel('Students ID', size=18) ## Better label all the axis
plt.ylabel('Marks', size=18) ## ## Better label all the axis
plt.legend(fontsize="18") ## Legend, small box with what each symbol on the plots
plt.ylim([0, 11])
plt.savefig('time_series05.png') ## save figure if you want to
plt.show() ## just show off
#### Now let's make histogram
plt.hist(marks, bins=range(0,11), rwidth=0.9, align='left')
#### hist is the function does the work
#### marks is the array! 1-D now for the making histrogram
#### bins i.e. values of y for which we need frequency
#### I am giving bins such that plot looks good or exactly I want
#### rwidth = 0.9 so that it looks like bars, one can play around with it !
#### how the bar should oriten, left looks better
plt.axvline(x=mean, color='slateblue',ls='--' ,lw=4, label='Mean')
plt.axvline(x=median, color='fuchsia',ls='--' ,lw=4, label='Median')
plt.legend(fontsize="18") ## Legend, small box with what each symbol on the plots
plt.xlim([0, 11])
plt.savefig('histogram_05.png')
plt.xlabel('Marks', size=18)
plt.ylabel('Number or Frequency', size=18)
plt.grid(alpha=0.3)
plt.show() ##