How to Create a Bland-Altman Plot in Python


A Bland-Altman plot is used to visualize the differences in measurements between two different instruments or two different measurement techniques.

It’s useful for determining how similar two instruments or techniques are at measuring the same construct.

This tutorial provides a step-by-step example of how to create a Bland-Altman plot in Python.

Step 1: Create the Data

Suppose a biologist uses two different instruments (A and B) to measure the weight of the same set of 20 different frogs, in grams.

We’ll create the following data frame that represents the weight of each frog, as measured by each instrument:

import pandas as pd

df = pd.DataFrame({'A': [5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9,
                         10, 11, 13, 14, 14, 15, 18, 22, 25],
                   'B': [4, 4, 5, 5, 5, 7, 8, 6, 9, 7, 7, 11,
                         13, 13, 12, 13, 14, 19, 19, 24]})

Step 2: Create the Bland-Altman Plot

Next, we’ll use the mean_diff_plot() function from the statsmodels package to create a Bland-Altman plot:

import statsmodels.api as sm
import matplotlib.pyplot as plt

#create Bland-Altman plot                  
f, ax = plt.subplots(1, figsize = (8,5))
sm.graphics.mean_diff_plot(df.A, df.B, ax = ax)

#display Bland-Altman plot
plt.show()

Bland-Altman plot in Python

The x-axis of the plot displays the average measurement of the two instruments and the y-axis displays the difference in measurements between the two instruments.

The black solid line represents the average difference in measurements between the two instruments while the two dashed lines represent the 95% confidence interval limits for the average difference.

The average difference turns out to be 0.5 and the 95% confidence interval for the average difference is [-1.86, 2.86].

Leave a Reply

Your email address will not be published.