Bland-Altman plot in Python

Is it possible to make a Bland-Altman plot in Python? I can't seem to find anything.

Another name for this type of plot is Tuki's difference plot.

Example:

enter image description here

+10
source share
3 answers

If I understand the conspiracy theory correctly, this code should provide the basic plotting, while you can configure it for your own needs.

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='--') 

The corresponding elements in data1 and data2 are used to calculate the coordinates for the plotted points.

Then you can create a schedule by doing, for example,

 from numpy.random import random bland_altman_plot(random(10), random(10)) plt.title('Bland-Altman Plot') plt.show() 

Bland-altman plot

+23
source

Maybe I missed something, but it seems pretty simple:

 from numpy.random import random import matplotlib.pyplot as plt x = random(25) y = random(25) plt.title("FooBar") plt.scatter(x,y) plt.axhline(y=0.5,linestyle='--') plt.show() 

Here I just create random data between 0 and 1, and I randomly put the horizontal line at y = 0.5, but you can put as much as you want, wherever you want.

0
source

Now this is implemented in statsmodels: https://www.statsmodels.org/devel/generated/statsmodels.graphics.agreement.mean_diff_plot.html.

Here is an example of them:

 import statsmodels.api as sm import numpy as np import matplotlib.pyplot as plt # Seed the random number generator. # This ensures that the results below are reproducible. np.random.seed(9999) m1 = np.random.random(20) m2 = np.random.random(20) f, ax = plt.subplots(1, figsize = (8,5)) sm.graphics.mean_diff_plot(m1, m2, ax = ax) plt.show() 

which produces this:

enter image description here

0
source

All Articles