This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/statistics/lecture/nonlincorrelation.py

43 lines
1.0 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
plt.xkcd()
fig = plt.figure( figsize=(6,2.2) )
n = 200
x = np.random.randn( n )
y = np.random.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, 5.5, 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 )
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, 2.5, 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 )
plt.tight_layout()
plt.savefig('nonlincorrelation.pdf')
#plt.show()