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/statistics/lecture/quartile.py
2015-11-27 19:29:39 +01:00

50 lines
1.9 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
# normal distribution:
x = np.arange( -4.0, 4.0, 0.01 )
g = np.exp(-0.5*x*x)/np.sqrt(2.0*np.pi)
q = [ -0.67488, 0.0, 0.67488 ]
plt.xkcd()
fig = plt.figure( figsize=(6,3.4) )
ax = fig.add_subplot( 1, 1, 1 )
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax.set_xlabel( 'x' )
ax.set_ylabel( 'Probability density p(x)' )
ax.set_ylim( 0.0, 0.46 )
ax.set_yticks( np.arange( 0.0, 0.45, 0.1 ) )
ax.text(-1.2, 0.1, '25%', ha='center' )
ax.text(-0.35, 0.1, '25%', ha='center' )
ax.text(+0.35, 0.1, '25%', ha='center' )
ax.text(+1.2, 0.1, '25%', ha='center' )
ax.annotate('1. quartile',
xy=(-0.75, 0.2), xycoords='data',
xytext=(-1.7, 0.25), textcoords='data', ha='right',
arrowprops=dict(arrowstyle="->", relpos=(1.0,0.5),
connectionstyle="angle3,angleA=170,angleB=120") )
ax.annotate('3. quartile',
xy=(0.75, 0.17), xycoords='data',
xytext=(1.7, 0.22), textcoords='data', ha='left',
arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5),
connectionstyle="angle3,angleA=10,angleB=70") )
ax.annotate('Median',
xy=(0.1, 0.3), xycoords='data',
xytext=(1.6, 0.35), textcoords='data', ha='left',
arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5),
connectionstyle="angle3,angleA=10,angleB=40") )
ax.fill_between( x[x<q[0]], 0.0, g[x<q[0]], color='#ffcc00' )
ax.fill_between( x[(x>q[0])&(x<q[1])], 0.0, g[(x>q[0])&(x<q[1])], color='#ff0000' )
ax.fill_between( x[(x>q[1])&(x<q[2])], 0.0, g[(x>q[1])&(x<q[2])], color='#ff9900' )
ax.fill_between( x[x>q[2]], 0.0, g[x>q[2]], color='#ffff66' )
ax.plot(x,g, 'b', lw=4)
ax.plot([0.0, 0.0], [0.0, 0.45], 'k', lw=2 )
ax.plot([q[0], q[0]], [0.0, 0.4], 'k', lw=2 )
ax.plot([q[2], q[2]], [0.0, 0.4], 'k', lw=2 )
plt.tight_layout()
fig.savefig( 'quartile.pdf' )
#plt.show()