In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [2]:
data = [1,2,2,2,2,2,3,4,5,6,6,6,6,7,8,9,8,6,3]

plt.hist(data)
plt.xlabel('Value')
plt.ylabel('Number of appearances (count)')
plt.show()

# histdata = np.histogram(data,bins=np.arange(1,11))
y,x = np.histogram(data,bins=np.arange(1,11))
print(np.arange(1,11))
# print(histdata)
# print(histdata[1][:-1])

# plt.bar(histdata[1][:-1],histdata[0])
plt.bar(x[:-1],y)
plt.xlabel('Value')
plt.ylabel('Number of appearances (count)')
plt.show()

yp = y/np.sum(y)

plt.bar(x[:-1],yp)
plt.xlabel('Value')
plt.ylabel('Proportion')
plt.xticks(np.arange(1,10))
plt.show()

yp = y/np.max(y)

plt.bar(x[:-1],yp)
plt.xlabel('Value')
plt.ylabel('Proportion (norm.)')
plt.xticks(np.arange(1,10))
plt.show()
[ 1  2  3  4  5  6  7  8  9 10]

Exercise

Make a graph and probability density for thee functions.

$y=x$

$y=x^2$

$y=x^3$

In [3]:
x = np.linspace(-3,3,1000)

fig,ax = plt.subplots(2,3)
fig.set_size_inches(10,6)

for i in range(3):
    # create the function and its histogram
    y = x**(i+1)
    h = np.histogram(y,50)
    yP = h[0]/np.sum(h[0])
#     print(h)
#     print(yP)
    
    ax[0,i].plot(x,y)
    ax[0,i].set_title('$y=x^{%g}$' %(i+1))
    
    ax[1,i].plot(h[1][:-1],yP)
    
plt.show()