This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/simulations/lecture/normaldata.py

48 lines
1.6 KiB
Python

import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from plotstyle import colors, cm_size, show_spines, set_xlabel, set_ylabel, bar_fac
if __name__ == "__main__":
# wikipedia:
# Generally, males vary in total length from 250 to 390 cm and
# weigh between 90 and 306 kg
n = 300
mu = 220.0
sigma = 40.0
rng = np.random.RandomState(32281)
indices = np.arange(n)
data = sigma*rng.randn(len(indices))+mu
fig = plt.figure(figsize=cm_size(16.0, 6.0))
spec = gridspec.GridSpec(nrows=1, ncols=2, width_ratios=[3, 1],
left=0.12, bottom=0.23, right=0.97, top=0.96, wspace=0.08)
ax1 = fig.add_subplot(spec[0, 0])
show_spines(ax1, 'lb')
ax1.scatter(indices, data, c=colors['blue'], edgecolor='white', s=50)
set_xlabel(ax1, 'index')
set_ylabel(ax1, 'Weight', 'kg')
ax1.set_xlim(-10, 310)
ax1.set_ylim(0, 370)
ax1.set_yticks(np.arange(0, 351, 100))
ax2 = fig.add_subplot(spec[0, 1])
show_spines(ax2, 'lb')
xx = np.arange(0.0, 350.0, 0.5)
yy = st.norm.pdf(xx, mu, sigma)
ax2.plot(yy, xx, color=colors['red'], lw=2)
bw = 20.0
h, b = np.histogram(data, np.arange(0, 401, bw))
ax2.barh(b[:-1], h/np.sum(h)/(b[1]-b[0]), fc=colors['yellow'], height=bar_fac*bw, align='edge')
set_xlabel(ax2, 'pdf', '1/kg')
ax2.set_xlim(0, 0.012)
ax2.set_xticks([0, 0.005, 0.01])
ax2.set_xticklabels(['0', '0.005', '0.01'])
ax2.set_ylim(0, 370)
ax2.set_yticks(np.arange(0, 351, 100))
ax2.set_yticklabels([])
fig.savefig("normaldata.pdf")
plt.close()