From 999a3003a6580bc4bf805f52943d32ec739af614 Mon Sep 17 00:00:00 2001 From: Jan Benda Date: Tue, 14 Jan 2020 17:52:27 +0100 Subject: [PATCH] [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()