10.7 C
London
Saturday, May 17, 2025
HomePythonData Visualizations in PythonHow to Create a Bland-Altman Plot in Python

How to Create a Bland-Altman Plot in Python

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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].

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories