From d19e0bcb3a96978d7feb8b837f5a19a3c06b4a83 Mon Sep 17 00:00:00 2001 From: Jan Benda Date: Thu, 9 Jan 2020 16:33:05 +0100 Subject: [PATCH 1/7] color cycler imported where iti is needed --- plotstyle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotstyle.py b/plotstyle.py index fd4e95b..de5d5fd 100644 --- a/plotstyle.py +++ b/plotstyle.py @@ -1,6 +1,5 @@ import matplotlib as mpl import matplotlib.pyplot as plt -from cycler import cycler from mpl_toolkits.mplot3d import Axes3D xkcd_style = False @@ -368,6 +367,7 @@ def common_format(): mpl.rcParams['axes.edgecolor'] = lsSpine['c'] mpl.rcParams['axes.linewidth'] = lsSpine['linewidth'] if 'axes.prop_cycle' in mpl.rcParams: + from cycler import cycler mpl.rcParams['axes.prop_cycle'] = cycler(color=[colors['blue'], colors['red'], colors['orange'], colors['green'], colors['purple'], colors['yellow'], From cf2d82b94b46bd04738e41d9706029619804d9ef Mon Sep 17 00:00:00 2001 From: Jan Benda Date: Mon, 13 Jan 2020 22:38:31 +0100 Subject: [PATCH 2/7] [projects] updated README --- projects/README | 68 ++++++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/projects/README b/projects/README index cba7a86..cfbe1c5 100644 --- a/projects/README +++ b/projects/README @@ -1,94 +1,104 @@ +For new projects: +Copy project_template/ and adapt according to your needs + All projects: check for time information +1) project_activation_curve +medium +Write questions -project_adaptation_fit +2) project_adaptation_fit OK, medium Add plotting of cost function -project_eod +3) project_eod OK, medium - difficult b_0 is not defined -project_eyetracker +4) project_eyetracker OK, difficult no statistics, but kmeans -project_fano_slope +5) project_face_selectivity +medium-difficult +(Marius monkey data) + +6) project_fano_slope OK, difficult -project_fano_test +7) project_fano_test OK - -project_fano_time +8) project_fano_time OK, medium-difficult -project_ficurves +9) project_ficurves OK, medium Maybe add correlation test or fit statistics -project_input_resistance +10) project_input_resistance medium What is the problem with this project? --> No difference between segments Improve questions -project_isicorrelations +11) project_isicorrelations medium-difficult Need to finish solution -project_isipdffit +12) project_isipdffit Too technical -project_lif +13) project_lif OK, difficult no statistics -project_mutualinfo +14) project_mutualinfo OK, medium -project_noiseficurves +15) project_noiseficurves OK, simple-medium no statistics -project_numbers +16) project_numbers simple We might add some more involved statistical analysis -project_pca_natural_images +17) project_pca_natural_images medium Make a solution (->Lukas) -project_photoreceptor +18) project_photoreceptor OK, simple -project_populationvector +19) project_populationvector difficult OK -project_qvalues +20) project_power_analysis +medium + +21) project_qvalues - Interesting! But needs solution. -project_random_walk +22) project_random_walk simple-medium -project_serialcorrelation +23) project_serialcorrelation OK, simple-medium -project_spectra +24) project_shorttermpotentiation +Write questions + +25) project_spectra - Needs improvements and a solution -project_stimulus_reconstruction +26) project_stimulus_reconstruction OK, difficult -project_vector_strength +27) project_vector_strength OK, medium-difficult - -project_power_analysis -medium - -Marius monkey data: -medium-difficult \ No newline at end of file From 2d0d962ea116bf9897d9a5b3caa852dfa8c11bc8 Mon Sep 17 00:00:00 2001 From: Jan Benda Date: Mon, 13 Jan 2020 23:48:31 +0100 Subject: [PATCH 3/7] [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() From 999a3003a6580bc4bf805f52943d32ec739af614 Mon Sep 17 00:00:00 2001 From: Jan Benda Date: Tue, 14 Jan 2020 17:52:27 +0100 Subject: [PATCH 4/7] [statistics] simplified plots, part 2 --- plotstyle.py | 4 +- statistics/lecture/boxwhisker.py | 15 ++---- statistics/lecture/correlation.py | 37 +++++++------- statistics/lecture/kerneldensity.py | 64 ++++++++++--------------- statistics/lecture/nonlincorrelation.py | 51 ++++++++------------ 5 files changed, 70 insertions(+), 101 deletions(-) diff --git a/plotstyle.py b/plotstyle.py index 82d084e..ef13e65 100644 --- a/plotstyle.py +++ b/plotstyle.py @@ -29,8 +29,8 @@ colors['black'] = '#000000' #colors_bendalab_vivid['blue'] = '#0020C0' # line styles for plot(): -lwthick = 4.0 -lwthin = 2.0 +lwthick = 3.0 +lwthin = 1.8 fillalpha = 0.5 # helper lines: diff --git a/statistics/lecture/boxwhisker.py b/statistics/lecture/boxwhisker.py index c3cc373..76ee315 100644 --- a/statistics/lecture/boxwhisker.py +++ b/statistics/lecture/boxwhisker.py @@ -1,19 +1,14 @@ import numpy as np import matplotlib.pyplot as plt +from plotstyle import * rng = np.random.RandomState(981) x = rng.randn( 40, 10 ) -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') +fig, ax = plt.subplots() ax.set_xlabel('Experiment') ax.set_ylabel('x') -ax.set_ylim( -4.0, 4.0) +ax.set_ylim(-4.0, 4.0) ax.annotate('Median', xy=(3.9, 0.0), xycoords='data', xytext=(3.5, -2.7), textcoords='data', ha='right', @@ -39,8 +34,6 @@ ax.annotate('maximum', xytext=(4.9, 3.5), textcoords='data', ha='right', arrowprops=dict(arrowstyle="->", relpos=(1.0,0.5), connectionstyle="angle3,angleA=0,angleB=120") ) -ax.boxplot( x, whis=100.0 ) -plt.tight_layout() +ax.boxplot(x, whis=100.0) plt.savefig('boxwhisker.pdf') -#plt.show() diff --git a/statistics/lecture/correlation.py b/statistics/lecture/correlation.py index 3019a65..f4bbf80 100644 --- a/statistics/lecture/correlation.py +++ b/statistics/lecture/correlation.py @@ -1,33 +1,30 @@ import numpy as np import matplotlib.pyplot as plt +import matplotlib.gridspec as gridspec +from plotstyle import * -plt.xkcd() -fig = plt.figure( figsize=(6,4.6) ) +fig = plt.figure(figsize=cm_size(figure_width, 1.5*figure_height)) +spec = gridspec.GridSpec(nrows=2, ncols=2, wspace=0.35, hspace=0.35, + **adjust_fs(fig, left=5.5, top=0.5, bottom=2.7)) rng = np.random.RandomState(2981) n = 200 -for k, r in enumerate( [ 1.0, 0.6, 0.0, -0.9 ] ) : - x = rng.randn( n ) - y = r*x + np.sqrt(1.0-r*r)*rng.randn( n ) - ax = fig.add_subplot( 2, 2, k+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.text( -2, 2.5, 'r=%.1f' % r ) +for k, r in enumerate([ 1.0, 0.6, 0.0, -0.9 ]) : + x = rng.randn(n) + y = r*x + np.sqrt(1.0-r*r)*rng.randn(n) + ax = fig.add_subplot(spec[k//2, k%2]) + ax.text(-2, 2.5, 'r=%.1f' % r) if k == 0 : - ax.text( 2.8, -2, 'positively\ncorrelated', ha='right' ) + ax.text(2.8, -2.8, 'positively\ncorrelated', ha='right', va='bottom') elif k == 1 : - ax.text( 2.8, -2.5, 'weakly\ncorrelated', ha='right' ) + ax.text(2.8, -2.8, 'weakly\ncorrelated', ha='right', va='bottom') elif k == 2 : - ax.text( 2.8, -2.5, 'not\ncorrelated', ha='right' ) + ax.text(2.8, -2.8, 'not\ncorrelated', ha='right', va='bottom') elif k == 3 : - ax.text( -2.5, -2, 'negatively\ncorrelated', ha='left' ) + ax.text(-2.8, -2.8, 'negatively\ncorrelated', ha='left', va='bottom') ax.set_xlabel('x') ax.set_ylabel('y') - ax.set_xlim( -3.0, 3.0) - ax.set_ylim( -3.0, 3.0) - ax.scatter( x[(np.abs(x)<2.8)&(np.abs(y)<2.8)], y[(np.abs(x)<2.8)&(np.abs(y)<2.8)] ) + ax.set_xlim(-3.0, 3.0) + ax.set_ylim(-3.0, 3.0) + ax.scatter(x[(np.abs(x)<2.8)&(np.abs(y)<2.8)], y[(np.abs(x)<2.8)&(np.abs(y)<2.8)]) -plt.tight_layout() plt.savefig('correlation.pdf') -#plt.show() diff --git a/statistics/lecture/kerneldensity.py b/statistics/lecture/kerneldensity.py index cfdfb62..19e06b9 100644 --- a/statistics/lecture/kerneldensity.py +++ b/statistics/lecture/kerneldensity.py @@ -1,9 +1,11 @@ import numpy as np import matplotlib.pyplot as plt +import matplotlib.gridspec as gridspec +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) @@ -29,55 +31,41 @@ def kerneldensity(data, xmin, xmax, sigma=1.0) : kd[k0:k1] += gauss[g0:g1] kd /= len(data) return kd, x - -plt.xkcd() +fig = plt.figure() +spec = gridspec.GridSpec(nrows=2, ncols=2, wspace=0.35, hspace=0.3, + **adjust_fs(fig, left=5.5, top=0.2, bottom=2.7)) -fig = plt.figure( figsize=(6,3) ) -ax = fig.add_subplot(2, 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 = fig.add_subplot(spec[0, 0]) +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( 'p(x)' ) +ax.set_xticks(np.arange(-3.0, 3.1, 1.0)) +ax.set_ylabel('p(x)') ax.set_ylim(0.0, 0.49) -ax.set_yticks( np.arange( 0.0, 0.41, 0.1 ) ) +ax.set_yticks(np.arange(0.0, 0.41, 0.1)) #ax.plot(x, g, '-b', lw=2, zorder=-1) -ax.hist(r, np.arange(-4.1, 4, 0.4), normed=True, color='#FFCC00', zorder=-5) +ax.hist(r, np.arange(-4.1, 4, 0.4), normed=True, zorder=-5, **fsC) -ax = fig.add_subplot(2, 2, 3) -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 = fig.add_subplot(spec[1, 0]) +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( 'p(x)' ) +ax.set_xticks(np.arange(-3.0, 3.1, 1.0)) +ax.set_ylabel('p(x)') ax.set_ylim(0.0, 0.49) -ax.set_yticks( np.arange( 0.0, 0.41, 0.1 ) ) +ax.set_yticks(np.arange(0.0, 0.41, 0.1)) #ax.plot(x, g, '-b', lw=2, zorder=-1) -ax.hist(r, np.arange(-4.3, 4, 0.4), normed=True, color='#FFCC00', zorder=-5) +ax.hist(r, np.arange(-4.3, 4, 0.4), 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 = fig.add_subplot(spec[:, 1]) +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_xticks(np.arange(-3.0, 3.1, 1.0)) +ax.set_ylabel('Probab. density p(x)') ax.set_ylim(0.0, 0.49) -ax.set_yticks( np.arange( 0.0, 0.41, 0.1 ) ) +ax.set_yticks(np.arange(0.0, 0.41, 0.1)) kd, xx = kerneldensity(r, -3.2, 3.2, 0.2) -ax.fill_between(xx, 0.0, kd, color='#FF9900', zorder=-5) -ax.plot(xx, kd, '-', lw=3, color='#CC0000', zorder=-1) +ax.fill_between(xx, 0.0, kd, zorder=-5, **fsDs) +ax.plot(xx, kd, '-', zorder=-1, **lsB) -plt.subplots_adjust(left=0.1, right=0.98, bottom=0.15, top=0.98, wspace=0.35, hspace=0.3) -fig.savefig( 'kerneldensity.pdf' ) -#plt.show() +fig.savefig('kerneldensity.pdf') diff --git a/statistics/lecture/nonlincorrelation.py b/statistics/lecture/nonlincorrelation.py index e346826..16a5a20 100644 --- a/statistics/lecture/nonlincorrelation.py +++ b/statistics/lecture/nonlincorrelation.py @@ -1,42 +1,33 @@ import numpy as np import matplotlib.pyplot as plt +import matplotlib.gridspec as gridspec +from plotstyle import * + +fig, (ax1, ax2) = plt.subplots(1, 2) -plt.xkcd() -fig = plt.figure( figsize=(6,2.2) ) n = 200 -x = np.random.randn( n ) -y = np.random.randn( n ) +rng = np.random.RandomState(3981) +x = rng.randn(n) +y = rng.randn(n) z = x*x+0.2*y r =np.corrcoef(x,z)[0,1] -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.text( 0, 4.0, 'r=%.1f' % r, ha='center' ) -ax.text( 0, 6, r'$y = x^2+\xi/5$', ha='center' ) -ax.set_xlabel('x') -ax.set_ylabel('y') -ax.set_xlim( -3.0, 3.0) -ax.set_ylim( -0.5, 6.0) -ax.scatter( x, z ) +ax1.text(0, 4.0, 'r=%.1f' % r, ha='center') +ax1.text(0, 5.6, r'$y = x^2+\xi/5$', ha='center') +ax1.set_xlabel('x') +ax1.set_ylabel('y') +ax1.set_xlim(-3.0, 3.0) +ax1.set_ylim(-0.5, 6.0) +ax1.scatter(x, z) z = 0.5*x*y r =np.corrcoef(x,z)[0,1] -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.text( 0, 1.5, 'r=%.1f' % r, ha='center' ) -ax.text( 0, 3, r'$y = x \cdot \xi/2$', ha='center' ) -ax.set_xlabel('x') -ax.set_ylabel('y') -ax.set_xlim( -3.0, 3.0) -ax.set_ylim( -3.0, 3.0) -ax.scatter( x, z ) +ax2.text(0, 1.5, 'r=%.1f' % r, ha='center') +ax2.text(0, 2.8, r'$y = x \cdot \xi/2$', ha='center') +ax2.set_xlabel('x') +ax2.set_ylabel('y') +ax2.set_xlim(-3.0, 3.0) +ax2.set_ylim(-3.0, 3.0) +ax2.scatter(x, z) -plt.tight_layout() plt.savefig('nonlincorrelation.pdf') -#plt.show() From 75aa46d71c575f19c48de1bbc50a033997648b68 Mon Sep 17 00:00:00 2001 From: Jan Benda Date: Tue, 14 Jan 2020 20:58:03 +0100 Subject: [PATCH 5/7] [statistics] simplified plots, last part, added pointstyles --- plotstyle.py | 54 ++++++++++++++++----- statistics/lecture/correlation.py | 6 +-- statistics/lecture/cumulative.py | 2 +- statistics/lecture/displayunivariatedata.py | 4 +- statistics/lecture/nonlincorrelation.py | 6 ++- statistics/lecture/pdfprobabilities.py | 2 +- statistics/lecture/quartile.py | 2 +- 7 files changed, 53 insertions(+), 23 deletions(-) diff --git a/plotstyle.py b/plotstyle.py index ef13e65..f41fce7 100644 --- a/plotstyle.py +++ b/plotstyle.py @@ -18,9 +18,10 @@ colors['orange'] = '#FF9900' colors['lightorange'] = '#FFCC00' colors['yellow'] = '#FFFF66' colors['green'] = '#99FF00' -colors['blue'] = '#0000CC' +colors['blue'] = '#0010CC' colors['gray'] = '#A7A7A7' colors['black'] = '#000000' +colors['white'] = '#FFFFFF' #colors_bendalab_vivid['red'] = '#D71000' #colors_bendalab_vivid['orange'] = '#FF9000' @@ -31,6 +32,11 @@ colors['black'] = '#000000' # line styles for plot(): lwthick = 3.0 lwthin = 1.8 +mainline = {'linestyle': '-', 'linewidth': lwthick} +minorline = {'linestyle': '-', 'linewidth': lwthin} +largemarker = {'marker': 'o', 'markersize': 9, 'markeredgecolor': colors['white'], 'markeredgewidth': 1} +smallmarker = {'marker': 'o', 'markersize': 6, 'markeredgecolor': colors['white'], 'markeredgewidth': 1} +filllw = 1.0 fillalpha = 0.5 # helper lines: @@ -38,30 +44,52 @@ lsSpine = {'c': colors['black'], 'linestyle': '-', 'linewidth': 1} lsGrid = {'c': colors['gray'], 'linestyle': '--', 'linewidth': 1} lsMarker = {'c': colors['black'], 'linestyle': '-', 'linewidth': 2} -# line styles and fill styles: -lsA = {'color': colors['blue'], 'linestyle': '-', 'linewidth': lwthick} -lsAm = {'color': colors['blue'], 'linestyle': '-', 'linewidth': lwthin} +# line (ls), point (ps), and fill styles (fs). + +# Each style is derived from a main color as indicated by the capital letter. + +# Line styles come in two variants: +# - plain style with a thick/solid line (e.g. lsA), and +# - minor style with a thinner or dashed line (e.g. lsAm). + +# Point styles come in two variants: +# - plain style with large solid markers (e.g. psA), and +# - minor style with smaller markers (e.g. lsBm). + +# Fill styles come in three variants: +# - plain (e.g. fsB) for a solid fill color and a darker edge color, +# - solid (e.g. fsBs) for a solid fill color and without edge color, and +# - alpha (e.g. fsBa) for a transparent fill color without edge color. + +lsA = dict({'color': colors['blue']}, **mainline) +lsAm = dict({'color': colors['blue']}, **minorline) +psA = dict({'color': colors['blue'], 'linestyle': 'none'}, **largemarker) +psAm = dict({'color': colors['blue'], 'linestyle': 'none'}, **smallmarker) 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} +lsB = dict({'color': colors['red']}, **mainline) +lsBm = dict({'color': colors['red']}, **minorline) +psB = dict({'color': colors['red'], 'linestyle': 'none'}, **largemarker) +psBm = dict({'color': colors['red'], 'linestyle': 'none'}, **smallmarker) +fsB = {'facecolor': colors['red'], 'edgecolor': colors['black'], 'linewidth': filllw} fsBs = {'facecolor': colors['red'], 'edgecolor': 'none'} fsBa = {'facecolor': colors['red'], 'edgecolor': 'none', 'alpha': fillalpha} -lsC = {'color': colors['lightorange'], 'linestyle': '-', 'linewidth': lwthick} -lsCm = {'color': colors['lightorange'], 'linestyle': '-', 'linewidth': lwthin} -fsC = {'facecolor': colors['lightorange'], 'edgecolor': colors['black'], 'linewidth': 1} +lsC = dict({'color': colors['lightorange']}, **mainline) +lsCm = dict({'color': colors['lightorange']}, **minorline) +psC = dict({'color': colors['lightorange'], 'linestyle': 'none'}, **largemarker) +psCm = dict({'color': colors['lightorange'], 'linestyle': 'none'}, **smallmarker) +fsC = {'facecolor': colors['lightorange'], 'edgecolor': colors['black'], 'linewidth': filllw} fsCs = {'facecolor': colors['lightorange'], 'edgecolor': 'none'} fsCa = {'facecolor': colors['lightorange'], 'edgecolor': 'none', 'alpha': fillalpha} -fsD = {'facecolor': colors['orange'], 'edgecolor': colors['black'], 'linewidth': 1} +fsD = {'facecolor': colors['orange'], 'edgecolor': colors['black'], 'linewidth': filllw} fsDs = {'facecolor': colors['orange'], 'edgecolor': 'none'} -fsE = {'facecolor': colors['yellow'], 'edgecolor': colors['black'], 'linewidth': 1} +fsE = {'facecolor': colors['yellow'], 'edgecolor': colors['black'], 'linewidth': filllw} fsEs = {'facecolor': colors['yellow'], 'edgecolor': 'none'} -fsF = {'facecolor': colors['green'], 'edgecolor': colors['black'], 'linewidth': 1} +fsF = {'facecolor': colors['green'], 'edgecolor': colors['black'], 'linewidth': filllw} fsFs = {'facecolor': colors['green'], 'edgecolor': 'none'} # factor for scaling widths of bars in a bar plot: diff --git a/statistics/lecture/correlation.py b/statistics/lecture/correlation.py index f4bbf80..e594f8e 100644 --- a/statistics/lecture/correlation.py +++ b/statistics/lecture/correlation.py @@ -3,8 +3,8 @@ import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec from plotstyle import * -fig = plt.figure(figsize=cm_size(figure_width, 1.5*figure_height)) -spec = gridspec.GridSpec(nrows=2, ncols=2, wspace=0.35, hspace=0.35, +fig = plt.figure(figsize=cm_size(figure_width, 2.0*figure_height)) +spec = gridspec.GridSpec(nrows=2, ncols=2, wspace=0.35, hspace=0.5, **adjust_fs(fig, left=5.5, top=0.5, bottom=2.7)) rng = np.random.RandomState(2981) n = 200 @@ -25,6 +25,6 @@ for k, r in enumerate([ 1.0, 0.6, 0.0, -0.9 ]) : ax.set_ylabel('y') ax.set_xlim(-3.0, 3.0) ax.set_ylim(-3.0, 3.0) - ax.scatter(x[(np.abs(x)<2.8)&(np.abs(y)<2.8)], y[(np.abs(x)<2.8)&(np.abs(y)<2.8)]) + ax.plot(x[(np.abs(x)<2.8)&(np.abs(y)<2.8)], y[(np.abs(x)<2.8)&(np.abs(y)<2.8)], **psAm) plt.savefig('correlation.pdf') diff --git a/statistics/lecture/cumulative.py b/statistics/lecture/cumulative.py index 5d9cedb..014805e 100644 --- a/statistics/lecture/cumulative.py +++ b/statistics/lecture/cumulative.py @@ -15,7 +15,7 @@ gauss = np.exp(-0.5*xx*xx)/np.sqrt(2.0*np.pi) gausscdf = np.cumsum(gauss)*dx # plot: -fig, ax = plt.subplots() +fig, ax = plt.subplots(figsize=cm_size(figure_width, 1.2*figure_height)) ax.set_xlabel('x') ax.set_xlim(-3.2, 3.2) ax.set_xticks(np.arange(-3.0, 3.1, 1.0)) diff --git a/statistics/lecture/displayunivariatedata.py b/statistics/lecture/displayunivariatedata.py index 1eb36aa..da2c347 100644 --- a/statistics/lecture/displayunivariatedata.py +++ b/statistics/lecture/displayunivariatedata.py @@ -14,7 +14,7 @@ scatterpos = 1.0 barpos = 2.5 boxpos = 4.0 -fig = plt.figure(figsize=cm_size(figure_width, 1.4*figure_height)) +fig = plt.figure(figsize=cm_size(figure_width, 1.2*figure_height)) spec = gridspec.GridSpec(nrows=1, ncols=2, width_ratios=[3, 1], wspace=0.1, **adjust_fs(fig, left=4.0)) @@ -61,7 +61,7 @@ ax.set_ylim( 0.0, 8.0) kernel = gaussian_kde(data) x = kernel(data) x /= np.max(x) -ax.scatter(scatterpos+barwidth*x*(rng.rand(len(data))-0.5), data, s=50) +ax.plot(scatterpos+barwidth*x*(rng.rand(len(data))-0.5), data, **psA) barmean = np.mean(data) barstd = np.std(data) diff --git a/statistics/lecture/nonlincorrelation.py b/statistics/lecture/nonlincorrelation.py index 16a5a20..1fc1fc2 100644 --- a/statistics/lecture/nonlincorrelation.py +++ b/statistics/lecture/nonlincorrelation.py @@ -4,6 +4,8 @@ import matplotlib.gridspec as gridspec from plotstyle import * fig, (ax1, ax2) = plt.subplots(1, 2) +fig.subplots_adjust(wspace=0.35, hspace=0.5, + **adjust_fs(fig, left=4, top=0.5, bottom=2.7)) n = 200 rng = np.random.RandomState(3981) @@ -18,7 +20,7 @@ ax1.set_xlabel('x') ax1.set_ylabel('y') ax1.set_xlim(-3.0, 3.0) ax1.set_ylim(-0.5, 6.0) -ax1.scatter(x, z) +ax1.plot(x, z, **psAm) z = 0.5*x*y r =np.corrcoef(x,z)[0,1] @@ -28,6 +30,6 @@ ax2.set_xlabel('x') ax2.set_ylabel('y') ax2.set_xlim(-3.0, 3.0) ax2.set_ylim(-3.0, 3.0) -ax2.scatter(x, z) +ax2.plot(x, z, **psAm) plt.savefig('nonlincorrelation.pdf') diff --git a/statistics/lecture/pdfprobabilities.py b/statistics/lecture/pdfprobabilities.py index bafe13b..3e9e3c6 100644 --- a/statistics/lecture/pdfprobabilities.py +++ b/statistics/lecture/pdfprobabilities.py @@ -8,7 +8,7 @@ g = np.exp(-0.5*x*x)/np.sqrt(2.0*np.pi) x1=0.0 x2=1.0 -fig, ax = plt.subplots() +fig, ax = plt.subplots(figsize=cm_size(figure_width, 1.2*figure_height)) ax.set_xlabel('x') ax.set_ylabel('Probability density p(x)') ax.set_ylim(0.0, 0.46) diff --git a/statistics/lecture/quartile.py b/statistics/lecture/quartile.py index 36d54bf..856552a 100644 --- a/statistics/lecture/quartile.py +++ b/statistics/lecture/quartile.py @@ -7,7 +7,7 @@ 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 ] -fig, ax = plt.subplots(figsize=cm_size(figure_width, 1.2*figure_height)) +fig, ax = plt.subplots(figsize=cm_size(figure_width, 1.0*figure_height)) fig.subplots_adjust(**adjust_fs(bottom=2.7, top=0.1)) ax.set_xlabel('x') ax.set_ylabel('Probability density p(x)') From 9769d5e94f5d4022349cdca37b960c20c73b2080 Mon Sep 17 00:00:00 2001 From: Jan Benda Date: Tue, 14 Jan 2020 23:38:16 +0100 Subject: [PATCH 6/7] updated plots of the data analysis chapters --- bootstrap/lecture/bootstrapsem.py | 16 ++--- bootstrap/lecture/permutecorrelation.py | 24 +++----- likelihood/lecture/mlecoding.py | 65 +++++++++----------- likelihood/lecture/mlemean.py | 59 ++++++++---------- likelihood/lecture/mlepdf.py | 60 ++++++++----------- likelihood/lecture/mlepropline.py | 66 +++++++-------------- plotstyle.py | 51 +++++++++++----- regression/lecture/cubicerrors.py | 16 ++--- regression/lecture/cubicfunc.py | 6 +- regression/lecture/cubicmse.py | 12 ++-- regression/lecture/derivative.py | 23 ++++--- regression/lecture/lin_regress.py | 10 ++-- regression/lecture/linear_least_squares.py | 10 ++-- statistics/lecture/cumulative.py | 6 +- statistics/lecture/diehistograms.py | 4 +- statistics/lecture/displayunivariatedata.py | 6 +- statistics/lecture/kerneldensity.py | 2 +- statistics/lecture/statistics.tex | 62 +++++++++---------- 18 files changed, 226 insertions(+), 272 deletions(-) diff --git a/bootstrap/lecture/bootstrapsem.py b/bootstrap/lecture/bootstrapsem.py index 66154ce..d8b0346 100644 --- a/bootstrap/lecture/bootstrapsem.py +++ b/bootstrap/lecture/bootstrapsem.py @@ -1,8 +1,7 @@ import numpy as np import matplotlib.pyplot as plt +from plotstyle import * -plt.xkcd() -fig = plt.figure( figsize=(6,3.5) ) rng = np.random.RandomState(637281) nsamples = 100 @@ -25,11 +24,8 @@ for i in range(nresamples) : musrs.append(np.mean(rng.randn(nsamples))) hmusrs, _ = np.histogram(musrs, bins, density=True) -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') +fig, ax = plt.subplots(figsize=cm_size(figure_width, 1.2*figure_height)) +fig.subplots_adjust(**adjust_fs(left=4.0, bottom=2.7, right=1.5)) ax.set_xlabel('Mean') ax.set_xlim(-0.4, 0.4) ax.set_ylabel('Probability density') @@ -45,9 +41,7 @@ ax.annotate('bootstrap\ndistribution', xytext=(0.25, 4), textcoords='data', arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5), connectionstyle="angle3,angleA=20,angleB=60") ) -ax.bar(bins[:-1]-0.25*db, hmusrs, 0.5*db, color='r') -ax.bar(bins[:-1]+0.25*db, hmus, 0.5*db, color='b') +ax.bar(bins[:-1]-0.25*db, hmusrs, 0.5*db, **fsB) +ax.bar(bins[:-1]+0.25*db, hmus, 0.5*db, **fsA) -plt.tight_layout() plt.savefig('bootstrapsem.pdf') -#plt.show(); diff --git a/bootstrap/lecture/permutecorrelation.py b/bootstrap/lecture/permutecorrelation.py index b6bc1bd..2f10af8 100644 --- a/bootstrap/lecture/permutecorrelation.py +++ b/bootstrap/lecture/permutecorrelation.py @@ -1,9 +1,8 @@ import numpy as np import scipy.stats as st import matplotlib.pyplot as plt +from plotstyle import * -plt.xkcd() -fig = plt.figure( figsize=(6,3.5) ) rng = np.random.RandomState(637281) # generate correlated data: @@ -36,33 +35,28 @@ print('Measured correlation coefficient %.2f is at %.4f percentile of bootstrap' rp, ra = st.pearsonr(x, y) print('Measured correlation coefficient %.2f is at %.4f percentile of test' % (rp, ra)) -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') +fig, ax = plt.subplots(figsize=cm_size(figure_width, 1.2*figure_height)) +fig.subplots_adjust(**adjust_fs(left=4.0, bottom=2.7, right=0.5, top=1.0)) ax.annotate('Measured\ncorrelation\nis significant!', xy=(rd, 1.1), xycoords='data', xytext=(rd, 2.2), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.2,0.0), - connectionstyle="angle3,angleA=10,angleB=80") ) + connectionstyle="angle3,angleA=10,angleB=80") ) ax.annotate('95% percentile', xy=(0.14, 0.9), xycoords='data', xytext=(0.2, 4.0), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.1,0.0), - connectionstyle="angle3,angleA=30,angleB=70") ) + connectionstyle="angle3,angleA=30,angleB=70") ) ax.annotate('Distribution of\nuncorrelated\nsamples', xy=(-0.08, 3.6), xycoords='data', xytext=(-0.22, 5.0), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.5,0.0), - connectionstyle="angle3,angleA=150,angleB=100") ) -ax.bar(b[:-1], h, width=b[1]-b[0], color='#ffff66') -ax.bar(b[:-1][b[:-1]>=rq], h[b[:-1]>=rq], width=b[1]-b[0], color='#ff9900') -ax.plot( [rd, rd], [0, 1], 'b', linewidth=4 ) + connectionstyle="angle3,angleA=150,angleB=100") ) +ax.bar(b[:-1], h, width=b[1]-b[0], **fsC) +ax.bar(b[:-1][b[:-1]>=rq], h[b[:-1]>=rq], width=b[1]-b[0], **fsB) +ax.plot( [rd, rd], [0, 1], **lsA) ax.set_xlim(-0.25, 0.35) ax.set_xlabel('Correlation coefficient') ax.set_ylabel('Probability density of H0') -plt.tight_layout() plt.savefig('permutecorrelation.pdf') -#plt.show(); diff --git a/likelihood/lecture/mlecoding.py b/likelihood/lecture/mlecoding.py index 1a5f829..3edd5a5 100644 --- a/likelihood/lecture/mlecoding.py +++ b/likelihood/lecture/mlecoding.py @@ -1,24 +1,22 @@ import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm +import matplotlib.gridspec as gridspec +from plotstyle import * -plt.xkcd() -fig = plt.figure( figsize=(6,6.8) ) rng = np.random.RandomState(4637281) lmarg=0.1 rmarg=0.1 -ax = fig.add_axes([lmarg, 0.75, 1.0-rmarg, 0.25]) -ax.spines['bottom'].set_position('zero') -ax.spines['left'].set_visible(False) -ax.spines['right'].set_visible(False) -ax.spines['top'].set_visible(False) -ax.xaxis.set_ticks_position('bottom') -ax.get_yaxis().set_visible(False) +fig = plt.figure(figsize=cm_size(figure_width, 2.8*figure_height)) +spec = gridspec.GridSpec(nrows=4, ncols=1, height_ratios=[4, 4, 1, 3], hspace=0.2, + **adjust_fs(fig, left=4.0)) +ax = fig.add_subplot(spec[0, 0]) ax.set_xlim(0.0, np.pi) ax.set_xticks(np.arange(0.125*np.pi, 1.*np.pi, 0.125*np.pi)) ax.set_xticklabels([]) ax.set_ylim(0.0, 3.5) +ax.yaxis.set_major_locator(plt.NullLocator()) ax.text(-0.2, 0.5*3.5, 'Activity', rotation='vertical', va='center') ax.annotate('Tuning curve', xy=(0.42*np.pi, 2.5), xycoords='data', @@ -31,55 +29,49 @@ ax.annotate('', arrowprops=dict(arrowstyle="->", relpos=(0.5,0.5), connectionstyle="angle3,angleA=80,angleB=90") ) ax.text(0.52*np.pi, 0.7, 'preferred\norientation') -ax.plot([0, 0], [0.0, 3.5], 'k', zorder=10, clip_on=False) xx = np.arange(0.0, 2.0*np.pi, 0.01) pp = 0.5*np.pi yy = np.exp(np.cos(2.0*(xx+pp))) -ax.fill_between(xx, yy+0.25*yy, yy-0.25*yy, color=cm.autumn(0.3, 1), alpha=0.5) -ax.plot(xx, yy, color=cm.autumn(0.0, 1)) +ax.fill_between(xx, yy+0.25*yy, yy-0.25*yy, **fsBa) +ax.plot(xx, yy, **lsB) -ax = fig.add_axes([lmarg, 0.34, 1.0-rmarg, 0.38]) -ax.spines['bottom'].set_position('zero') -ax.spines['left'].set_visible(False) -ax.spines['right'].set_visible(False) -ax.spines['top'].set_visible(False) -ax.xaxis.set_ticks_position('bottom') -ax.get_yaxis().set_visible(False) +ax = fig.add_subplot(spec[1, 0]) ax.set_xlim(0.0, np.pi) ax.set_xticks(np.arange(0.125*np.pi, 1.*np.pi, 0.125*np.pi)) ax.set_xticklabels([]) -ax.set_ylim(-1.5, 3.0) -ax.text(0.5*np.pi, -1.8, 'Orientation', ha='center') +ax.set_ylim(0.0, 3.0) +ax.yaxis.set_major_locator(plt.NullLocator()) ax.text(-0.2, 0.5*3.5, 'Activity', rotation='vertical', va='center') -ax.plot([0, 0], [0.0, 3.0], 'k', zorder=10, clip_on=False) xx = np.arange(0.0, 1.0*np.pi, 0.01) prefphases = np.arange(0.125*np.pi, 1.*np.pi, 0.125*np.pi) responses = [] xresponse = 0.475*np.pi -for pp in prefphases : +for pp, ls, ps in zip(prefphases, [lsE, lsC, lsD, lsB, lsD, lsC, lsE], + [psE, psC, psD, psB, psD, psC, psE]) : yy = np.exp(np.cos(2.0*(xx+pp))) - ax.plot(xx, yy, color=cm.autumn(2.0*np.abs(pp/np.pi-0.5), 1)) + #ax.plot(xx, yy, color=cm.autumn(2.0*np.abs(pp/np.pi-0.5), 1)) + ax.plot(xx, yy, **ls) y = np.exp(np.cos(2.0*(xresponse+pp))) responses.append(y + rng.randn()*0.25*y) - ax.plot(xresponse, y, '.', markersize=20, color=cm.autumn(2.0*np.abs(pp/np.pi-0.5), 1)) - r=0.3 - y=-0.8 - ax.plot([pp-0.5*r*np.cos(pp), pp+0.5*r*np.cos(pp)], [y-r*np.sin(pp), y+r*np.sin(pp)], 'k', lw=6) + ax.plot(xresponse, y, **ps) responses = np.array(responses) -ax = fig.add_axes([lmarg, 0.05, 1.0-rmarg, 0.22]) -ax.spines['left'].set_visible(False) -ax.spines['right'].set_visible(False) -ax.spines['top'].set_visible(False) -ax.xaxis.set_ticks_position('bottom') -ax.get_yaxis().set_visible(False) +ax = fig.add_subplot(spec[2, 0]) +ax.show_spines('') +r = 0.3 +ax.set_ylim(-1.1*r, 1.1*r) +for pp in prefphases: + ax.plot([pp-0.5*r*np.cos(pp), pp+0.5*r*np.cos(pp)], [-r*np.sin(pp), r*np.sin(pp)], + colors['black'], lw=6, clip_on=False) + +ax = fig.add_subplot(spec[3, 0]) ax.set_xlim(0.0, np.pi) ax.set_xticks(np.arange(0.125*np.pi, 1.*np.pi, 0.125*np.pi)) ax.set_xticklabels([]) ax.set_ylim(-1600, 0) +ax.yaxis.set_major_locator(plt.NullLocator()) ax.set_xlabel('Orientation') ax.text(-0.2, -800, 'Log-Likelihood', rotation='vertical', va='center') -ax.plot([0, 0], [-1600, 0], 'k', zorder=10, clip_on=False) phases = np.linspace(0.0, 1.1*np.pi, 100) probs = np.zeros((len(responses), len(phases))) for k, (pp, r) in enumerate(zip(prefphases, responses)) : @@ -95,7 +87,6 @@ ax.annotate('', arrowprops=dict(arrowstyle="->", relpos=(0.5,0.5), connectionstyle="angle3,angleA=80,angleB=90") ) ax.text(maxp+0.05, -1100, 'most likely\norientation\ngiven the responses') -ax.plot(phases, loglikelihood, '-b') +ax.plot(phases, loglikelihood, **lsA) plt.savefig('mlecoding.pdf') -#plt.show(); diff --git a/likelihood/lecture/mlemean.py b/likelihood/lecture/mlemean.py index 5fd4932..83b1bab 100644 --- a/likelihood/lecture/mlemean.py +++ b/likelihood/lecture/mlemean.py @@ -1,8 +1,11 @@ import numpy as np import matplotlib.pyplot as plt +import matplotlib.gridspec as gridspec +from plotstyle import * -plt.xkcd() -fig = plt.figure( figsize=(6,5) ) +fig = plt.figure(figsize=cm_size(figure_width, 1.8*figure_height)) +spec = gridspec.GridSpec(nrows=2, ncols=2, hspace=0.6, + **adjust_fs(fig, left=5.5)) # the data: n = 40 @@ -11,21 +14,17 @@ sigma = 0.5 rmu = 2.0 xd = rng.randn(n)*sigma+rmu # and possible pdfs: -x = np.arange( 0.0, 4.0, 0.01 ) +x = np.arange(0.0, 4.0, 0.01) mus = [1.5, 2.0, 2.5] g=np.zeros((len(x), len(mus))) for k, mu in enumerate(mus) : g[:,k] = np.exp(-0.5*((x-mu)/sigma)**2.0)/np.sqrt(2.0*np.pi)/sigma # plot it: -ax = fig.add_subplot( 2, 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 = fig.add_subplot(spec[0, :]) ax.set_xlim(0.5, 3.5) ax.set_ylim(-0.02, 0.85) -ax.set_xticks( np.arange(0, 5)) -ax.set_yticks( np.arange(0, 0.9, 0.2)) +ax.set_xticks(np.arange(0, 5)) +ax.set_yticks(np.arange(0, 0.9, 0.2)) ax.set_xlabel('x') ax.set_ylabel('Probability density') s = 1 @@ -36,14 +35,14 @@ for mu in mus : ax.annotate('', xy=(mu, 0.02), xycoords='data', xytext=(mu, 0.75), textcoords='data', arrowprops=dict(arrowstyle="->", relpos=(0.5,0.5), - connectionstyle=cs), zorder=1 ) + connectionstyle=cs), zorder=1) if mu > rmu : ax.text(mu-0.1, 0.04, '?', zorder=1, ha='right') else : ax.text(mu+0.1, 0.04, '?', zorder=1) -for k in range(len(mus)) : - ax.plot(x, g[:,k], zorder=5) -ax.scatter(xd, 0.05*rng.rand(len(xd))+0.2, s=30, zorder=10) +for k, ls in enumerate([lsCm, lsBm, lsDm]) : + ax.plot(x, g[:,k], zorder=5, **ls) +ax.plot(xd, 0.05*rng.rand(len(xd))+0.2, zorder=10, **psAm) # likelihood: thetas=np.arange(1.5, 2.6, 0.01) @@ -52,48 +51,38 @@ for i, theta in enumerate(thetas) : ps[:,i]=np.exp(-0.5*((xd-theta)/sigma)**2.0)/np.sqrt(2.0*np.pi)/sigma p=np.prod(ps,axis=0) # plot it: -ax = fig.add_subplot( 2, 2, 3 ) -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 = fig.add_subplot(spec[1, 0]) ax.set_xlabel(r'Parameter $\theta$') ax.set_ylabel('Likelihood') -ax.set_xticks( np.arange(1.6, 2.5, 0.4)) +ax.set_xticks(np.arange(1.6, 2.5, 0.4)) ax.annotate('Maximum', xy=(2.0, 5.5e-11), xycoords='data', xytext=(1.0, 1.1), textcoords='axes fraction', ha='right', arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5), - connectionstyle="angle3,angleA=10,angleB=70") ) + connectionstyle="angle3,angleA=10,angleB=70")) ax.annotate('', xy=(2.0, 0), xycoords='data', xytext=(2.0, 5e-11), textcoords='data', arrowprops=dict(arrowstyle="->", relpos=(0.5,0.5), - connectionstyle="angle3,angleA=90,angleB=80") ) -ax.plot(thetas,p) + connectionstyle="angle3,angleA=90,angleB=80")) +ax.plot(thetas, p, **lsAm) -ax = fig.add_subplot( 2, 2, 4 ) -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 = fig.add_subplot(spec[1, 1]) ax.set_xlabel(r'Parameter $\theta$') ax.set_ylabel('Log-Likelihood') ax.set_ylim(-50,-20) -ax.set_xticks( np.arange(1.6, 2.5, 0.4)) -ax.set_yticks( np.arange(-50, -19, 10.0)) +ax.set_xticks(np.arange(1.6, 2.5, 0.4)) +ax.set_yticks(np.arange(-50, -19, 10.0)) ax.annotate('Maximum', xy=(2.0, -23), xycoords='data', xytext=(1.0, 1.1), textcoords='axes fraction', ha='right', arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5), - connectionstyle="angle3,angleA=10,angleB=70") ) + connectionstyle="angle3,angleA=10,angleB=70")) ax.annotate('', xy=(2.0, -50), xycoords='data', xytext=(2.0, -26), textcoords='data', arrowprops=dict(arrowstyle="->", relpos=(0.5,0.5), - connectionstyle="angle3,angleA=80,angleB=100") ) -ax.plot(thetas,np.log(p)) + connectionstyle="angle3,angleA=80,angleB=100")) +ax.plot(thetas,np.log(p), **lsAm) -plt.tight_layout(); plt.savefig('mlemean.pdf') -#plt.show(); diff --git a/likelihood/lecture/mlepdf.py b/likelihood/lecture/mlepdf.py index 54ffd4c..ddbcb57 100644 --- a/likelihood/lecture/mlepdf.py +++ b/likelihood/lecture/mlepdf.py @@ -2,9 +2,10 @@ import numpy as np import scipy.stats as st import scipy.optimize as opt import matplotlib.pyplot as plt +from plotstyle import * -plt.xkcd() -fig = plt.figure( figsize=(6,3) ) +fig, (ax1, ax2) = plt.subplots(1, 2) +fig.subplots_adjust(**adjust_fs(fig, right=1.0)) # the data: n = 100 @@ -23,27 +24,23 @@ a = st.gamma.fit(xd, 5.0) yf = st.gamma.pdf(xx, *a) # plot it: -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, 10.0) -ax.set_ylim(0.0, 0.42) -ax.set_xticks( np.arange(0, 11, 2)) -ax.set_yticks( np.arange(0, 0.42, 0.1)) -ax.set_xlabel('x') -ax.set_ylabel('Probability density') -ax.plot(xx, yy, '-', lw=5, color='#ff0000', label='pdf') -ax.plot(xx, yf, '-', lw=2, color='#ffcc00', label='mle') +ax1.set_xlim(0, 10.0) +ax1.set_ylim(0.0, 0.42) +ax1.set_xticks(np.arange(0, 11, 2)) +ax1.set_yticks(np.arange(0, 0.42, 0.1)) +ax1.set_xlabel('x') +ax1.set_ylabel('Probability density') +ax1.plot(xx, yy, label='pdf', **lsB) +ax1.plot(xx, yf, label='mle', **lsCm) kernel = st.gaussian_kde(xd) x = kernel(xd) x /= np.max(x) -ax.scatter(xd, 0.05*x*(rng.rand(len(xd))-0.5)+0.05, s=30, zorder=10) -ax.legend(loc='upper right', frameon=False) +sigma = 0.07 +ax1.plot(xd, sigma*x*(rng.rand(len(xd))-0.5)+sigma, zorder=10, **psAm) +ax1.legend(loc='upper right') # histogram: -h,b = np.histogram(xd, np.arange(0, 8.5, 1), density=True) +h,b = np.histogram(xd, np.arange(0, 8.4, 0.5), density=True) # fit histogram: def gammapdf(x, n, l, s) : @@ -52,22 +49,15 @@ popt, pcov = opt.curve_fit(gammapdf, b[:-1]+0.5*(b[1]-b[0]), h) yc = st.gamma.pdf(xx, *popt) # plot it: -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, 10.0) -ax.set_xticks( np.arange(0, 11, 2)) -ax.set_xlabel('x') -ax.set_ylim(0.0, 0.42) -ax.set_yticks( np.arange(0, 0.42, 0.1)) -ax.set_ylabel('Probability density') -ax.plot(xx, yy, '-', lw=5, color='#ff0000', label='pdf') -ax.plot(xx, yc, '-', lw=2, color='#ffcc00', label='fit') -ax.bar(b[:-1], h, np.diff(b)) -ax.legend(loc='upper right', frameon=False) +ax2.set_xlim(0, 10.0) +ax2.set_xticks(np.arange(0, 11, 2)) +ax2.set_xlabel('x') +ax2.set_ylim(0.0, 0.42) +ax2.set_yticks(np.arange(0, 0.42, 0.1)) +ax2.set_ylabel('Probability density') +ax2.plot(xx, yy, label='pdf', **lsB) +ax2.plot(xx, yc, label='fit', **lsCm) +ax2.bar(b[:-1], h, np.diff(b), **fsA) +ax2.legend(loc='upper right') -plt.tight_layout(); plt.savefig('mlepdf.pdf') -#plt.show(); diff --git a/likelihood/lecture/mlepropline.py b/likelihood/lecture/mlepropline.py index c89310c..906a3f2 100644 --- a/likelihood/lecture/mlepropline.py +++ b/likelihood/lecture/mlepropline.py @@ -1,9 +1,14 @@ import numpy as np import scipy.stats as st import matplotlib.pyplot as plt +import matplotlib.gridspec as gridspec +from plotstyle import * -plt.xkcd() -fig = plt.figure(figsize=(6, 3)) +fig = plt.figure() +spec = gridspec.GridSpec(nrows=1, ncols=2, wspace=0.3, + **adjust_fs(fig, left=5.5)) +spec1 = gridspec.GridSpecFromSubplotSpec(1, 2, spec[0, 0], width_ratios=[3, 1], wspace=0.0) +spec2 = gridspec.GridSpecFromSubplotSpec(1, 2, spec[0, 1], width_ratios=[3, 1], wspace=0.0) # the line: slope = 2.0 @@ -20,71 +25,44 @@ slopef = np.sum(x*y)/np.sum(x*x) yf = slopef*xx # plot it: -ax = fig.add_axes([0.09, 0.02, 0.33, 0.9]) -ax.spines['left'].set_position('zero') -ax.spines['bottom'].set_position('zero') -ax.spines['right'].set_visible(False) -ax.spines['top'].set_visible(False) -ax.get_xaxis().set_tick_params(direction='inout', length=10, width=2) -ax.get_yaxis().set_tick_params(direction='inout', length=10, width=2) -ax.yaxis.set_ticks_position('left') -ax.xaxis.set_ticks_position('bottom') +ax = fig.add_subplot(spec1[0, 0]) ax.set_xticks(np.arange(0.0, 4.1)) ax.set_xlim(0.0, 4.2) ax.set_ylim(-4.0, 12.0) ax.set_xlabel('x') ax.set_ylabel('y') -ax.scatter(x, y, label='data', s=40, zorder=10) -ax.plot(xx, yy, 'r', lw=5.0, color='#ff0000', label='original', zorder=5) -ax.plot(xx, yf, '--', lw=1.0, color='#ffcc00', label='fit', zorder=7) -ax.legend(loc='upper left', bbox_to_anchor=(0.0, 1.15), frameon=False) +ax.plot(x, y, label='data', zorder=10, **psAm) +ax.plot(xx, yy, label='original', zorder=5, **lsB) +ax.plot(xx, yf, label='fit', zorder=7, **lsCm) +ax.legend(loc='upper left', bbox_to_anchor=(0.0, 1.15)) -ax = fig.add_axes([0.42, 0.02, 0.07, 0.9]) -ax.spines['left'].set_position('zero') -ax.spines['right'].set_visible(False) -ax.spines['top'].set_visible(False) -ax.spines['bottom'].set_visible(False) -ax.get_yaxis().set_tick_params(direction='inout', length=10, width=2) -ax.yaxis.set_ticks_position('left') +ax = fig.add_subplot(spec1[0, 1]) +ax.show_spines('l') ax.set_xticks([]) ax.set_ylim(-4.0, 12.0) ax.set_yticks([]) bins = np.arange(-4.0, 12.1, 0.75) -ax.hist(y, bins, orientation='horizontal', zorder=10) +ax.hist(y, bins, orientation='horizontal', zorder=10, **fsA) -ax = fig.add_axes([0.6, 0.02, 0.33, 0.9]) -ax.spines['left'].set_position('zero') -ax.spines['bottom'].set_position('zero') -ax.spines['right'].set_visible(False) -ax.spines['top'].set_visible(False) -ax.get_xaxis().set_tick_params(direction='inout', length=10, width=2) -ax.get_yaxis().set_tick_params(direction='inout', length=10, width=2) -ax.yaxis.set_ticks_position('left') -ax.xaxis.set_ticks_position('bottom') +ax = fig.add_subplot(spec2[0, 0]) ax.set_xticks(np.arange(0.0, 4.1)) ax.set_xlim(0.0, 4.2) ax.set_ylim(-4.0, 12.0) ax.set_xlabel('x') ax.set_ylabel('y - mx') -ax.scatter(x, y - slopef*x, label='residuals', s=40, zorder=10) -#ax.legend(loc='upper left', bbox_to_anchor=(0.0, 1.0), frameon=False) +ax.plot(x, y - slopef*x, label='residuals', zorder=10, **psAm) +#ax.legend(loc='upper left', bbox_to_anchor=(0.0, 1.0)) -ax = fig.add_axes([0.93, 0.02, 0.07, 0.9]) -ax.spines['left'].set_position('zero') -ax.spines['right'].set_visible(False) -ax.spines['top'].set_visible(False) -ax.spines['bottom'].set_visible(False) -ax.get_yaxis().set_tick_params(direction='inout', length=10, width=2) -ax.yaxis.set_ticks_position('left') +ax = fig.add_subplot(spec2[0, 1]) +ax.show_spines('l') ax.set_xlim(0.0, 11.0) ax.set_xticks([]) ax.set_ylim(-4.0, 12.0) ax.set_yticks([]) r = y - slopef*x -ax.hist(r, bins, orientation='horizontal', zorder=10) +ax.hist(r, bins, orientation='horizontal', zorder=10, **fsA) gx = np.arange(-4.0, 12.1, 0.1) gy = st.norm.pdf(gx, np.mean(r), np.std(r)) -ax.plot(1.0+gy*29.0, gx, 'r', lw=2, zorder=5) +ax.plot(1.0+gy*29.0, gx, zorder=5, **lsBm) plt.savefig('mlepropline.pdf') -#plt.show(); diff --git a/plotstyle.py b/plotstyle.py index f41fce7..8799339 100644 --- a/plotstyle.py +++ b/plotstyle.py @@ -13,19 +13,16 @@ ppi = 72.0 # colors: colors = {} -colors['red'] = '#CC0000' +colors['red'] = '#DD1000' colors['orange'] = '#FF9900' colors['lightorange'] = '#FFCC00' -colors['yellow'] = '#FFFF66' +colors['yellow'] = '#FFF720' colors['green'] = '#99FF00' colors['blue'] = '#0010CC' colors['gray'] = '#A7A7A7' colors['black'] = '#000000' colors['white'] = '#FFFFFF' -#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' @@ -36,11 +33,14 @@ mainline = {'linestyle': '-', 'linewidth': lwthick} minorline = {'linestyle': '-', 'linewidth': lwthin} largemarker = {'marker': 'o', 'markersize': 9, 'markeredgecolor': colors['white'], 'markeredgewidth': 1} smallmarker = {'marker': 'o', 'markersize': 6, 'markeredgecolor': colors['white'], 'markeredgewidth': 1} -filllw = 1.0 -fillalpha = 0.5 +largelinepoints = {'linestyle': '-', 'linewidth': lwthick, 'marker': 'o', 'markersize': 10, 'markeredgecolor': colors['white'], 'markeredgewidth': 1} +smalllinepoints = {'linestyle': '-', 'linewidth': lwthin, 'marker': 'o', 'markersize': 7, 'markeredgecolor': colors['white'], 'markeredgewidth': 1} +filllw = 1 +fillec = colors['white'] +fillalpha = 0.4 # helper lines: -lsSpine = {'c': colors['black'], 'linestyle': '-', 'linewidth': 1} +lsSpine = {'c': colors['black'], 'linestyle': '-', 'linewidth': 1, 'clip_on': False} lsGrid = {'c': colors['gray'], 'linestyle': '--', 'linewidth': 1} lsMarker = {'c': colors['black'], 'linestyle': '-', 'linewidth': 2} @@ -52,9 +52,13 @@ lsMarker = {'c': colors['black'], 'linestyle': '-', 'linewidth': 2} # - plain style with a thick/solid line (e.g. lsA), and # - minor style with a thinner or dashed line (e.g. lsAm). -# Point styles come in two variants: -# - plain style with large solid markers (e.g. psA), and -# - minor style with smaller markers (e.g. lsBm). +# Point (marker) styles come in two variants: +# - plain style with large solid markers (e.g. psB), and +# - minor style with smaller markers (e.g. psBm). + +# Linepoint styles (markers connected by lines) come in two variants: +# - plain style with large solid markers (e.g. lpsA), and +# - minor style with smaller markers (e.g. lpsAm). # Fill styles come in three variants: # - plain (e.g. fsB) for a solid fill color and a darker edge color, @@ -65,13 +69,19 @@ lsA = dict({'color': colors['blue']}, **mainline) lsAm = dict({'color': colors['blue']}, **minorline) psA = dict({'color': colors['blue'], 'linestyle': 'none'}, **largemarker) psAm = dict({'color': colors['blue'], 'linestyle': 'none'}, **smallmarker) +lpsA = dict({'color': colors['blue']}, **largelinepoints) +lpsAm = dict({'color': colors['blue']}, **smalllinepoints) +fsA = {'facecolor': colors['blue'], 'edgecolor': fillec, 'linewidth': filllw} +fsAs = {'facecolor': colors['blue'], 'edgecolor': 'none'} fsAa = {'facecolor': colors['blue'], 'edgecolor': 'none', 'alpha': fillalpha} lsB = dict({'color': colors['red']}, **mainline) lsBm = dict({'color': colors['red']}, **minorline) psB = dict({'color': colors['red'], 'linestyle': 'none'}, **largemarker) psBm = dict({'color': colors['red'], 'linestyle': 'none'}, **smallmarker) -fsB = {'facecolor': colors['red'], 'edgecolor': colors['black'], 'linewidth': filllw} +lpsB = dict({'color': colors['red']}, **largelinepoints) +lpsBm = dict({'color': colors['red']}, **smalllinepoints) +fsB = {'facecolor': colors['red'], 'edgecolor': fillec, 'linewidth': filllw} fsBs = {'facecolor': colors['red'], 'edgecolor': 'none'} fsBa = {'facecolor': colors['red'], 'edgecolor': 'none', 'alpha': fillalpha} @@ -79,17 +89,25 @@ lsC = dict({'color': colors['lightorange']}, **mainline) lsCm = dict({'color': colors['lightorange']}, **minorline) psC = dict({'color': colors['lightorange'], 'linestyle': 'none'}, **largemarker) psCm = dict({'color': colors['lightorange'], 'linestyle': 'none'}, **smallmarker) -fsC = {'facecolor': colors['lightorange'], 'edgecolor': colors['black'], 'linewidth': filllw} +fsC = {'facecolor': colors['lightorange'], 'edgecolor': fillec, 'linewidth': filllw} fsCs = {'facecolor': colors['lightorange'], 'edgecolor': 'none'} fsCa = {'facecolor': colors['lightorange'], 'edgecolor': 'none', 'alpha': fillalpha} -fsD = {'facecolor': colors['orange'], 'edgecolor': colors['black'], 'linewidth': filllw} +lsD = dict({'color': colors['orange']}, **mainline) +lsDm = dict({'color': colors['orange']}, **minorline) +psD = dict({'color': colors['orange'], 'linestyle': 'none'}, **largemarker) +psDm = dict({'color': colors['orange'], 'linestyle': 'none'}, **smallmarker) +fsD = {'facecolor': colors['orange'], 'edgecolor': fillec, 'linewidth': filllw} fsDs = {'facecolor': colors['orange'], 'edgecolor': 'none'} -fsE = {'facecolor': colors['yellow'], 'edgecolor': colors['black'], 'linewidth': filllw} +lsE = dict({'color': colors['yellow']}, **mainline) +lsEm = dict({'color': colors['yellow']}, **minorline) +psE = dict({'color': colors['yellow'], 'linestyle': 'none'}, **largemarker) +psEm = dict({'color': colors['yellow'], 'linestyle': 'none'}, **smallmarker) +fsE = {'facecolor': colors['yellow'], 'edgecolor': fillec, 'linewidth': filllw} fsEs = {'facecolor': colors['yellow'], 'edgecolor': 'none'} -fsF = {'facecolor': colors['green'], 'edgecolor': colors['black'], 'linewidth': filllw} +fsF = {'facecolor': colors['green'], 'edgecolor': fillec, 'linewidth': filllw} fsFs = {'facecolor': colors['green'], 'edgecolor': 'none'} # factor for scaling widths of bars in a bar plot: @@ -323,6 +341,7 @@ def common_format(): mpl.rcParams['grid.color'] = lsGrid['c'] mpl.rcParams['grid.linestyle'] = lsGrid['linestyle'] mpl.rcParams['grid.linewidth'] = lsGrid['linewidth'] + mpl.rcParams['legend.frameon'] = False mpl.rcParams['axes.facecolor'] = 'none' mpl.rcParams['axes.edgecolor'] = lsSpine['c'] mpl.rcParams['axes.linewidth'] = lsSpine['linewidth'] diff --git a/regression/lecture/cubicerrors.py b/regression/lecture/cubicerrors.py index 64a4671..e072d9e 100644 --- a/regression/lecture/cubicerrors.py +++ b/regression/lecture/cubicerrors.py @@ -16,11 +16,11 @@ def create_data(): def plot_data(ax, x, y, c): - ax.scatter(x, y, marker='o', color=colors['blue'], s=40, zorder=10) + ax.plot(x, y, zorder=10, **psAm) xx = np.linspace(2.1, 3.9, 100) - ax.plot(xx, c*xx**3.0, color=colors['red'], lw=2, zorder=5) + ax.plot(xx, c*xx**3.0, zorder=5, **lsBm) for cc in [0.25*c, 0.5*c, 2.0*c, 4.0*c]: - ax.plot(xx, cc*xx**3.0, color=colors['orange'], lw=1.5, zorder=5) + ax.plot(xx, cc*xx**3.0, zorder=5, **lsDm) ax.set_xlabel('Size x', 'm') ax.set_ylabel('Weight y', 'kg') ax.set_xlim(2, 4) @@ -42,15 +42,15 @@ def plot_data_errors(ax, x, y, c): xytext=(3.4, 70), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.9,1.0), connectionstyle="angle3,angleA=50,angleB=-30") ) - ax.scatter(x[:40], y[:40], color=colors['blue'], s=10, zorder=0) + ax.plot(x[:40], y[:40], zorder=0, **psAm) inxs = [3, 10, 11, 17, 18, 21, 28, 30, 33] - ax.scatter(x[inxs], y[inxs], color=colors['blue'], s=40, zorder=10) + ax.plot(x[inxs], y[inxs], zorder=10, **psA) xx = np.linspace(2.1, 3.9, 100) - ax.plot(xx, c*xx**3.0, color=colors['red'], lw=2) + ax.plot(xx, c*xx**3.0, **lsBm) for i in inxs : xx = [x[i], x[i]] yy = [c*x[i]**3.0, y[i]] - ax.plot(xx, yy, color=colors['orange'], lw=2, zorder=5) + ax.plot(xx, yy, zorder=5, **lsDm) def plot_error_hist(ax, x, y, c): ax.set_xlabel('Squared error') @@ -67,7 +67,7 @@ def plot_error_hist(ax, x, y, c): xytext=(800, 3), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.0,0.2), connectionstyle="angle3,angleA=10,angleB=90") ) - ax.hist(errors, bins, color=colors['orange']) + ax.hist(errors, bins, **fsC) diff --git a/regression/lecture/cubicfunc.py b/regression/lecture/cubicfunc.py index e08f21d..e8c0565 100644 --- a/regression/lecture/cubicfunc.py +++ b/regression/lecture/cubicfunc.py @@ -16,11 +16,11 @@ if __name__ == "__main__": fig, ax = plt.subplots(figsize=cm_size(figure_width, 1.4*figure_height)) fig.subplots_adjust(**adjust_fs(left=6.0, right=1.2)) - ax.scatter(x, y, marker='o', color=colors['blue'], s=40, zorder=10) + ax.plot(x, y, zorder=10, **psA) xx = np.linspace(2.1, 3.9, 100) - ax.plot(xx, c*xx**3.0, color=colors['red'], lw=3, zorder=5) + ax.plot(xx, c*xx**3.0, zorder=5, **lsB) for cc in [0.25*c, 0.5*c, 2.0*c, 4.0*c]: - ax.plot(xx, cc*xx**3.0, color=colors['orange'], lw=2, zorder=5) + ax.plot(xx, cc*xx**3.0, zorder=5, **lsDm) ax.set_xlabel('Size x', 'm') ax.set_ylabel('Weight y', 'kg') ax.set_xlim(2, 4) diff --git a/regression/lecture/cubicmse.py b/regression/lecture/cubicmse.py index 2520824..f687885 100644 --- a/regression/lecture/cubicmse.py +++ b/regression/lecture/cubicmse.py @@ -39,9 +39,9 @@ def plot_mse(ax, x, y, c, cs): for i, cc in enumerate(ccs): mses[i] = np.mean((y-(cc*x**3.0))**2.0) - ax.plot(ccs, mses, colors['blue'], lw=2, zorder=10) - ax.scatter(cs, ms, color=colors['red'], s=40, zorder=20) - ax.scatter(cs[-1], ms[-1], color=colors['orange'], s=60, zorder=30) + ax.plot(ccs, mses, zorder=10, **lsAm) + ax.plot(cs[:12], ms[:12], zorder=20, **psB) + ax.plot(cs[-1], ms[-1], zorder=30, **psC) for i in range(4): ax.annotate('', xy=(cs[i+1]+0.2, ms[i+1]), xycoords='data', @@ -56,12 +56,12 @@ def plot_mse(ax, x, y, c, cs): ax.set_yticks(np.arange(0, 30001, 10000)) def plot_descent(ax, cs, mses): - ax.plot(np.arange(len(mses))+1, mses, '-o', c=colors['red'], mew=0, ms=8) + ax.plot(np.arange(len(mses))+1, mses, **lpsBm) ax.set_xlabel('Iteration') #ax.set_ylabel('Mean squared error') - ax.set_xlim(0, 10.5) + ax.set_xlim(0, 12.5) ax.set_ylim(0, 25000) - ax.set_xticks(np.arange(0.0, 10.1, 2.0)) + ax.set_xticks(np.arange(0.0, 12.1, 2.0)) ax.set_yticks(np.arange(0, 30001, 10000)) ax.set_yticklabels([]) diff --git a/regression/lecture/derivative.py b/regression/lecture/derivative.py index a377109..62a9e39 100644 --- a/regression/lecture/derivative.py +++ b/regression/lecture/derivative.py @@ -4,8 +4,7 @@ from plotstyle import * plain_style() -fig = plt.figure( figsize=(2.5,3.4) ) -ax = fig.add_subplot(1, 1, 1) +fig, ax = plt.subplots(figsize=(2.5,3.4)) # parabula: x1 = -0.2 @@ -14,7 +13,7 @@ x = np.linspace(x1, x2, 200) y = x*x ax.set_xlim(x1, x2) ax.set_ylim(-0.2, 0.7) -ax.plot(x, y, c=colors['blue'], lw=4, zorder=0) +ax.plot(x, y, zorder=0, **lsA) # secant: x = np.asarray([0.1, 0.7]) y = x*x @@ -22,33 +21,33 @@ ax.set_xticks(x) ax.set_yticks(y) ax.set_xticklabels(['$x$','$x+\Delta x$']) ax.set_yticklabels(['','']) -ax.scatter(x, y, c=colors['red'], edgecolor='none', s=150, zorder=10) +ax.plot(x, y, zorder=10, **psB) # function values: -ax.plot([x[0], x[0], x1],[-0.2, y[0], y[0]], '--k', lw=1, zorder=6) -ax.plot([x[1], x[1], x1],[-0.2, y[1], y[1]], '--k', lw=1, zorder=6) +ax.plot([x[0], x[0], x1],[-0.2, y[0], y[0]], zorder=6, **lsGrid) +ax.plot([x[1], x[1], x1],[-0.2, y[1], y[1]], zorder=6, **lsGrid) ax.text(x1+0.05, y[0]+0.05, '$f(x)$', zorder=6) ax.text(x1+0.05, y[1]+0.05, '$f(x+\Delta x)$', zorder=6) # slope triangle: -ax.plot([x[0], x[1], x[1]],[y[0], y[0], y[1]], '-k', lw=2, zorder=7) -ax.text(np.mean(x), y[0]-0.08, '$\Delta x$', ha='center', zorder=7) +ax.plot([x[0], x[1], x[1]],[y[0], y[0], y[1]], zorder=7, **lsMarker) +ax.text(np.mean(x), y[0]-0.07, '$\Delta x$', ha='center', zorder=7) ax.text(x[1]+0.05, np.mean(y), '$f(x+\Delta x)-f(x)$', va='center', rotation='vertical', zorder=7) # secant line: m = np.diff(y)/np.diff(x) xl = [x1, x2] yl = m*(xl-x[0])+y[0] -ax.plot(xl, yl, c=colors['red'], lw=3, zorder=7) +ax.plot(xl, yl, zorder=7, **lsBm) # derivative: md = 2.0*x[0] yl = md*(xl-x[0])+y[0] -ax.plot(xl, yl, c=colors['yellow'], lw=3, zorder=5) +ax.plot(xl, yl, zorder=5, **lsDm) # limit: for ml in np.linspace(md, m, 5)[1:] : yl = ml*(xl-x[0])+y[0] xs = 0.5*(ml+np.sqrt(ml*ml-4.0*(ml*x[0]-y[0]))) - ax.scatter([xs], [xs*xs], c=colors['orange'], edgecolor='none', s=80, zorder=3) - ax.plot(xl, yl, c=colors['orange'], lw=2, zorder=3) + ax.plot([xs], [xs*xs], zorder=3, **psC) + ax.plot(xl, yl, zorder=3, **lsCm) fig.subplots_adjust(**adjust_fs(fig, 0.5, 0.5, 1.4, 0.5)) plt.savefig('derivative.pdf') diff --git a/regression/lecture/lin_regress.py b/regression/lecture/lin_regress.py index 443bab9..ae854d5 100644 --- a/regression/lecture/lin_regress.py +++ b/regression/lecture/lin_regress.py @@ -14,7 +14,7 @@ def create_data(): def plot_data(ax, x, y): - ax.scatter(x, y, marker='o', color=colors['blue'], s=40) + ax.plot(x, y, **psA) ax.set_xlabel('Input x') ax.set_ylabel('Output y') ax.set_xlim(0, 120) @@ -24,10 +24,10 @@ def plot_data(ax, x, y): def plot_data_slopes(ax, x, y, m, n): - ax.scatter(x, y, marker='o', color=colors['blue'], s=40) + ax.plot(x, y, **psA) xx = np.asarray([2, 118]) for i in np.linspace(0.3*m, 2.0*m, 5): - ax.plot(xx, i*xx+n, color=colors['red'], lw=2) + ax.plot(xx, i*xx+n, **lsBm) ax.set_xlabel('Input x') #ax.set_ylabel('Output y') ax.set_xlim(0, 120) @@ -37,10 +37,10 @@ def plot_data_slopes(ax, x, y, m, n): def plot_data_intercepts(ax, x, y, m, n): - ax.scatter(x, y, marker='o', color=colors['blue'], s=40) + ax.plot(x, y, **psA) xx = np.asarray([2, 118]) for i in np.linspace(n-1*n, n+1*n, 5): - ax.plot(xx, m*xx + i, color=colors['red'], lw=2) + ax.plot(xx, m*xx + i, **lsBm) ax.set_xlabel('Input x') #ax.set_ylabel('Output y') ax.set_xlim(0, 120) diff --git a/regression/lecture/linear_least_squares.py b/regression/lecture/linear_least_squares.py index 5b07e99..d36fd32 100644 --- a/regression/lecture/linear_least_squares.py +++ b/regression/lecture/linear_least_squares.py @@ -25,15 +25,15 @@ def plot_data(ax, x, y, m, n): xytext=(80, -50), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.9,1.0), connectionstyle="angle3,angleA=50,angleB=-30") ) - ax.scatter(x[:40], y[:40], color=colors['blue'], s=10, zorder=0) + ax.plot(x[:40], y[:40], zorder=0, **psAm) inxs = [3, 13, 16, 19, 25, 34, 36] - ax.scatter(x[inxs], y[inxs], color=colors['blue'], s=40, zorder=10) + ax.plot(x[inxs], y[inxs], zorder=10, **psA) xx = np.asarray([2, 118]) - ax.plot(xx, m*xx+n, color=colors['red'], lw=2) + ax.plot(xx, m*xx+n, **lsBm) for i in inxs : xx = [x[i], x[i]] yy = [m*x[i]+n, y[i]] - ax.plot(xx, yy, color=colors['orange'], lw=2, zorder=5) + ax.plot(xx, yy, zorder=5, **lsDm) def plot_error_hist(ax, x, y, m, n): @@ -51,7 +51,7 @@ def plot_error_hist(ax, x, y, m, n): xytext=(350, 20), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.0,0.2), connectionstyle="angle3,angleA=10,angleB=90") ) - ax.hist(errors, bins, color=colors['orange']) + ax.hist(errors, bins, **fsD) diff --git a/statistics/lecture/cumulative.py b/statistics/lecture/cumulative.py index 014805e..0cc0102 100644 --- a/statistics/lecture/cumulative.py +++ b/statistics/lecture/cumulative.py @@ -24,17 +24,17 @@ ax.set_ylim(-0.05, 1.05) 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], zorder=-5, **lsMarker) +ax.plot([-3.2, med, med], [0.5, 0.5, 0.0], zorder=-5, **lsSpine) 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], zorder=-5, **lsMarker) +ax.plot([-3.2, q3, q3], [0.75, 0.75, 0.0], zorder=-5, **lsSpine) 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], zorder=-5, **lsMarker) +ax.plot([-3.2, -1.0, -1.0], [p, p, 0.0], zorder=-5, **lsSpine) ax.text(-2.8, 0.2, 'F=%.2f' % p) ax.text(-0.9, 0.05, '-1') diff --git a/statistics/lecture/diehistograms.py b/statistics/lecture/diehistograms.py index 57283a1..637d420 100644 --- a/statistics/lecture/diehistograms.py +++ b/statistics/lecture/diehistograms.py @@ -25,6 +25,6 @@ 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) +ax2.plot([0.2, 6.8], [1.0/6.0, 1.0/6.0], zorder=-10, **lsAm) +ax2.hist([x2, x1], bins, normed=True, zorder=-5, **fs) fig.savefig('diehistograms.pdf') diff --git a/statistics/lecture/displayunivariatedata.py b/statistics/lecture/displayunivariatedata.py index da2c347..38b0df4 100644 --- a/statistics/lecture/displayunivariatedata.py +++ b/statistics/lecture/displayunivariatedata.py @@ -14,7 +14,7 @@ scatterpos = 1.0 barpos = 2.5 boxpos = 4.0 -fig = plt.figure(figsize=cm_size(figure_width, 1.2*figure_height)) +fig = plt.figure(figsize=cm_size(figure_width, 1.1*figure_height)) spec = gridspec.GridSpec(nrows=1, ncols=2, width_ratios=[3, 1], wspace=0.1, **adjust_fs(fig, left=4.0)) @@ -53,7 +53,7 @@ ax.set_xticklabels([]) 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_xticklabels(['(1) data', '(2) bar\n plot', '(3) box-\nwhisker'], fontsize='medium') ax.set_ylabel('x') ax.set_ylim( 0.0, 8.0) @@ -85,7 +85,7 @@ 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)) -ax.set_xlabel('(4) p(x)') +ax.set_xlabel('(4) pdf') bw = 0.75 bins = np.arange(0, 8.0+bw, bw) h, b = np.histogram(data, bins) diff --git a/statistics/lecture/kerneldensity.py b/statistics/lecture/kerneldensity.py index 19e06b9..14f6147 100644 --- a/statistics/lecture/kerneldensity.py +++ b/statistics/lecture/kerneldensity.py @@ -60,7 +60,7 @@ ax = fig.add_subplot(spec[:, 1]) 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_ylabel('Prob. density p(x)') ax.set_ylim(0.0, 0.49) ax.set_yticks(np.arange(0.0, 0.41, 0.1)) kd, xx = kerneldensity(r, -3.2, 3.2, 0.2) diff --git a/statistics/lecture/statistics.tex b/statistics/lecture/statistics.tex index 59a3c0f..e6945d2 100644 --- a/statistics/lecture/statistics.tex +++ b/statistics/lecture/statistics.tex @@ -115,17 +115,9 @@ function \mcode{median()} computes the median. writing reliable code! \end{exercise} -\begin{figure}[t] - \includegraphics[width=1\textwidth]{quartile} - \titlecaption{\label{quartilefig} Median and quartiles of a normal - distribution.}{ The interquartile range between the first and the - third quartile contains 50\,\% of the data and contains the - median.} -\end{figure} - The distribution of data can be further characterized by the position of its \entermde[quartile]{Quartil}{quartiles}. Neighboring quartiles are -separated by 25\,\% of the data (\figref{quartilefig}). +separated by 25\,\% of the data.% (\figref{quartilefig}). \entermde[percentile]{Perzentil}{Percentiles} allow to characterize the distribution of the data in more detail. The 3$^{\rm rd}$ quartile corresponds to the 75$^{\rm th}$ percentile, because 75\,\% of the @@ -156,15 +148,13 @@ median that extends from the 1$^{\rm st}$ to the 3$^{\rm rd}$ quartile. The whiskers mark the minimum and maximum value of the data set (\figref{displayunivariatedatafig} (3)). -\begin{exercise}{univariatedata.m}{} - Generate 40 normally distributed random numbers with a mean of 2 and - illustrate their distribution in a box-whisker plot - (\code{boxplot()} function), with a bar and errorbar illustrating - the mean and standard deviation (\code{bar()}, \code{errorbar()}), - and the data themselves jittered randomly (as in - \figref{displayunivariatedatafig}). How to interpret the different - plots? -\end{exercise} +% \begin{figure}[t] +% \includegraphics[width=1\textwidth]{quartile} +% \titlecaption{\label{quartilefig} Median and quartiles of a normal +% distribution.}{ The interquartile range between the first and the +% third quartile contains 50\,\% of the data and contains the +% median.} +% \end{figure} % \begin{exercise}{boxwhisker.m}{} % Generate a $40 \times 10$ matrix of random numbers and @@ -201,6 +191,16 @@ Histograms are often used to estimate the \enterm[probability!distribution]{probability distribution} (\determ[Wahrscheinlichkeits!-verteilung]{Wahrscheinlichkeitsverteilung}) of the data values. +\begin{exercise}{univariatedata.m}{} + Generate 40 normally distributed random numbers with a mean of 2 and + illustrate their distribution in a box-whisker plot + (\code{boxplot()} function), with a bar and errorbar illustrating + the mean and standard deviation (\code{bar()}, \code{errorbar()}), + and the data themselves jittered randomly (as in + \figref{displayunivariatedatafig}). How to interpret the different + plots? +\end{exercise} + \subsection{Probabilities} In the frequentist interpretation of probability, the \enterm{probability} (\determ{Wahrscheinlichkeit}) of an event @@ -252,7 +252,7 @@ real number like, e.g., 0.123456789 is zero, because there are uncountable many real numbers. We can only ask for the probability to get a measurement value in some -range. For example, we can ask for the probability $P(1.2 Date: Tue, 14 Jan 2020 23:45:18 +0100 Subject: [PATCH 7/7] [simulations] updated plots --- plotstyle.py | 2 +- simulations/lecture/normaldata.py | 6 +++--- simulations/lecture/randomnumbers.py | 12 ++++-------- simulations/lecture/staticnonlinearity.py | 8 ++++---- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/plotstyle.py b/plotstyle.py index 8799339..328565a 100644 --- a/plotstyle.py +++ b/plotstyle.py @@ -34,7 +34,7 @@ minorline = {'linestyle': '-', 'linewidth': lwthin} largemarker = {'marker': 'o', 'markersize': 9, 'markeredgecolor': colors['white'], 'markeredgewidth': 1} smallmarker = {'marker': 'o', 'markersize': 6, 'markeredgecolor': colors['white'], 'markeredgewidth': 1} largelinepoints = {'linestyle': '-', 'linewidth': lwthick, 'marker': 'o', 'markersize': 10, 'markeredgecolor': colors['white'], 'markeredgewidth': 1} -smalllinepoints = {'linestyle': '-', 'linewidth': lwthin, 'marker': 'o', 'markersize': 7, 'markeredgecolor': colors['white'], 'markeredgewidth': 1} +smalllinepoints = {'linestyle': '-', 'linewidth': 1.4, 'marker': 'o', 'markersize': 7, 'markeredgecolor': colors['white'], 'markeredgewidth': 1} filllw = 1 fillec = colors['white'] fillalpha = 0.4 diff --git a/simulations/lecture/normaldata.py b/simulations/lecture/normaldata.py index 4721ec8..b259a3b 100644 --- a/simulations/lecture/normaldata.py +++ b/simulations/lecture/normaldata.py @@ -19,7 +19,7 @@ if __name__ == "__main__": 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]) - ax1.scatter(indices, data, c=colors['blue'], edgecolor='white', s=50) + ax1.plot(indices, data, **psAm) ax1.set_xlabel('Index') ax1.set_ylabel('Weight', 'kg') ax1.set_xlim(-10, 310) @@ -29,10 +29,10 @@ if __name__ == "__main__": ax2 = fig.add_subplot(spec[0, 1]) 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) + ax2.plot(yy, xx, **lsBm) 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.barh(b[:-1], h/np.sum(h)/(b[1]-b[0]), height=bar_fac*bw, align='edge', **fsC) ax2.set_xlabel('Pdf', '1/kg') ax2.set_xlim(0, 0.012) ax2.set_xticks([0, 0.005, 0.01]) diff --git a/simulations/lecture/randomnumbers.py b/simulations/lecture/randomnumbers.py index 785e032..cf85f39 100644 --- a/simulations/lecture/randomnumbers.py +++ b/simulations/lecture/randomnumbers.py @@ -24,32 +24,28 @@ if __name__ == "__main__": spec = gridspec.GridSpec(nrows=2, ncols=2, **adjust_fs(fig)) ax = fig.add_subplot(spec[0, 0]) - ax.plot(indices, x1, c=colors['blue'], lw=1, zorder=10) - ax.scatter(indices, x1, c=colors['blue'], edgecolor='white', s=50, zorder=20) + ax.plot(indices, x1, zorder=10, **lpsAm) ax.set_xlabel('Index') ax.set_ylabel('Random number') ax.set_xlim(-1.0, n+1.0) ax.set_ylim(-0.05, 1.05) ax = fig.add_subplot(spec[0, 1]) - ax.plot(indices, x2, c=colors['blue'], lw=1, zorder=10) - ax.scatter(indices, x2, c=colors['blue'], edgecolor='white', s=50, zorder=20) + ax.plot(indices, x2, zorder=10, **lpsAm) ax.set_xlabel('Index') ax.set_ylabel('Random number') ax.set_xlim(-1.0, n+1.0) ax.set_ylim(-0.05, 1.05) ax = fig.add_subplot(spec[1, 1]) - ax.plot(indices, x3, c=colors['blue'], lw=1, zorder=10) - ax.scatter(indices, x3, c=colors['blue'], edgecolor='white', s=50, zorder=20) + ax.plot(indices, x3, zorder=10, **lpsAm) ax.set_xlabel('Index') ax.set_ylabel('Random number') ax.set_xlim(-1.0, n+1.0) ax.set_ylim(-0.05, 1.05) ax = fig.add_subplot(spec[1, 0]) - ax.plot(lags, corrs, c=colors['red'], lw=1, zorder=10) - ax.scatter(lags, corrs, c=colors['red'], edgecolor='white', s=50, zorder=20) + ax.plot(lags, corrs, zorder=10, **lpsBm) ax.set_xlabel('Lag') ax.set_ylabel('Serial correlation') ax.set_xlim(-maxl-0.5, maxl+0.5) diff --git a/simulations/lecture/staticnonlinearity.py b/simulations/lecture/staticnonlinearity.py index 8b36fe7..6cfde3d 100644 --- a/simulations/lecture/staticnonlinearity.py +++ b/simulations/lecture/staticnonlinearity.py @@ -23,8 +23,8 @@ if __name__ == "__main__": fig = plt.figure() spec = gridspec.GridSpec(nrows=1, ncols=2, **adjust_fs(fig, left=4.5)) ax1 = fig.add_subplot(spec[0, 0]) - ax1.plot(xx, yy, **ls['A1']) - ax1.scatter(x, y, c=colors['blue'], edgecolor='white', s=50) + ax1.plot(x, y, **psAm) + ax1.plot(xx, yy, **lsB) ax1.set_xlabel('Hair deflection', 'nm') ax1.set_ylabel('Conductance', 'nS') ax1.set_xlim(-20, 20) @@ -35,10 +35,10 @@ if __name__ == "__main__": ax2 = fig.add_subplot(spec[0, 1]) xg = np.linspace(-3.0, 3.01, 200) yg = st.norm.pdf(xg, 0.0, sigma) - ax2.plot(xg, yg, **ls['A1']) + ax2.plot(xg, yg, **lsB) bw = 0.25 h, b = np.histogram(y-boltzmann(x, x0, k), np.arange(-3.0, 3.01, bw)) - ax2.bar(b[:-1], h/np.sum(h)/(b[1]-b[0]), fc=colors['yellow'], width=bar_fac*bw, align='edge') + ax2.bar(b[:-1], h/np.sum(h)/(b[1]-b[0]), width=bar_fac*bw, align='edge', **fsC) ax2.set_xlabel('Residuals', 'nS') ax2.set_ylabel('Pdf', '1/nS') ax2.set_xlim(-2.5, 2.5)