Skip to content
LearnPY
Matplotlib

Compare series with lines and bars

Use line charts for ordered change, bars for categorical totals, and labels plus a legend to tell the series apart.

11 min readbeginnermatplotlib, numpy

By the end

  • Plot more than one series on the same axes.
  • Use labels and legends instead of relying on color alone.
  • Choose line charts for ordered data and bars for categories.
  • Build a grouped bar chart with explicit x positions.
  • Check the artists in the Axes to confirm what was drawn.

Compare two series

Give each call to plot a label, then call ax.legend. The label is the series name that appears in the legend; without it, the lines are anonymous even if their colors differ.

Python

A shared scale and a legend

Ready
Loading editor…

Output

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

Bars show categorical totals

A bar chart compares categories by length. The categories do not have to be ordered, and the baseline should usually be zero so that the visual length is proportional to the value.

Python

One bar per category

Ready
Loading editor…

Output

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

Horizontal bars are often easier to read when category names are long: use ax.barh and set the y label. The same baseline rule applies.

Grouped bars need positions

To place two bars in each category, convert the categories to numeric positions, then offset each series by half a bar width. Without explicit positions, the second ax.bar call draws on top of the first.

Python

Two series side by side

Ready
Loading editor…

Output

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

  1. Create numeric positions for the category centers.
  2. Draw the first series shifted left by half the width.
  3. Draw the second series shifted right by half the width.
  4. Set the tick positions and labels back to the original categories.

Check what you drew

A chart can look plausible and still plot the wrong data. The Axes keeps its artists: lines, patches, labels, and legend. Testing those objects is faster and more reliable than inspecting a screenshot.

Python

Assert the data and the labels

Ready
Loading editor…

Output

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

The next lesson moves from comparison to distribution: scatter plots for relationships and histograms for the shape of one variable.

Practice

Make it stick

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

Exercise

Build a grouped bar chart for north and south using numeric x positions and width 0.35. Add series labels, set the x tick labels to months, title the chart "Monthly signups by region", label the y-axis "Signups", and show a legend.

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

01Which chart is usually better for ordered monthly change?
02Where does a legend's text come from?
03Why should a bar chart usually start its value axis at zero?

Lesson progress