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.5 KiB
Python

import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from plotstyle import *
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()
spec = gridspec.GridSpec(nrows=1, ncols=2, width_ratios=[3, 1], wspace=0.08,
**adjust_fs(fig, left=6.0))
ax1 = fig.add_subplot(spec[0, 0])
show_spines(ax1, 'lb')
ax1.scatter(indices, data, c=colors['blue'], edgecolor='white', s=50)
ax1.set_xlabel('Index')
ax1.set_ylabel('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')
ax2.set_xlabel('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()