How to Create a Q-Q Plot in Python

Spread the love

A Quantile-Quantile (Q-Q) plot is a powerful graphical tool used in statistical analysis to check if the data follows a certain theoretical distribution. Q-Q plots are often used to assess if data is normally distributed, which is a common assumption in many statistical tests and methods. In this article, we’ll go through the steps needed to create a Q-Q plot in Python.

Step 1: Import Necessary Libraries

We’ll be using numpy, matplotlib, and scipy, so make sure you have them installed. If not, you can use pip or conda to install them.

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

Step 2: Generate or Import Your Data

For the purposes of this tutorial, we’ll generate some normally distributed data using numpy’s random.normal function:

# Set a seed for reproducibility

# Generate normally distributed data
data = np.random.normal(loc=0, scale=1, size=1000)

In this case, loc corresponds to the mean of the distribution, scale corresponds to the standard deviation, and size is the number of observations.

Step 3: Create the Q-Q Plot

Next, we’ll use the probplot function from scipy.stats to create the Q-Q plot:

# Create Q-Q plot
fig = plt.figure(figsize=(8,8))
res = stats.probplot(data, plot=plt)

This code generates a Q-Q plot of the data, plotting their quantiles against the quantiles of the standard normal distribution (mean of 0 and standard deviation of 1).

If the data points lie approximately along the red line, we can say that the data is normally distributed. Deviations from this line suggest that the data may not be normally distributed.

In more detail, if the points deviate from the line in the middle, the distribution is skewed. If the points deviate from the line at the ends, the distribution has heavy or light tails, meaning there are more or fewer extreme values than expected for a normal distribution.

Interpreting Q-Q Plots

Interpretation of Q-Q plots revolves around the line that represents the chosen theoretical distribution (a normal distribution in our case). The data points should lie on or around this line if they follow the same distribution. Specifically:

  • A perfect match with the theoretical distribution will result in the data points forming a line that matches the red line in the plot.
  • A skew in the distribution is shown by a curve in the data points.
  • Outliers are shown as points that are far from the red line, especially if other points follow the line well.


Q-Q plots are simple, yet powerful tools for visually inspecting the distribution of your data and checking if it meets certain assumptions. While the above guide used the example of a normal distribution, remember that Q-Q plots can be used to check against any theoretical distribution by providing the appropriate distribution to the dist parameter of the probplot function. As always, visualizations like Q-Q plots should be used in conjunction with other statistical tests to fully understand your data and to make more informed decisions.

Leave a Reply