matplotlib, by Drawing It
Figures and axes, the two APIs, and the styling that turns a default plot into one worth showing - every example drawn on the page.
About this track
matplotlib is two libraries sharing a name: a stateful pyplot interface that draws into whatever figure is current, and an object-oriented one where you hold the figure and the axes and ask them to draw. Most confusing examples online mix the two.
This track uses the object-oriented form throughout and explains the other where you will meet it. Every editor draws a real figure and shows it under the output, so a change to a line is a change you see.
Every editor draws a real figure and shows it under the output, so changing a number and running it again is the fastest way to find out what an argument does - which matters here more than in most libraries, because matplotlib's argument names are not always guessable.
What you will be able to do
- Reach for the object-oriented API and translate any pyplot example you find into it.
- Choose a chart from the question being asked rather than from habit.
- Control limits, ticks, scales and colour deliberately instead of accepting the defaults.
- Build a figure of several panels that is a genuine comparison rather than four adjacent charts.
- Recognise the displays that mislead - a truncated bar axis, a dual axis, an uncentred diverging colormap.
How the track is ordered
The figure and the axes come first, along with the two APIs that make most examples online confusing. Then the drawing types - lines, scatters, bars, histograms, boxes and images - followed by everything that makes a chart readable: labels, legends, limits, ticks, colour and annotation. Layout and saving come next, because a figure that looks right on screen and crops when saved is the commonest frustration. The track ends with judgement rather than mechanism - choosing a chart, the mistakes that produce a plausible wrong picture, and what to do when there are more points than pixels.
Where this leads
matplotlib is the layer under pandas' .plot and under seaborn, so anything either of those produces can be adjusted with what is here. The pandas track covers getting data into the shape a chart wants, which is usually the larger half of the work.
All 22 modules, in teaching order
- 01Figure and Axes
- 02Line Plots
- 03Scatter Plots
- 04Bar Charts
- 05Histograms and Distributions
- 06Labels, Titles and Legends
- 07Limits, Ticks and Scales
- 08Subplots
- 09Colour and Colormaps
- 10Annotating a Plot
- 11Saving Figures
- 12Dates on an Axis
- 13Error Bars and Bands
- 14Images and Heatmaps
- 15Styles and rcParams
- 16Twin and Secondary Axes
- 17Box and Violin Plots
- 18Plotting from pandas
- 19Choosing a Chart
- 20Performance and Large Data
- 21Layout and Spacing
- 22Common Mistakes