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/correlation.py

31 lines
1.1 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from plotstyle import *
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
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.8, 'positively\ncorrelated', ha='right', va='bottom')
elif k == 1 :
ax.text(2.8, -2.8, 'weakly\ncorrelated', ha='right', va='bottom')
elif k == 2 :
ax.text(2.8, -2.8, 'not\ncorrelated', ha='right', va='bottom')
elif k == 3 :
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.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')