我正在尝试绘制 Bland-Altman 图。
import matplotlib.pyplot as plt
import numpy as np
def bland_altman_plot(data1, data2, *args, **kwargs):
data1 = np.asarray(data1)
data2 = np.asarray(data2)
mean = np.mean([data1, data2], axis=0)
diff = data1 - data2 # Difference between data1 and data2
md = np.mean(diff) # Mean of the difference
sd = np.std(diff, axis=0) # Standard deviation of the difference
plt.scatter(mean, diff, *args, **kwargs)
plt.axhline(md, color='gray', linestyle='--')
plt.axhline(md + 1.96*sd, color='gray', linestyle='--')
plt.axhline(md - 1.96*sd, color='gray', linestyle='--')
from numpy.random import random
bland_altman_plot(random(10), random(10))
plt.title('Bland-Altman Plot')
plt.show()
上面的代码产生以下结果:
我正尝试绘制置信区间边界,如下图所示。假设顶行的置信区间坐标为:
左(0.3,0.5),(0.3,0.4)和右(0.9,0.5),(0.9,0,4)
最重要的是:
左(0.3,0.7),(0.3,0.5)和右(0.9,0.7),(0.9,0.5)
但是我不知道如何在 matplotlib 中绘制它。如果有人可以帮助我,那就太好了。
答案 0 :(得分:2)
我认为您想要errorbar
:
def bland_altman_plot(data1, data2, *args, **kwargs):
data1 = np.asarray(data1)
data2 = np.asarray(data2)
mean = np.mean([data1, data2], axis=0)
diff = data1 - data2 # Difference between data1 and data2
md = np.mean(diff) # Mean of the difference
sd = np.std(diff, axis=0) # Standard deviation of the difference
plt.scatter(mean, diff, *args, **kwargs)
plt.axhline(md, color='gray', linestyle='--')
plt.axhline(md + 1.96*sd, color='gray', linestyle='--')
plt.axhline(md - 1.96*sd, color='gray', linestyle='--')
# set up params for ci_loa
# change these accordingly
ci_loa_height = np.std(mean)
ci_loa_x = mean.min(), mean.max()
# draw the errorbar/ci intervals
# change color and capsize as wished
plt.errorbar(ci_loa_x, [md + 1.96*sd]*2,
yerr=ci_loa_height, fmt='none',
capsize=10, c='r')
plt.errorbar(ci_loa_x, [md - 1.96*sd]*2,
yerr=ci_loa_height, fmt='none',
capsize=10, c='r')
plt.title('Bland-Altman Plot')
plt.show()
然后:
np.random.seed(10)
bland_altman_plot(np.random.random(10), np.random.random(10))
产生: