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 AxesLearn the difference between Figure and Axes in matplotlib. Understand the two APIs, pyplot and object-oriented, and when to use each one.
- 02Line PlotsDraw single and multiple-line plots with matplotlib's plot function. Set colours, markers, line styles and labels with the arguments that matter.
- 03Scatter PlotsBuild scatter plots with matplotlib's scatter function. Map colour and size to data columns and encode four dimensions in one chart.
- 04Bar ChartsBuild vertical, horizontal, grouped and stacked bar charts in matplotlib. See why the y-axis must start at zero and when to reorder bars.
- 05Histograms and DistributionsPlot a histogram in matplotlib and change the bin count. Watch the same data appear to tell a different story depending on the bins chosen.
- 06Labels, Titles and LegendsAdd axis labels, a title and a legend to a matplotlib figure. Make a chart that someone else can read without asking what it shows.
- 07Limits, Ticks and ScalesSet axis limits, tick positions, tick labels and log scales in matplotlib. Control exactly what the axis shows and how it reads.
- 08SubplotsCreate grids of subplots with plt.subplots and GridSpec in matplotlib. Place multiple axes on one figure with controlled sizes and spacing.
- 09Colour and ColormapsPick colours by name, hex or RGB in matplotlib. Choose a colormap that is perceptually uniform and still readable in greyscale.
- 10Annotating a PlotAdd text labels, arrows and reference lines to a matplotlib chart. Turn a bare plot into one that tells the reader what to notice.
- 11Saving FiguresSave matplotlib figures with savefig. Set the DPI, file format and bounding box so the saved image matches what you see on screen.
- 12Dates on an AxisPlot time series in matplotlib with proper date axes. Set locators and formatters so that date labels stay readable without overlapping.
- 13Error Bars and BandsAdd error bars and shaded confidence bands to a matplotlib plot. Label whether you are showing standard deviation, standard error or a CI.
- 14Images and HeatmapsDisplay images and heatmaps with imshow and pcolormesh in matplotlib. Fix the upside-down origin and choose a colormap that reads correctly.
- 15Styles and rcParamsApply built-in styles and set rcParams to restyle every matplotlib chart at once. Create a consistent look without repeating style code.
- 16Twin and Secondary AxesAdd a second y-axis to a matplotlib plot with twinx. See why dual axes can mislead readers and learn the safer alternatives for two scales.
- 17Box and Violin PlotsDraw box plots and violin plots side by side in matplotlib. Compare what each summary shows and what each one hides about the distribution.
- 18Plotting from pandasCreate charts from a pandas DataFrame in one line with df.plot. See when the shortcut works and when to fall back to raw matplotlib calls.
- 19Choosing a ChartMatch your data question to the right chart type. Walk through a decision tree that picks between bar, line, scatter and histogram.
- 20Performance and Large DataSpeed up slow matplotlib figures that render too many points. Use rasterisation, line simplification and collections to stay responsive.
- 21Layout and SpacingSet figure size, margins and subplot spacing in matplotlib. Use constrained_layout to stop labels and titles from overlapping automatically.
- 22Common MistakesSpot the matplotlib mistakes that produce a chart which looks correct but misrepresents the data. Fix truncated axes and dual-axis traps.