Lines show ordered change
A line chart implies that the x values have an order and that movement between neighboring points is meaningful. Months, weeks, and time are natural candidates. Unordered categories are usually better served by bars.
Python
An ordered monthly series
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
The line does not prove causation, and it does not fill in missing months. It simply connects the observations in the order you supplied.
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
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
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
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
- Create numeric positions for the category centers.
- Draw the first series shifted left by half the width.
- Draw the second series shifted right by half the width.
- 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
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
ReadyResults
Run the tests to check your solution. The first run downloads Python; later runs reuse the session.
Quiz
Check your understanding
Lesson progress