Learn Matplotlib

The foundational Python library for static, publication-quality plots

21 lessons · 4 hours · 4 levels · covers Matplotlib 3.11

Matplotlib is Python's original and most widely used plotting library: it draws line, scatter, bar, image and almost any other 2D chart, and saves them as PNG, SVG or PDF. pandas, seaborn and many scientific tools build on it, so knowing its Figure/Axes model lets you control and fix any chart they produce.

Before you start

Matplotlib builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics7 lessons · 1 hour in total
2. PracticalUse it productively at work, with the usual idioms14 lessons · 2 hours in total
3. AdvancedThe less common features, performance and trade-offs18 lessons · 3 hours in total
4. ExpertInternals and the edge cases experts know21 lessons · 4 hours in total

The Matplotlib tree

MatplotlibFigures, Axes and pyplotFigures, Axes andpyplotLine plots with plot()Line plots with plot()Scatter plotsScatter plotsBar charts and histogramsBar charts andhistogramsTitles, labels and legendsTitles, labels andlegendsGrids of plots with subplotsGrids of plots withsubplotsSaving figures to filesSaving figures to filespyplot vs the object-oriented APIpyplot vs theobject-oriented APIColors, markers and style sheetsColors, markers andstyle sheetsLimits, ticks and tick formattingLimits, ticks and tickformattingLog scales and twin axesLog scales and twinaxesImages, heatmaps and colormapsImages, heatmapsand colormapsText, annotations and bar labelsText, annotations andbar labelsTime series, error bars and bandsTime series, errorbars and bandsModules and importsModules and importsCreating arraysCreating arraysDefining functionsDefining functionsFunctions as objects, lambdas and closuresFunctions as objects,lambdas and closur…Indexing and slicingIndexing and slicingContext managers and withContext managersand withClasses and instancesClasses andinstances
Essentials and Practical shown (14 of 21 lessons); the full list is below. Click a lesson to read it.

All 21 lessons, in order

Figures and basic plots

  1. Figures, Axes and pyplot · Essentials · 10 min

    The two objects behind every Matplotlib chart: the Figure (the canvas) and the Axes (one plot with its own coordinates).

  2. Line plots with plot() · Essentials · 10 min

    Draw one or more lines from x and y sequences, and control their color, style and markers.

  3. pyplot vs the object-oriented API · Practical · 10 min

    Understand the implicit pyplot state machine and the explicit Axes-method style, and when to use each.

Chart types

  1. Scatter plots · Essentials · 8 min

    Show the relationship between two variables as unconnected points, optionally sized and colored by more data.

  2. Bar charts and histograms · Essentials · 10 min

    Compare categories with bar and barh, and show a distribution of values with hist.

  3. Images, heatmaps and colormaps · Practical · 12 min

    Display 2D arrays with imshow and pcolormesh, pick a perceptually uniform colormap, and add a colorbar.

  4. Time series, error bars and bands · Practical · 12 min

    Plot datetime x axes with readable date ticks and show uncertainty with errorbar and fill_between.

Labels, ticks and style

  1. Titles, labels and legends · Essentials · 8 min

    Make a chart self-explanatory with a title, axis labels including units, and a legend for multiple series.

  2. Colors, markers and style sheets · Practical · 10 min

    Specify colors in every supported way, use the property cycle, and switch the whole look with style sheets.

  3. Limits, ticks and tick formatting · Practical · 12 min

    Control axis ranges, tick positions and tick label formats with set_xlim, locators and formatters.

  4. Log scales and twin axes · Practical · 10 min

    Plot data spanning orders of magnitude on log or symlog scales, and overlay two y scales with twinx.

  5. Text, annotations and bar labels · Practical · 10 min

    Point out what matters with ax.text, ax.annotate arrows, reference lines and automatic bar labels.

  6. rcParams and building a house style · Advanced · 10 min

    Change Matplotlib defaults through rcParams, write your own .mplstyle file, and scope changes with rc_context.

Layout and output

  1. Grids of plots with subplots · Essentials · 10 min

    Create several Axes in a grid with plt.subplots(nrows, ncols) and share axes between them.

  2. Saving figures to files · Essentials · 8 min

    Export figures as PNG, SVG or PDF with savefig, and control resolution, margins and transparency.

  3. Complex layouts: constrained, mosaic, GridSpec · Advanced · 14 min

    Build uneven multi-panel figures with subplot_mosaic and GridSpec, and let constrained layout prevent overlaps.

Backends and animation

  1. Backends, interactive mode and notebooks · Advanced · 12 min

    Learn how backends render and display figures, how to run headless, and how plt.show and interactive mode behave.

  2. Animations with FuncAnimation · Advanced · 14 min

    Animate a figure frame by frame with FuncAnimation and export it as GIF or MP4.

Artists, transforms and performance

  1. The Artist hierarchy · Expert · 14 min

    See every visible element as an Artist in a tree under the Figure, and find, inspect and modify any of them.

  2. Coordinate systems and transforms · Expert · 15 min

    Understand data, axes, figure and display coordinates and the transform pipeline that maps between them.

  3. Performance and large data · Expert · 14 min

    Speed up rendering of big datasets and live plots with blitting, rasterization, simplification and decimation.