[statistics] simplified plots, part 2

This commit is contained in:
Jan Benda 2020-01-14 17:52:27 +01:00
parent d1c8112030
commit 999a3003a6
5 changed files with 70 additions and 101 deletions

View File

@ -29,8 +29,8 @@ colors['black'] = '#000000'
#colors_bendalab_vivid['blue'] = '#0020C0' #colors_bendalab_vivid['blue'] = '#0020C0'
# line styles for plot(): # line styles for plot():
lwthick = 4.0 lwthick = 3.0
lwthin = 2.0 lwthin = 1.8
fillalpha = 0.5 fillalpha = 0.5
# helper lines: # helper lines:

View File

@ -1,19 +1,14 @@
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from plotstyle import *
rng = np.random.RandomState(981) rng = np.random.RandomState(981)
x = rng.randn( 40, 10 ) x = rng.randn( 40, 10 )
plt.xkcd() fig, ax = plt.subplots()
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('Experiment') ax.set_xlabel('Experiment')
ax.set_ylabel('x') ax.set_ylabel('x')
ax.set_ylim( -4.0, 4.0) ax.set_ylim(-4.0, 4.0)
ax.annotate('Median', ax.annotate('Median',
xy=(3.9, 0.0), xycoords='data', xy=(3.9, 0.0), xycoords='data',
xytext=(3.5, -2.7), textcoords='data', ha='right', 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', xytext=(4.9, 3.5), textcoords='data', ha='right',
arrowprops=dict(arrowstyle="->", relpos=(1.0,0.5), arrowprops=dict(arrowstyle="->", relpos=(1.0,0.5),
connectionstyle="angle3,angleA=0,angleB=120") ) connectionstyle="angle3,angleA=0,angleB=120") )
ax.boxplot( x, whis=100.0 ) ax.boxplot(x, whis=100.0)
plt.tight_layout()
plt.savefig('boxwhisker.pdf') plt.savefig('boxwhisker.pdf')
#plt.show()

View File

@ -1,33 +1,30 @@
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from plotstyle import *
plt.xkcd() fig = plt.figure(figsize=cm_size(figure_width, 1.5*figure_height))
fig = plt.figure( figsize=(6,4.6) ) 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) rng = np.random.RandomState(2981)
n = 200 n = 200
for k, r in enumerate( [ 1.0, 0.6, 0.0, -0.9 ] ) : for k, r in enumerate([ 1.0, 0.6, 0.0, -0.9 ]) :
x = rng.randn( n ) x = rng.randn(n)
y = r*x + np.sqrt(1.0-r*r)*rng.randn( n ) y = r*x + np.sqrt(1.0-r*r)*rng.randn(n)
ax = fig.add_subplot( 2, 2, k+1 ) ax = fig.add_subplot(spec[k//2, k%2])
ax.spines['right'].set_visible(False) ax.text(-2, 2.5, 'r=%.1f' % r)
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 )
if k == 0 : 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 : 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 : 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 : 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_xlabel('x')
ax.set_ylabel('y') ax.set_ylabel('y')
ax.set_xlim( -3.0, 3.0) ax.set_xlim(-3.0, 3.0)
ax.set_ylim( -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.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.savefig('correlation.pdf')
#plt.show()

View File

@ -1,9 +1,11 @@
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from plotstyle import *
# normal distribution: # normal distribution:
rng = np.random.RandomState(6281) 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) g = np.exp(-0.5*x*x)/np.sqrt(2.0*np.pi)
r = rng.randn(100) r = rng.randn(100)
@ -29,55 +31,41 @@ def kerneldensity(data, xmin, xmax, sigma=1.0) :
kd[k0:k1] += gauss[g0:g1] kd[k0:k1] += gauss[g0:g1]
kd /= len(data) kd /= len(data)
return kd, x 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(spec[0, 0])
ax = fig.add_subplot(2, 2, 1) ax.set_xlabel('x')
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_xlim(-3.2, 3.2)
ax.set_xticks( np.arange( -3.0, 3.1, 1.0 ) ) ax.set_xticks(np.arange(-3.0, 3.1, 1.0))
ax.set_ylabel( 'p(x)' ) ax.set_ylabel('p(x)')
ax.set_ylim(0.0, 0.49) 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.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 = fig.add_subplot(spec[1, 0])
ax.spines['right'].set_visible(False) ax.set_xlabel('x')
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_xlim(-3.2, 3.2)
ax.set_xticks( np.arange( -3.0, 3.1, 1.0 ) ) ax.set_xticks(np.arange(-3.0, 3.1, 1.0))
ax.set_ylabel( 'p(x)' ) ax.set_ylabel('p(x)')
ax.set_ylim(0.0, 0.49) 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.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 = fig.add_subplot(spec[:, 1])
ax.spines['right'].set_visible(False) ax.set_xlabel('x')
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_xlim(-3.2, 3.2)
ax.set_xticks( np.arange( -3.0, 3.1, 1.0 ) ) ax.set_xticks(np.arange(-3.0, 3.1, 1.0))
ax.set_ylabel( 'Probab. density p(x)' ) ax.set_ylabel('Probab. density p(x)')
ax.set_ylim(0.0, 0.49) 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) kd, xx = kerneldensity(r, -3.2, 3.2, 0.2)
ax.fill_between(xx, 0.0, kd, color='#FF9900', zorder=-5) ax.fill_between(xx, 0.0, kd, zorder=-5, **fsDs)
ax.plot(xx, kd, '-', lw=3, color='#CC0000', zorder=-1) 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')
fig.savefig( 'kerneldensity.pdf' )
#plt.show()

