Skip to content
LearnPY
Matplotlib

See relationships and distributions

Use scatter plots to inspect two-variable relationships and histograms to understand the shape of one variable.

11 min readbeginnermatplotlib, numpy

By the end

  • Draw a scatter plot for paired observations.
  • Use point size and transparency without overloading the chart.
  • Draw and interpret a histogram.
  • Choose bins deliberately.
  • Verify plotted values through Axes artists.

Scatter plots show pairs

A scatter plot places one point for each observation. It is the right first look at the relationship between two numeric variables: direction, clusters, gaps, and outliers become visible before any model is fitted.

Python

One point per student

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

A pattern in a scatter plot is not proof of causation. It can suggest a relationship worth testing, but confounders and measurement choices still matter.

Encode carefully

Point size, color, and transparency can carry additional information. Use them when the extra dimension matters, and stop when the chart becomes harder to read. Overlapping points often need transparency more than decoration.

Python

Size encodes class size

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

Histograms show one variable's shape

A histogram divides the range of one numeric variable into bins and counts how many observations fall in each bin. It answers questions about center, spread, skew, and gaps that a list of numbers does not.

Python

Counts across six score bins

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

The bin count changes the picture. Too few bins hide structure; too many bins turn random noise into apparent spikes. Try a few sensible values before drawing a conclusion.

Add a reference line

A vertical or horizontal reference line can show a mean, target, or threshold. Draw it after the data so it sits on top, and label it in the title or a legend rather than leaving an unexplained line.

Python

A labelled reference line

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

The next lesson controls layouts and styling across a whole figure: multiple axes, spacing, annotations, and a visual system that stays legible.

Practice

Make it stick

Run the tests as often as you like. Hints and the solution are available when you need them.

Exercise

Draw a histogram of scores with 6 bins and white edges. Set the title to "Score distribution", xlabel to "Score", ylabel to "Students", and add a horizontal-only grid.

Your solution

Ready
Loading editor…

Results

Run the tests to check your solution. The first run downloads Python; later runs reuse the session.

Quiz

Check your understanding

01What is a scatter plot most useful for?
02What happens when a histogram has too few bins?
03Why add a labelled mean line to a histogram?

Lesson progress