import matplotlib.pyplot as plt
import numpy as np
from mpl_ornaments.titles import set_title_and_subtitle

fig, ax = plt.subplots(nrows=1, ncols=1)

#1
cpalette = {'Border1': '#045a8d', 'Border2': '#d7191c',
            'Area1': '#74a9cf', 'Area2': '#d7191c'}

#2
npoints, nbins = 10**6, 100
mu, sigma = (3.0, 4.5), (1.0, 1.5)
support = (lbound := -.5, ubound := 8.0)

#3
bin_width = (ubound - lbound)/nbins
bin_edges = np.arange(start=lbound, stop=ubound, step=bin_width)
bin_edges = np.append(bin_edges, ubound)
centroids = (bin_edges[:-1] + bin_edges[1:])/2

#4
raw_data_1 = np.random.normal(loc=mu[0], scale=sigma[0], size=npoints)
raw_data_2 = np.random.normal(loc=mu[1], scale=sigma[1], size=npoints)

#5
density_1, _ = np.histogram(a=raw_data_1, bins=bin_edges, density=True)
density_2, _ = np.histogram(a=raw_data_2, bins=bin_edges, density=True)

#6
plot1, = ax.plot(centroids, density_1, color='#045a8d')
ax.fill_between(x=centroids, y1=density_1, color='#74a9cf', alpha=0.4)
plot2, = ax.plot(centroids, density_2, color='#d7191c')
ax.fill_between(x=centroids, y1=density_2, color='#fdae61', alpha=0.4)

ax.set_xlim(left=lbound, right=ubound)
ax.spines[['right', 'top', 'left']].set_visible(False)

#7
label1 = f'$N_1$: $\mu$ = {mu[0]}, $\sigma$ = {sigma[0]}'
label2 = f'$N_2$: $\mu$ = {mu[1]}, $\sigma$ = {sigma[1]}'
ax.legend(handles=[plot1, plot2], labels=[label1, label2], 
          loc='upper right', frameon=False)

title = 'Frequency distribution of two normal random samples'
subtitle = f'{npoints:.2e} sample points, {nbins} bins.'
set_title_and_subtitle(fig=fig, title=title, subtitle=subtitle, 
                       alignment='left', h_offset=25)

fig.savefig('charts/area-chart-multiple.png', bbox_inches='tight',
            dpi=300)