"""
==================================================
Using histograms to plot a cumulative distribution
Adapted from https://matplotlib.org/3.1.0/gallery/statistics/histogram_cumulative.html
==================================================
"""

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(19610526)

mu, sigma, n_bins, n_samples = 0, 1, 400, 5000
x = np.random.normal(mu, sigma, size=n_samples)
xaverage = x.mean()
x -= xaverage
xstd = x.std()
x /= xstd

fig, ax = plt.subplots(figsize=(9, 6))


# plot the cumulative histogram
n, bins, patches = ax.hist(x, n_bins, density=True, histtype='step',
                           cumulative=True, label='Empirical', color='white' )


# Add a line showing the expected distribution.
y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
     np.exp(-0.5 * ((1 / sigma) * (bins - mu))**2))
y = y.cumsum()
y /= y[-1]

# https://www.rapidtables.com/web/color/RGB_Color.html
ax.plot(bins, y, linewidth=3, color='#8A2BE2')
ax.tick_params(axis = 'both', which = 'major', labelsize = 16)

locs, labels = plt.yticks()  # Get the current locations and labels.
plt.yticks(np.arange(0, 1.1, step=0.1))  
ax.set_ylim([0,1])
ax.set_title('Standard Normal Cumulative Distribution Function', fontsize=16)
plt.grid()
plt.tight_layout()


plt.savefig('sncdf_curve.png', dpi=200, bbox_inches='tight', transparent=True)
plt.show()
