How can a Bland-Altman plot be created using Python?

A Bland-Altman plot is a graphical method used to assess the agreement between two quantitative measurements. It is commonly used in medical and scientific research to compare two methods of measurement or to evaluate the consistency of a single measurement method over time. To create a Bland-Altman plot using Python, one can use the matplotlib library to plot the difference between the two measurements (y-axis) against the average of the two measurements (x-axis). This will produce a scatter plot with a horizontal line representing the mean difference and two parallel lines representing the limits of agreement. The plot can be further customized by adding labels, titles, and other visual elements to aid in the interpretation of the data. Using Python to create a Bland-Altman plot offers a simple and efficient way to visualize and analyze the agreement between two measurements, providing valuable insights for researchers and practitioners.

Create a Bland-Altman Plot in Python


A 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.apias sm
import matplotlib.pyplotas 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].

x