typestar

Plotting with matplotlib

14 steps in 4 sets of Python.

matplotlib is old, enormous, and still what most scientific figures are made of. The single most useful thing to learn is the figure and axes object model, because almost every confusing example online is confusing for mixing that up with the pyplot state machine.

Fourteen steps: figures and axes, the plot types that cover most needs, and then the annotation, scaling and legend work that turns a plot into something a reader can actually interpret.

Start this tour

Figures & axes

  • Figure and axesThe object API: one figure, one or more axes, and everything hangs off them.
  • Grids of axessubplots returns an array of axes you can index like any other.
  • StylingStyle sheets set the defaults; rcParams override them per figure.
  • Saving figuresThe format follows the extension; dpi and bbox decide the rest.

Plot types

  • Line plotsSeveral series on one axes, each with its own style and label.
  • Scatter plotsPoint size and color are two more dimensions you can spend.
  • Bar chartsCategories along one axis, values along the other, labels on the bars.
  • HistogramsDistribution shape, and the bin count that decides what you see.
  • Heatmapsimshow draws a matrix; the colorbar and the ticks make it legible.

Reading the plot

Encore

  • mpl_dashboard.pyA four-panel dashboard built from one dataset, saved as a single figure.

The other Python tours