From 2d0d962ea116bf9897d9a5b3caa852dfa8c11bc8 Mon Sep 17 00:00:00 2001 From: Jan Benda Date: Mon, 13 Jan 2020 23:48:31 +0100 Subject: [PATCH] [statistics] simplified plots, part 1 --- plotstyle.py | 164 ++++++-------------- statistics/lecture/cumulative.py | 33 ++-- statistics/lecture/diehistograms.py | 53 +++---- statistics/lecture/displayunivariatedata.py | 54 ++----- statistics/lecture/median.py | 88 +++++------ statistics/lecture/pdfhistogram.py | 55 +++---- statistics/lecture/pdfprobabilities.py | 34 ++-- statistics/lecture/quartile.py | 37 ++--- 8 files changed, 186 insertions(+), 332 deletions(-) diff --git a/plotstyle.py b/plotstyle.py index fd4e95b..4bf898c 100644 --- a/plotstyle.py +++ b/plotstyle.py @@ -13,125 +13,57 @@ figure_height = 6.0 # cm, for a 1 x 2 figure ppi = 72.0 # colors: +colors = {} +colors['red'] = '#CC0000' +colors['orange'] = '#FF9900' +colors['lightorange'] = '#FFCC00' +colors['yellow'] = '#FFFF66' +colors['green'] = '#99FF00' +colors['blue'] = '#0000CC' +colors['gray'] = '#A7A7A7' +colors['black'] = '#000000' + +#colors_bendalab_vivid['red'] = '#D71000' +#colors_bendalab_vivid['orange'] = '#FF9000' +#colors_bendalab_vivid['yellow'] = '#FFF700' +#colors_bendalab_vivid['green'] = '#30D700' +#colors_bendalab_vivid['blue'] = '#0020C0' -def lighter(color, lightness): - """ Make a color lighter. +# line styles for plot(): +lwthick = 4.0 +lwthin = 2.0 +fillalpha = 0.5 - Parameters - ---------- - color: string - An RGB color as a hexadecimal string (e.g. '#rrggbb'). - lightness: float - The smaller the lightness, the lighter the returned color. - A lightness of 1 leaves the color untouched. - A lightness of 0 returns white. +# helper lines: +lsSpine = {'c': colors['black'], 'linestyle': '-', 'linewidth': 1} +lsGrid = {'c': colors['gray'], 'linestyle': '--', 'linewidth': 1} +lsMarker = {'c': colors['black'], 'linestyle': '-', 'linewidth': 2} - Returns - ------- - color: string - The lighter color as a hexadecimal RGB string (e.g. '#rrggbb'). - """ - r = int(color[1:3], 16) - g = int(color[3:5], 16) - b = int(color[5:7], 16) - rl = r + (1.0-lightness)*(0xff - r) - gl = g + (1.0-lightness)*(0xff - g) - bl = b + (1.0-lightness)*(0xff - b) - return '#%02X%02X%02X' % (rl, gl, bl) +# line styles and fill styles: +lsA = {'color': colors['blue'], 'linestyle': '-', 'linewidth': lwthick} +lsAm = {'color': colors['blue'], 'linestyle': '-', 'linewidth': lwthin} +fsAa = {'facecolor': colors['blue'], 'edgecolor': 'none', 'alpha': fillalpha} +lsB = {'color': colors['red'], 'linestyle': '-', 'linewidth': lwthick} +lsBm = {'color': colors['red'], 'linestyle': '-', 'linewidth': lwthin} +fsB = {'facecolor': colors['red'], 'edgecolor': colors['black'], 'linewidth': 1} +fsBs = {'facecolor': colors['red'], 'edgecolor': 'none'} +fsBa = {'facecolor': colors['red'], 'edgecolor': 'none', 'alpha': fillalpha} -def darker(color, saturation): - """ Make a color darker. +lsC = {'color': colors['lightorange'], 'linestyle': '-', 'linewidth': lwthick} +lsCm = {'color': colors['lightorange'], 'linestyle': '-', 'linewidth': lwthin} +fsC = {'facecolor': colors['lightorange'], 'edgecolor': colors['black'], 'linewidth': 1} +fsCs = {'facecolor': colors['lightorange'], 'edgecolor': 'none'} +fsCa = {'facecolor': colors['lightorange'], 'edgecolor': 'none', 'alpha': fillalpha} - Parameters - ---------- - color: string - An RGB color as a hexadecimal string (e.g. '#rrggbb'). - saturation: float - The smaller the saturation, the darker the returned color. - A saturation of 1 leaves the color untouched. - A saturation of 0 returns black. - - Returns - ------- - color: string - The darker color as a hexadecimal RGB string (e.g. '#rrggbb'). - """ - r = int(color[1:3], 16) - g = int(color[3:5], 16) - b = int(color[5:7], 16) - rd = r * saturation - gd = g * saturation - bd = b * saturation - return '#%02X%02X%02X' % (rd, gd, bd) +fsD = {'facecolor': colors['orange'], 'edgecolor': colors['black'], 'linewidth': 1} +fsDs = {'facecolor': colors['orange'], 'edgecolor': 'none'} +fsE = {'facecolor': colors['yellow'], 'edgecolor': colors['black'], 'linewidth': 1} +fsEs = {'facecolor': colors['yellow'], 'edgecolor': 'none'} -# colors: -colors = { - 'red': '#CC0000', - 'orange': '#FF9900', - 'lightorange': '#FFCC00', - 'yellow': '#FFFF66', - 'green': '#99FF00', - 'blue': '#0000CC' - } - -""" Muted colors used by the Benda-lab. """ -colors_bendalab = {} -colors_bendalab['red'] = '#C02010' -colors_bendalab['orange'] = '#F78010' -colors_bendalab['yellow'] = '#F0D730' -colors_bendalab['green'] = '#A0B717' -colors_bendalab['cyan'] = '#40A787' -colors_bendalab['blue'] = '#2757A0' -colors_bendalab['purple'] = '#573790' -colors_bendalab['pink'] = '#C72750' -colors_bendalab['grey'] = '#A0A0A0' -colors_bendalab['black'] = '#000000' - -""" Vivid colors used by the Benda-lab. """ -colors_bendalab_vivid = {} -colors_bendalab_vivid['red'] = '#D71000' -colors_bendalab_vivid['orange'] = '#FF9000' -colors_bendalab_vivid['yellow'] = '#FFF700' -colors_bendalab_vivid['green'] = '#30D700' -colors_bendalab_vivid['cyan'] = '#00F0B0' -colors_bendalab_vivid['blue'] = '#0020C0' -colors_bendalab_vivid['purple'] = '#B000B0' -colors_bendalab_vivid['pink'] = '#F00080' -colors_bendalab_vivid['grey'] = '#A7A7A7' -colors_bendalab_vivid['black'] = '#000000' - -# colors for the plots of the script: -colors = colors_bendalab_vivid -colors['lightorange'] = colors['yellow'] -#colors['yellow'] = lighter(colors['yellow'], 0.65) -colors['yellow'] = '#FFFF55' - -# line styles for plot(): -lsSpine = {'c': colors['black'], 'linestyle': '-', 'linewidth': 1} -lsGrid = {'c': colors['grey'], 'linestyle': '--', 'linewidth': 1} - -# 'B1': prominent line with first color and style from color group 'B' -# 'C2m': minor line with second color and style from color group 'C' -ls = { - 'A1': {'c': colors['red'], 'linestyle': '-', 'linewidth': 3}, - 'A2': {'c': colors['orange'], 'linestyle': '-', 'linewidth': 3}, - 'A3': {'c': colors['lightorange'], 'linestyle': '-', 'linewidth': 3}, - 'B1': {'c': colors['orange'], 'linestyle': '-', 'linewidth': 3}, - 'B2': {'c': colors['lightorange'], 'linestyle': '-', 'linewidth': 3}, - 'B3': {'c': colors['yellow'], 'linestyle': '-', 'linewidth': 3}, - 'C1': {'c': colors['green'], 'linestyle': '-', 'linewidth': 3}, - 'D1': {'c': colors['blue'], 'linestyle': '-', 'linewidth': 3}, - 'A1m': {'c': colors['red'], 'linestyle': '-', 'linewidth': 2}, - 'A2m': {'c': colors['orange'], 'linestyle': '-', 'linewidth': 2}, - 'A3m': {'c': colors['lightorange'], 'linestyle': '-', 'linewidth': 2}, - 'B1m': {'c': colors['orange'], 'linestyle': '-', 'linewidth': 2}, - 'B2m': {'c': colors['lightorange'], 'linestyle': '-', 'linewidth': 2}, - 'B3m': {'c': colors['yellow'], 'linestyle': '-', 'linewidth': 2}, - 'C1m': {'c': colors['green'], 'linestyle': '-', 'linewidth': 2}, - 'D1m': {'c': colors['blue'], 'linestyle': '-', 'linewidth': 2}, - } +fsF = {'facecolor': colors['green'], 'edgecolor': colors['black'], 'linewidth': 1} +fsFs = {'facecolor': colors['green'], 'edgecolor': 'none'} # factor for scaling widths of bars in a bar plot: bar_fac = 1.0 @@ -369,14 +301,12 @@ def common_format(): mpl.rcParams['axes.linewidth'] = lsSpine['linewidth'] if 'axes.prop_cycle' in mpl.rcParams: mpl.rcParams['axes.prop_cycle'] = cycler(color=[colors['blue'], colors['red'], - colors['orange'], colors['green'], - colors['purple'], colors['yellow'], - colors['cyan'], colors['pink']]) + colors['lightorange'], colors['orange'], + colors['yellow'], colors['green']]) else: mpl.rcParams['axes.color_cycle'] = [colors['blue'], colors['red'], - colors['orange'], colors['green'], - colors['purple'], colors['yellow'], - colors['cyan'], colors['pink']] + colors['lightorange'], colors['orange'], + colors['yellow'], colors['green']] # overwrite axes constructor: if not hasattr(mpl.axes.Subplot, '__init__orig'): mpl.axes.Subplot.__init__orig = mpl.axes.Subplot.__init__ diff --git a/statistics/lecture/cumulative.py b/statistics/lecture/cumulative.py index a1cc849..5d9cedb 100644 --- a/statistics/lecture/cumulative.py +++ b/statistics/lecture/cumulative.py @@ -1,5 +1,6 @@ import numpy as np import matplotlib.pyplot as plt +from plotstyle import * # data: rng = np.random.RandomState(981) @@ -14,39 +15,31 @@ gauss = np.exp(-0.5*xx*xx)/np.sqrt(2.0*np.pi) gausscdf = np.cumsum(gauss)*dx # plot: -plt.xkcd() -fig = plt.figure( figsize=(6, 2.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' ) +fig, ax = plt.subplots() +ax.set_xlabel('x') ax.set_xlim(-3.2, 3.2) -ax.set_xticks( np.arange( -3.0, 3.1, 1.0 ) ) -ax.set_ylabel( 'F(x)' ) +ax.set_xticks(np.arange(-3.0, 3.1, 1.0)) +ax.set_ylabel('F(x)') ax.set_ylim(-0.05, 1.05) -ax.set_yticks( np.arange( 0.0, 1.1, 0.2 ) ) +ax.set_yticks(np.arange(0.0, 1.1, 0.2)) med = xs[cdf>=0.5][0] -ax.plot([-3.2, med, med], [0.5, 0.5, 0.0], 'k', lw=1, zorder=-5) +ax.plot([-3.2, med, med], [0.5, 0.5, 0.0], zorder=-5, **lsMarker) ax.text(-2.8, 0.55, 'F=0.5') ax.text(0.15, 0.25, 'median at %.2f' % med) q3 = xs[cdf>=0.75][0] -ax.plot([-3.2, q3, q3], [0.75, 0.75, 0.0], 'k', lw=1, zorder=-5) +ax.plot([-3.2, q3, q3], [0.75, 0.75, 0.0], zorder=-5, **lsMarker) ax.text(-2.8, 0.8, 'F=0.75') ax.text(0.8, 0.5, '3. quartile at %.2f' % q3) p = cdf[xs>=-1.0][0] -ax.plot([-3.2, -1.0, -1.0], [p, p, 0.0], 'k', lw=1, zorder=-5) +ax.plot([-3.2, -1.0, -1.0], [p, p, 0.0], zorder=-5, **lsMarker) ax.text(-2.8, 0.2, 'F=%.2f' % p) ax.text(-0.9, 0.05, '-1') -ax.plot(xx, gausscdf, '-', color='#0000ff', lw=2, zorder=-1) -ax.plot(xs, cdf, '-', color='#cc0000', lw=4, zorder=-1) -ax.plot([-3.2, 3.2], [1.0, 1.0], '--', color='k', lw=2, zorder=-10) +ax.plot(xx, gausscdf, zorder=-1, **lsAm) +ax.plot(xs, cdf, zorder=-1, **lsB) +ax.plot([-3.2, 3.2], [1.0, 1.0], zorder=-10, **lsGrid) -plt.subplots_adjust(left=0.1, right=0.98, bottom=0.15, top=0.98, wspace=0.35, hspace=0.3) -fig.savefig( 'cumulative.pdf' ) -#plt.show() +fig.savefig('cumulative.pdf') diff --git a/statistics/lecture/diehistograms.py b/statistics/lecture/diehistograms.py index 622d39b..57283a1 100644 --- a/statistics/lecture/diehistograms.py +++ b/statistics/lecture/diehistograms.py @@ -1,39 +1,30 @@ import numpy as np import matplotlib.pyplot as plt +from plotstyle import * # roll the die: rng = np.random.RandomState(57281) -x1 = rng.random_integers( 1, 6, 100 ) -x2 = rng.random_integers( 1, 6, 500 ) +x1 = rng.random_integers(1, 6, 100) +x2 = rng.random_integers(1, 6, 500) bins = np.arange(0.5, 7, 1.0) -plt.xkcd() +fig, (ax1, ax2) = plt.subplots(1, 2) +fig.subplots_adjust(**adjust_fs(bottom=2.7, top=0.1)) +ax1.set_xlim(0, 7) +ax1.set_xticks(range(1, 7)) +ax1.set_xlabel('x') +ax1.set_ylim(0, 98) +ax1.set_ylabel('Frequency') +fs = fsC +fs['color'] = [fsC['facecolor'], fsE['facecolor']] +del fs['facecolor'] +ax1.hist([x2, x1], bins, **fs) -fig = plt.figure( figsize=(6,3) ) -ax = fig.add_subplot( 1, 2, 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_xlim(0, 7) -ax.set_xticks( range(1, 7) ) -ax.set_xlabel( 'x' ) -ax.set_ylim(0, 98) -ax.set_ylabel( 'Frequency' ) -ax.hist([x2, x1], bins, color=['#FFCC00', '#FFFF66' ]) - -ax = fig.add_subplot( 1, 2, 2 ) -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_xlim(0, 7) -ax.set_xticks( range(1, 7) ) -ax.set_xlabel( 'x' ) -ax.set_ylim(0, 0.23) -ax.set_ylabel( 'Probability' ) -ax.plot([0.2, 6.8], [1.0/6.0, 1.0/6.0], '-b', lw=2, zorder=1) -ax.hist([x2, x1], bins, normed=True, color=['#FFCC00', '#FFFF66' ], zorder=10) -plt.subplots_adjust(left=0.1, right=0.98, bottom=0.15, top=0.98, wspace=0.4, hspace=0.0) -fig.savefig( 'diehistograms.pdf' ) -#plt.show() +ax2.set_xlim(0, 7) +ax2.set_xticks(range(1, 7)) +ax2.set_xlabel('x') +ax2.set_ylim(0, 0.23) +ax2.set_ylabel('Probability') +ax2.plot([0.2, 6.8], [1.0/6.0, 1.0/6.0], zorder=1, **lsAm) +ax2.hist([x2, x1], bins, normed=True, zorder=10, **fs) +fig.savefig('diehistograms.pdf') diff --git a/statistics/lecture/displayunivariatedata.py b/statistics/lecture/displayunivariatedata.py index 97816f2..1eb36aa 100644 --- a/statistics/lecture/displayunivariatedata.py +++ b/statistics/lecture/displayunivariatedata.py @@ -1,33 +1,24 @@ import numpy as np import matplotlib.pyplot as plt +import matplotlib.gridspec as gridspec from scipy.stats import gaussian_kde +from plotstyle import * #rng = np.random.RandomState(981) #data = rng.randn(40, 1) + 4.0 rng = np.random.RandomState(1981) data = rng.gamma(1.0, 1.5, 40) + 1.0 data = data[data<7.5] -xpos = 0.08 -ypos = 0.15 -width = 0.65 -height = 0.8 barwidth = 0.8 scatterpos = 1.0 barpos = 2.5 boxpos = 4.0 -plt.xkcd() -fig = plt.figure( figsize=(6,3.4) ) +fig = plt.figure(figsize=cm_size(figure_width, 1.4*figure_height)) +spec = gridspec.GridSpec(nrows=1, ncols=2, width_ratios=[3, 1], wspace=0.1, + **adjust_fs(fig, left=4.0)) -ax = fig.add_axes([xpos, ypos, width, height]) -ax.spines['right'].set_visible(False) -ax.spines['top'].set_visible(False) -#ax.spines['left'].set_visible(False) -#ax.spines['bottom'].set_visible(False) -#ax.xaxis.set_ticks_position('none') -#ax.yaxis.set_ticks_position('none') -#ax.set_xticklabels([]) -#ax.set_yticklabels([]) +ax = fig.add_subplot(spec[0, 0]) wh = ax.boxplot( data, positions=[boxpos], widths=[barwidth], whis=100.0, patch_artist=True) wh['medians'][0].set_linewidth(4) wh['whiskers'][0].set_linewidth(2) @@ -49,7 +40,7 @@ ax.annotate('maximum', connectionstyle="angle3,angleA=0,angleB=120") ) ax.annotate('3. quartile', xy=(boxpos-0.3*barwidth, 3.7), xycoords='data', - xytext=(boxpos-1.3*barwidth, 5.5), textcoords='data', ha='left', + xytext=(boxpos-0.1*barwidth, 5.5), textcoords='data', ha='right', arrowprops=dict(arrowstyle="->", relpos=(0.4,0.0), connectionstyle="angle3,angleA=0,angleB=120") ) ax.annotate('median', @@ -57,19 +48,14 @@ ax.annotate('median', xytext=(boxpos+0.1*barwidth, 4.2), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.8,0.0), connectionstyle="angle3,angleA=-60,angleB=20") ) - -ax = fig.add_axes([xpos, ypos, width, height]) -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_xticklabels([]) -ax.set_xlim(0.0, 4.8) -ax.set_ylabel('x') -ax.set_ylim( 0.0, 8.0) +ax = fig.add_subplot(spec[0, 0]) +ax.set_xlim(0.0, 4.8) ax.set_xticks([scatterpos, barpos, boxpos]) ax.set_xticklabels(['(1) data', '(2) bar\n plot', '(3) box-\nwhisker']) +ax.set_ylabel('x') +ax.set_ylim( 0.0, 8.0) # scatter data points according to their density: kernel = gaussian_kde(data) @@ -80,11 +66,10 @@ ax.scatter(scatterpos+barwidth*x*(rng.rand(len(data))-0.5), data, s=50) barmean = np.mean(data) barstd = np.std(data) ew = 0.2 -ax.bar([barpos-0.5*barwidth], [barmean], barwidth, color='#FFCC00') -eargs = {'color': 'k', 'lw': 2} -ax.plot([barpos, barpos], [barmean-barstd, barmean+barstd], **eargs) -ax.plot([barpos-0.5*ew, barpos+0.5*ew], [barmean-barstd, barmean-barstd], **eargs) -ax.plot([barpos-0.5*ew, barpos+0.5*ew], [barmean+barstd, barmean+barstd], **eargs) +ax.bar([barpos-0.5*barwidth], [barmean], barwidth, **fsC) +ax.plot([barpos, barpos], [barmean-barstd, barmean+barstd], **lsMarker) +ax.plot([barpos-0.5*ew, barpos+0.5*ew], [barmean-barstd, barmean-barstd], **lsMarker) +ax.plot([barpos-0.5*ew, barpos+0.5*ew], [barmean+barstd, barmean+barstd], **lsMarker) ax.annotate('mean', xy=(barpos-0.4*barwidth, 2.7), xycoords='data', xytext=(barpos-1*barwidth, 5.5), textcoords='data', ha='left', @@ -96,11 +81,7 @@ ax.annotate('mean plus\nstd. dev.', arrowprops=dict(arrowstyle="->", relpos=(0.5,0.0), connectionstyle="angle3,angleA=-60,angleB=80") ) -ax = fig.add_axes([xpos+width+0.03, ypos, 0.98-(xpos+width+0.03), height]) -ax.spines['right'].set_visible(False) -ax.spines['top'].set_visible(False) -ax.xaxis.set_ticks_position('bottom') -ax.yaxis.set_ticks_position('left') +ax = fig.add_subplot(spec[0, 1]) ax.set_yticklabels([]) ax.set_ylim( 0.0, 8.0) ax.set_xticks(np.arange(0.0, 0.4, 0.1)) @@ -108,8 +89,7 @@ ax.set_xlabel('(4) p(x)') bw = 0.75 bins = np.arange(0, 8.0+bw, bw) h, b = np.histogram(data, bins) -ax.barh(b[:-1], h/bw/np.sum(h), bw, color='#CC0000') +ax.barh(b[:-1], h/bw/np.sum(h), bw, **fsB) plt.savefig('displayunivariatedata.pdf') -#plt.show() diff --git a/statistics/lecture/median.py b/statistics/lecture/median.py index 8204688..da6fe40 100644 --- a/statistics/lecture/median.py +++ b/statistics/lecture/median.py @@ -1,78 +1,66 @@ import numpy as np import scipy.stats as st import matplotlib.pyplot as plt +from plotstyle import * # normal distribution: -x = np.arange( -3.0, 3.0, 0.01 ) +x = np.arange(-3.0, 3.0, 0.01) g = np.exp(-0.5*x*x)/np.sqrt(2.0*np.pi) -plt.xkcd() -fig = plt.figure( figsize=(6, 2.8) ) -ax = fig.add_subplot(1, 2, 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( 'Prob. density p(x)' ) -ax.set_ylim( 0.0, 0.46 ) -ax.set_yticks( np.arange( 0.0, 0.45, 0.1 ) ) -ax.text(-1.0, 0.06, '50%', ha='center' ) -ax.text(+1.0, 0.06, '50%', ha='center' ) -ax.annotate('Median\n= mean', +fig, (ax1, ax2) = plt.subplots(1, 2) +fig.subplots_adjust(**adjust_fs(bottom=2.7, top=0.1)) +ax1.set_xlabel('x') +ax1.set_ylabel('Prob. density p(x)') +ax1.set_ylim(0.0, 0.46) +ax1.set_yticks(np.arange(0.0, 0.45, 0.1)) +ax1.text(-1.0, 0.06, '50%', ha='center') +ax1.text(+1.0, 0.06, '50%', ha='center') +ax1.annotate('Median\n= mean', xy=(0.1, 0.3), xycoords='data', xytext=(1.2, 0.35), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.0,0.2), - connectionstyle="angle3,angleA=10,angleB=40") ) -ax.annotate('Mode', + connectionstyle="angle3,angleA=10,angleB=40")) +ax1.annotate('Mode', xy=(-0.1, 0.4), xycoords='data', xytext=(-2.5, 0.43), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.0,0.2), - connectionstyle="angle3,angleA=10,angleB=120") ) -ax.fill_between( x[x<0], 0.0, g[x<0], color='#ffcc00' ) -ax.fill_between( x[x>0], 0.0, g[x>0], color='#99ff00' ) -ax.plot(x, g, 'b', lw=4) -ax.plot([0.0, 0.0], [0.0, 0.45], 'k', lw=2 ) + connectionstyle="angle3,angleA=10,angleB=120")) +ax1.fill_between(x[x<0], 0.0, g[x<0], **fsCs) +ax1.fill_between(x[x>0], 0.0, g[x>0], **fsFs) +ax1.plot(x, g, **lsA) +ax1.plot([0.0, 0.0], [0.0, 0.45], **lsMarker) -# normal distribution: -x = np.arange( 0.0, 6.0, 0.01 ) +# gamma distribution: +x = np.arange(0.0, 6.0, 0.01) shape = 2.0 g = st.gamma.pdf(x, shape) m = st.gamma.median(shape) gm = st.gamma.mean(shape) -ax = fig.add_subplot(1, 2, 2) -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( 'Prob. density p(x)' ) -ax.set_ylim( 0.0, 0.46 ) -ax.set_yticks( np.arange( 0.0, 0.45, 0.1 ) ) -ax.text(m-0.8, 0.06, '50%', ha='center' ) -ax.text(m+1.2, 0.06, '50%', ha='center' ) -ax.annotate('Median', +ax2.set_xlabel('x') +ax2.set_ylabel('Prob. density p(x)') +ax2.set_ylim(0.0, 0.46) +ax2.set_yticks(np.arange(0.0, 0.45, 0.1)) +ax2.text(m-0.8, 0.06, '50%', ha='center') +ax2.text(m+1.2, 0.06, '50%', ha='center') +ax2.annotate('Median', xy=(m+0.1, 0.2), xycoords='data', xytext=(m+1.6, 0.25), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5), - connectionstyle="angle3,angleA=30,angleB=70") ) -ax.annotate('Mean', + connectionstyle="angle3,angleA=30,angleB=70")) +ax2.annotate('Mean', xy=(gm, 0.01), xycoords='data', xytext=(gm+1.8, 0.15), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5), - connectionstyle="angle3,angleA=0,angleB=90") ) -ax.annotate('Mode', + connectionstyle="angle3,angleA=0,angleB=90")) +ax2.annotate('Mode', xy=(1.0, 0.38), xycoords='data', xytext=(1.8, 0.42), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5), - connectionstyle="angle3,angleA=0,angleB=70") ) -ax.fill_between( x[xm], 0.0, g[x>m], color='#99ff00' ) -ax.plot(x, g, 'b', lw=4) -ax.plot([m, m], [0.0, 0.38], 'k', lw=2 ) -#ax.plot([gm, gm], [0.0, 0.42], 'k', lw=2 ) + connectionstyle="angle3,angleA=0,angleB=70")) +ax2.fill_between(x[xm], 0.0, g[x>m], **fsFs) +ax2.plot(x, g, **lsA) +ax2.plot([m, m], [0.0, 0.38], **lsMarker) +#ax2.plot([gm, gm], [0.0, 0.38], **lsMarker) -#plt.tight_layout() -plt.subplots_adjust(left=0.1, right=0.98, bottom=0.15, top=0.98, wspace=0.4, hspace=0.0) -fig.savefig( 'median.pdf' ) -#plt.show() +fig.savefig('median.pdf') diff --git a/statistics/lecture/pdfhistogram.py b/statistics/lecture/pdfhistogram.py index e35606c..805e113 100644 --- a/statistics/lecture/pdfhistogram.py +++ b/statistics/lecture/pdfhistogram.py @@ -1,44 +1,31 @@ import numpy as np import matplotlib.pyplot as plt +from plotstyle import * # normal distribution: rng = np.random.RandomState(6281) -x = np.arange( -4.0, 4.0, 0.01 ) +x = np.arange(-4.0, 4.0, 0.01) g = np.exp(-0.5*x*x)/np.sqrt(2.0*np.pi) -r = rng.randn( 100 ) +r = rng.randn(100) -plt.xkcd() +fig, (ax1, ax2) = plt.subplots(1, 2) +ax1.set_xlabel('x') +ax1.set_xlim(-3.2, 3.2) +ax1.set_xticks(np.arange(-3.0, 3.1, 1.0)) +ax1.set_ylabel('Frequency') +ax1.set_yticks(np.arange(0.0, 41.0, 10.0)) +ax1.hist(r, 5, zorder=-10, **fsB) +ax1.hist(r, 20, zorder=-5, **fsC) -fig = plt.figure( figsize=(6,3) ) -ax = fig.add_subplot( 1, 2, 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_xlim(-3.2, 3.2) -ax.set_xticks( np.arange( -3.0, 3.1, 1.0 ) ) -ax.set_ylabel( 'Frequency' ) -ax.set_yticks( np.arange( 0.0, 41.0, 10.0 ) ) -ax.hist(r, 5, color='#CC0000') -ax.hist(r, 20, color='#FFCC00') +ax2.set_xlabel('x') +ax2.set_xlim(-3.2, 3.2) +ax2.set_xticks(np.arange(-3.0, 3.1, 1.0)) +ax2.set_ylabel('Probab. density p(x)') +ax2.set_ylim(0.0, 0.44) +ax2.set_yticks(np.arange(0.0, 0.41, 0.1)) +ax2.plot(x, g, zorder=-1, **lsA) +ax2.hist(r, 5, normed=True, zorder=-10, **fsB) +ax2.hist(r, 20, normed=True, zorder=-5, **fsC) -ax = fig.add_subplot( 1, 2, 2 ) -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_xlim(-3.2, 3.2) -ax.set_xticks( np.arange( -3.0, 3.1, 1.0 ) ) -ax.set_ylabel( 'Probab. density p(x)' ) -ax.set_ylim(0.0, 0.44) -ax.set_yticks( np.arange( 0.0, 0.41, 0.1 ) ) -ax.plot(x, g, '-b', lw=2, zorder=-1) -ax.hist(r, 5, normed=True, color='#CC0000', zorder=-10) -ax.hist(r, 20, normed=True, color='#FFCC00', zorder=-5) - -plt.subplots_adjust(left=0.1, right=0.98, bottom=0.15, top=0.98, wspace=0.4, hspace=0.0) -fig.savefig( 'pdfhistogram.pdf' ) -#plt.show() +fig.savefig('pdfhistogram.pdf') diff --git a/statistics/lecture/pdfprobabilities.py b/statistics/lecture/pdfprobabilities.py index 10c4482..bafe13b 100644 --- a/statistics/lecture/pdfprobabilities.py +++ b/statistics/lecture/pdfprobabilities.py @@ -1,36 +1,28 @@ import numpy as np import matplotlib.pyplot as plt +from plotstyle import * # normal distribution: -x = np.arange( -3.0, 5.0, 0.01 ) +x = np.arange(-3.0, 5.0, 0.01) g = np.exp(-0.5*x*x)/np.sqrt(2.0*np.pi) x1=0.0 x2=1.0 -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 ) ) +fig, ax = plt.subplots() +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.annotate('Gaussian', xy=(-1.0, 0.28), xycoords='data', xytext=(-2.5, 0.35), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.5,0.0), - connectionstyle="angle3,angleA=10,angleB=110") ) + connectionstyle="angle3,angleA=10,angleB=110")) ax.annotate('$P(0", relpos=(0.0,0.5), - connectionstyle="angle3,angleA=10,angleB=80") ) -ax.fill_between( x[(x>x1)&(xx1)&(xx1)&(xx1)&(x", relpos=(0.0,0.5), connectionstyle="angle3,angleA=10,angleB=40") ) -ax.fill_between( x[xq[0])&(xq[0])&(xq[1])&(xq[1])&(xq[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.subplots_adjust(left=0.1, right=0.98, bottom=0.15, top=0.98, wspace=0.4, hspace=0.0) -#plt.tight_layout() +ax.fill_between( x[xq[0])&(xq[0])&(xq[1])&(xq[1])&(xq[2]], 0.0, g[x>q[2]], **fsEs) +ax.plot(x,g, **lsA) +ax.plot([0.0, 0.0], [0.0, 0.45], **lsMarker) +ax.plot([q[0], q[0]], [0.0, 0.4], **lsMarker) +ax.plot([q[2], q[2]], [0.0, 0.4], **lsMarker) fig.savefig( 'quartile.pdf' ) -#plt.show()