Skip to content

Instantly share code, notes, and snippets.

@cwindolf
Created February 6, 2022 22:07
Show Gist options
  • Save cwindolf/872012d6594051fb0894c1d7a1480a47 to your computer and use it in GitHub Desktop.
Save cwindolf/872012d6594051fb0894c1d7a1480a47 to your computer and use it in GitHub Desktop.
def regline(x, y, ax=None, **kwargs):
b = ((x - x.mean()) * (y - y.mean())).sum() / np.square(x - x.mean()).sum()
a = y.mean() - b * x.mean()
x0, x1 = ax.get_xlim()
r = np.corrcoef(x, y)[0, 1]
ax.plot([x0, x1], [a + b * x0, a + b * x1], lw=1)
ax.text(
0.1,
0.9,
f"$\\rho={r:.2f}$",
horizontalalignment="left",
verticalalignment="center",
transform=ax.transAxes,
fontsize=6,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment