I am trying to make an class with which I can make a s the histogram of any list and the plot of the data. My calculations run fine but the plot method does not seem to work. I would really appreciate your help here :)
from matplotlib import pyplot as plt
class BasicStatistics:
def __init__(self, list):
self.list = list
def mean(self):
return sum(self.list) / len(self.list)
def median(self):
self.list.sort()
if len(self.list) % 2 == 1:
return self.list[len(self.list) // 2]
else:
return (self.list[len(self.list) // 2 - 1] self.list[len(self.list) // 2]) / 2
def var(self):
avg = self.mean()
var = sum((x - avg) ** 2 for x in self.list) / len(self.list)
return var
def std_dev(self):
vari = self.var()
std_dev = (vari) ** (1 / 2)
return std_dev
def norma(self):
return [(i-self.mean()) / self.var() for i in self.list]
def plot(self):
fig, (axs1,axs2,axs3) = plt.subplots(2)
axs1.hist(self.list)
axs1.axvline(self.mean, color='red')
axs1.axvline(self.median, color='black')
axs1.axvline(self.mean - self.std_dev(), color='green')
axs1.axvline(self.mean self.std_dev(), color='green')
axs2.plot(lst)
plt.show()
CodePudding user response:
This is a version that produces a plot, but I am not sure if it is what you want. See my comment to the question for identified errors.
Note that stackoverflow is for specific questions, not a place for others to debug code. It is possible that your question may finally get deleted.
from matplotlib import pyplot as plt
class BasicStatistics:
def __init__(self, list):
self.list = list
def mean(self):
return sum(self.list) / len(self.list)
def median(self):
self.list.sort()
if len(self.list) % 2 == 1:
return self.list[len(self.list) // 2]
else:
return (self.list[len(self.list) // 2 - 1] self.list[len(self.list) // 2]) / 2
def var(self):
avg = self.mean()
var = sum((x - avg) ** 2 for x in self.list) / len(self.list)
return var
def std_dev(self):
vari = self.var()
std_dev = (vari) ** (1 / 2)
return std_dev
def norma(self):
return [(i-self.mean()) / self.var() for i in self.list]
def plot(self):
fig, (axs1,axs2) = plt.subplots(2)
axs1.hist(self.list)
axs1.axvline(self.mean(), color='red')
axs1.axvline(self.median(), color='black')
axs1.axvline(self.mean() - self.std_dev(), color='green')
axs1.axvline(self.mean() self.std_dev(), color='green')
axs2.plot(self.list)
plt.show()
aa = BasicStatistics([1,7,3, 4, 5, 3, 2])
aa.plot()