Link Search Menu Expand Document (external link)

How to create a QQ-plot (in Python, using statsmodels)

See all solutions.

Task

We often want to know whether a set of data is normally distributed, so that we can deduce what inference tests are appropriate to conduct. If we have a set of data and want to figure out if it comes from a population that follows a normal distribution, one tool that can help is a QQ plot. How do we make and interpret one?

Related tasks:

Solution

We’re going to use some fake data here by generating random numbers, but you can replace our fake data with your real data in the code below.

1
2
3
# Replace this with your data, such as a variable or column in a DataFrame
import numpy as np
values = np.random.normal(0, 1, 50)  # 50 random values

If the data is normally distributed, then we expect that the QQ plot will show the observed values (blue dots) falling very clsoe to the red line (the quantiles for the normal distribution).

1
2
3
4
5
import statsmodels.api as sm
import matplotlib.pyplot as plt

sm.qqplot(values, line = '45')
plt.show()

png

Our observed values fall pretty close to the reference line. In this case, we expected that, because we created fake data that was normally distributed. But for real data, it may not stay so close to the red line.

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by Elizabeth Czarniak (CZARNIA_ELIZ@bentley.edu)