I've used scipy's linregress function to fit lines through the points in the four panels below. The function outputs a p-value, shown on each panel. The p-values are large for panels a and b (I can't reject the null hypothesis that the slope is zero) and small for panels c and d (I can reject the null hypothesis.
I am not happy with this. I want panels a and c to pass, and panels b and d to fail the test (i.e. the spread of the points relative to the line should determine whether the test passes or fails, irrespective of what the slope is). I am looking for a function that will output a p-value, which I can then use to determine whether the test passes or fails (depending on whether it's below or greater than say 0.05 or 0.01).
Find the code below the figure...
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import linregress
plt.figure(figsize=(7, 7))
ax1 = plt.subplot2grid((2, 2), (0, 0))
ax2 = plt.subplot2grid((2, 2), (0, 1))
ax3 = plt.subplot2grid((2, 2), (1, 0))
ax4 = plt.subplot2grid((2, 2), (1, 1))
# axes 1
x = np.random.randn(40)
y = np.random.randn(40)*0.1
ax1.scatter(x, y, c='k')
slope, intercept, r_value, p_value, std_err = linregress(x, y)
print p_value
ax1.plot(x, intercept + slope*x, 'r')
ax1.text(0.65, 0.1, 'p = ' + str(np.round(p_value, 3)), transform=ax1.transAxes)
ax1.set_title('(a)')
# axes 2
x = np.random.randn(40)
y = np.random.randn(40)*0.8
ax2.scatter(x, y, c='k')
slope, intercept, r_value, p_value, std_err = linregress(x, y)
print p_value
ax2.plot(x, intercept + slope*x, 'r')
ax2.text(0.65, 0.1, 'p = ' + str(np.round(p_value, 3)), transform=ax2.transAxes)
ax2.set_title('(b)')
# axes 3
x = np.random.randn(40)
y = x + np.random.randn(40)*0.1
ax3.scatter(x, y, c='k')
slope, intercept, r_value, p_value, std_err = linregress(x, y)
print p_value
ax3.plot(x, intercept + slope*x, 'r')
ax3.text(0.65, 0.1, 'p = ' + str(np.round(p_value, 3)), transform=ax3.transAxes)
ax3.set_title('(c)')
# axes 4
x = np.random.randn(40)
y = x + np.random.randn(40)*0.8
ax4.scatter(x, y, c='k')
slope, intercept, r_value, p_value, std_err = linregress(x, y)
print p_value
ax4.plot(x, intercept + slope*x, 'r')
ax4.text(0.65, 0.1, 'p = ' + str(np.round(p_value, 3)), transform=ax4.transAxes)
ax4.set_title('(d)')
ax1.axis([-2, 2, -2, 2])
ax2.axis([-2, 2, -2, 2])
ax3.axis([-2, 2, -2, 2])
ax4.axis([-2, 2, -2, 2])
plt.tight_layout()
plt.ion(); plt.show()