View File

@ -1,42 +1,33 @@
import numpy as np import numpy as np
import matplotlib.pyplot as plt 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 n = 200
x = np.random.randn( n ) rng = np.random.RandomState(3981)
y = np.random.randn( n ) x = rng.randn(n)
y = rng.randn(n)
z = x*x+0.2*y z = x*x+0.2*y
r =np.corrcoef(x,z)[0,1] r =np.corrcoef(x,z)[0,1]
ax = fig.add_subplot( 1, 2, 1 ) ax1.text(0, 4.0, 'r=%.1f' % r, ha='center')
ax.spines['right'].set_visible(False) ax1.text(0, 5.6, r'$y = x^2+\xi/5$', ha='center')
ax.spines['top'].set_visible(False) ax1.set_xlabel('x')
ax.yaxis.set_ticks_position('left') ax1.set_ylabel('y')
ax.xaxis.set_ticks_position('bottom') ax1.set_xlim(-3.0, 3.0)
ax.text( 0, 4.0, 'r=%.1f' % r, ha='center' ) ax1.set_ylim(-0.5, 6.0)
ax.text( 0, 6, r'$y = x^2+\xi/5$', ha='center' ) ax1.scatter(x, z)
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 )
z = 0.5*x*y z = 0.5*x*y
r =np.corrcoef(x,z)[0,1] r =np.corrcoef(x,z)[0,1]
ax = fig.add_subplot( 1, 2, 2 ) ax2.text(0, 1.5, 'r=%.1f' % r, ha='center')
ax.spines['right'].set_visible(False) ax2.text(0, 2.8, r'$y = x \cdot \xi/2$', ha='center')
ax.spines['top'].set_visible(False) ax2.set_xlabel('x')
ax.yaxis.set_ticks_position('left') ax2.set_ylabel('y')
ax.xaxis.set_ticks_position('bottom') ax2.set_xlim(-3.0, 3.0)
ax.text( 0, 1.5, 'r=%.1f' % r, ha='center' ) ax2.set_ylim(-3.0, 3.0)
ax.text( 0, 3, r'$y = x \cdot \xi/2$', ha='center' ) ax2.scatter(x, z)
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 )
plt.tight_layout()
plt.savefig('nonlincorrelation.pdf') plt.savefig('nonlincorrelation.pdf')
#plt.show()