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 = {'Border': '#045a8d', 'Area': '#74a9cf'}

#2
dof, npoints, nbins = 3, 10**6, 100
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 = np.random.chisquare(df=dof, size=npoints)

#5
density, _ = np.histogram(a=raw_data, bins=bin_edges, density=True)

#6
ax.plot(centroids, density, color=cpalette['Border'])
ax.fill_between(x=centroids, y1=density, color=cpalette['Area'])

#7
ax.set_xlim(left=lbound, right=ubound)
ax.spines[['right', 'top', 'left']].set_visible(False)

title = 'Frequency distribution of a random sample from $\chi^2$'
subtitle = f'{npoints:.2e} sample points, {nbins} bins, d.o.f. = {dof}.'
set_title_and_subtitle(fig=fig, title=title, subtitle=subtitle, 
                       alignment='left', h_offset=25)

fig.savefig('charts/area-chart-single.png', bbox_inches='tight',
            dpi=300)