I have the following code:
df = sns.load_dataset('titanic')
# Data
data = df[df.age.notna()].age
# Fit a normal distribution to the data:
mu, std = scipy.stats.norm.fit(data)
# bin formulas
bin_f = {'sturges' : 1 + math.log(len(df), 2)}
# Plot the histogram.
sns.histplot( data = data, stat='density', bins=int(bin_f['sturges']), alpha=0.6, color='g', kde = True, legend = True)
# Plot the PDF.
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 1000)
p = scipy.stats.norm.pdf(x, mu, std)
sns.lineplot(x = x, y = p, color = 'black', linewidth=2)
title = f"Fit results: mu = {round(mu, 2)}, std ={round(std, 2)} "
plt.title(title)
Which produces this plot:
When I try to produce it in a subplot it wont work as expected:
f, ax = plt.subplots(nrows = 1, ncols = 2, figsize=(15, 8))
# Data
data = df[df.age.notna()].age
# Fit a normal distribution to the data:
mu, std = scipy.stats.norm.fit(data)
# bin formulas
bin_f = {'sturges' : 1 + math.log(len(df), 2)}
# Plot the histogram.
sns.histplot(ax = ax[0], data = data, stat='density', bins=int(bin_f['sturges']), alpha=0.4, color='g', kde = True, legend = True)
# Plot the PDF.
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 1000)
p = scipy.stats.norm.pdf(x, mu, std)
sns.lineplot(x = x, y = p, color = 'black', linewidth=2, ax=ax[0])
title = f"Fit results: mu = {round(mu, 2)}, std ={round(std, 2)} "
plt.title(title)
For some reason the title is only for a second plot and the previously plotted lineplot ( the black one ) is only a small tick in a second plot rather than a normal curve as in the first image. I am not sure why this is happening as the only difference is just using plt.subplots
and referencing ax
, where is my mistake?
My goal is to have the first graph as seen in the first picture as a the first subplot in the second plot.