Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 57 ยท Working with Data

Statistical Plots Recipes

Draw QQ plots, residual plots, ECDFs, density curves, correlation and confusion heatmaps, ROC curves and learning curves with Matplotlib.

Statistics and machine learning have a standard set of diagnostic charts. A QQ plot tells you whether data looks normal, a residual plot whether a model's errors are random, an ROC curve how well a classifier separates two groups. None of them need a special library: with Matplotlib, SciPy and scikit-learn each one is a few lines.

This lesson is a collection of recipes. For every chart you will see what question it answers, how to build it and how to read it. All data is generated with fixed seeds, so your pictures match the descriptions. The chart choice follows the same logic as in Designing Clear Charts: pick the plot that answers the question.

Is my data well behaved?

QQ plot Does it look normal?
ECDF The whole distribution, no bins.
Density A smooth histogram.
1 / 2

QQ plot: does the data look normal?

A quantile-quantile (QQ) plot compares the sorted values of your data with the values you would expect from a theoretical distribution, usually the normal. If the data is normal, the points fall on a straight line. Curves and bends reveal skew and heavy tails. scipy.stats.probplot computes both sets of quantiles and the fitted line.

Python QQ plots of normal and skewed data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The normal data hugs the line (r close to 1). The skewed data bends upward at the right end: its largest values are much larger than a normal curve would predict. The r value is the correlation between the two sets of quantiles, a quick numeric summary, but the shape tells you more than the number.

ECDF: the whole distribution without bins

The empirical cumulative distribution function answers "what fraction of values is at or below x?". Sort the data, then plot the sorted values against 1/n, 2/n, ... 1. It has no bin width to choose and shows every point, so it is excellent for comparing groups: the curve farther to the right has larger values.

Python ECDFs for two groups

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read an ECDF horizontally: where a curve crosses 0.5, you find the median. Read it vertically: at any x, the height is the share of values below it. A wider spread shows as a flatter curve, which is why group B looks stretched.

Density with gaussian_kde

You met KDE curves in Seaborn-Style Recipes in Pure Matplotlib. Their statistical use is to show a distribution's shape: how many peaks, whether it is symmetric, how heavy the tails are. It pays to put the histogram and the density together, so readers can see both the raw counts and the smoothed shape.

Python Histogram with a KDE overlay

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

density=True rescales the histogram so its area is 1, the same scale as the KDE; otherwise the two could not share an axis. The smoothing factor matters: bw=0.15 chases noise while bw=0.6 starts to merge the two peaks.

Residual plots: are the errors random?

After fitting a model, plot the residuals (actual minus predicted) against the predictions or against x. If the model fits, residuals scatter randomly around zero with a constant spread. A curve means the model missed a pattern (a straight line fitted to curved data). A funnel shape means the error grows with the value, a sign of non-constant variance.

Python Good fit versus missed curvature

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The line leaves a clear U shape in its residuals: positive at both ends, negative in the middle. The quadratic's residuals are a shapeless cloud around zero and their standard deviation drops to about 2, the noise level we added. "Looks random" is the goal; any visible structure is information the model failed to use.

Correlation heatmap

A correlation matrix holds the Pearson correlation of every pair of columns, between -1 and 1. A heatmap with a diverging colormap centered at zero, a fixed scale and the numbers printed in the cells is readable at a glance. Keep the scale at -1 to 1, otherwise a weak correlation can look strong.

Python An annotated correlation heatmap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The diagonal is always 1 because a column correlates perfectly with itself, and the matrix is symmetric, so half of it repeats the other half. Switching the text color on strong cells keeps numbers readable on dark red or blue. Remember that correlation measures straight-line association only, and it does not show causes.

Confusion-matrix heatmap

A confusion matrix counts how often each true class was predicted as each class. Rows are the true labels, columns the predictions; the diagonal holds correct answers. The same heatmap recipe works, but with a sequential map and integer counts.

Python A confusion matrix from a classifier

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look off the diagonal: a large number in row 1, column 2 means class 1 is often mistaken for class 2. To compare classes of different sizes, normalize each row by its sum (cm / cm.sum(axis=1, keepdims=True)), which turns counts into per-class recall.

ROC curve

For a binary classifier that outputs scores, the ROC curve plots the true positive rate (hits) against the false positive rate (false alarms) as the decision threshold sweeps from strict to lenient. The diagonal is random guessing. A curve hugging the top-left corner is a strong classifier. The AUC (area under the curve) summarizes it: 0.5 is chance, 1.0 is perfect.

Python ROC curves for two models

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

set_aspect("equal") makes the plot square, which is the convention because both axes run from 0 to 1. Both AUC values are printed and also appear in the legend, so a reader can compare models at a glance. Compare curves, not just AUC: two models can have the same area but behave differently at the strict end of the threshold, which is where many applications care most.

Learning curves

A learning curve trains a model on growing portions of the data and records training and validation scores. It answers "would more data help?". If the two curves are far apart, the model overfits and more data may help. If both are low and close together, the model is too simple. If both are high and close, you are done.

Python Training versus validation score

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The training score stays near 1.0 because an unpruned tree memorizes its data, while the validation score is clearly lower and creeps up with more data. The gap between the two is the overfitting. The bands show variation across the five cross-validation folds; wide bands mean the estimate is shaky.

Choosing the right diagnostic

Question Chart
Is this sample roughly normal? QQ plot
How do two groups differ overall? ECDF, or histogram with density
Does my regression miss a pattern? Residuals against predictions
Which variables move together? Correlation heatmap
Which classes get confused? Confusion matrix heatmap
How well does a classifier rank cases? ROC curve and AUC
Do I need more data or a different model? Learning curve

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In a normal QQ plot, the points curve upward at the right end. What does that suggest?

    Upward bending means the largest sample values are bigger than a normal distribution would predict, which is the signature of a right-skewed or heavy-tailed distribution.
  2. 2. How do you read the median from an ECDF plot?

    The ECDF height is the fraction of values at or below x, so the median is the x where the curve crosses 0.5.
  3. 3. A residual plot shows a clear U shape. What does this tell you?

    Residuals should look like a random cloud around zero. A U shape means a systematic pattern remains, such as curvature a straight line cannot capture.
  4. 4. Why should a correlation heatmap use vmin=-1 and vmax=1?

    A fixed scale from -1 to 1 prevents autoscaling from making a weak correlation look as strong as a perfect one.
  5. 5. A learning curve shows training accuracy near 1.0 and validation accuracy near 0.75. What is the most likely diagnosis?

    A large gap between training and validation scores is the classic sign of overfitting, because the model memorizes the training data but generalizes less well.

Summary

  • A QQ plot (scipy.stats.probplot) tests normality: straight is normal, bends show skew or heavy tails.
  • An ECDF shows the whole distribution without bins; medians sit where the curve crosses 0.5.
  • gaussian_kde plus a density=True histogram shows shape; bandwidth controls smoothness.
  • Residuals against predictions should look like random noise around zero; patterns mean the model missed something.
  • Heatmaps built with imshow and text display correlation (diverging map, fixed -1 to 1 scale) and confusion matrices (counts, true labels on rows).
  • roc_curve and auc summarize a classifier's ranking quality; the diagonal is chance.
  • learning_curve plots training against validation scores to diagnose overfitting and underfitting.

Next: 3D Plots