Last active
July 8, 2017 12:29
-
-
Save liob/93f7bfefd660e3ba3c6bea2eab290b22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import numpy as np | |
from matplotlib import pyplot as plt | |
from matplotlib.transforms import blended_transform_factory | |
def bland_altman(x, y, axis=None, sd_offset=1.96, | |
n_format='{0:.2f}', *args, **kwargs): | |
if not axis: | |
axis = plt.gca() | |
mean = np.mean([x, y], axis=0) | |
diff = x - y | |
md = np.mean(diff) | |
sd = np.std(diff, axis=0) | |
axis.scatter(mean, diff, *args, **kwargs) | |
axis.axhline(md, linestyle='--', *args, **kwargs) | |
axis.axhline(md + sd_offset * sd, linestyle='--', *args, **kwargs) | |
axis.axhline(md - sd_offset * sd, linestyle='--', *args, **kwargs) | |
tform = blended_transform_factory(axis.transAxes, axis.transData) | |
axis.annotate('MEAN\n' + n_format.format(md), xy=(0, 0), | |
xycoords=tform, xytext=(0.98, md), | |
textcoords=tform, ha='right', va='center', *args, **kwargs) | |
axis.annotate(('+%sSD\n' % sd_offset) + n_format.format(md + sd_offset * sd), | |
xy=(0, 0), xycoords=tform, xytext=(0.98, md + sd_offset * sd), | |
textcoords=tform, ha='right', va='center', *args, **kwargs) | |
axis.annotate(('-%sSD\n' % sd_offset) + n_format.format(md - sd_offset * sd), | |
xy=(0, 0), xycoords=tform, xytext=(0.98, md - sd_offset * sd), | |
textcoords=tform, ha='right', va='center', *args, **kwargs) | |
if __name__ == "__main__": | |
x = np.random.randn(100) | |
y = np.random.randn(100) | |
bland_altman(x, y, color='black') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment