A way to draw things in python which is better than no way to draw things in python


The classic python plotting is matplotlib. It can’t do all those modern hipster graphs without hard labour and is awful at animations and interactions, and it fugly per default. It works OK out of the box. There are libraries which use matplotlib as a backend and build more eleaborate systems on the top, but these have not had much longevity so far, so I find myself falling back to plain old matplotlib. It is an acceptable default with lots of weird edge cases when you try to be clever, but gets the job 80% done.

Note some confusing terminology; An Axes object, which is constructed by an add_subplot command, contains two Axis objects, but is much more than a list of such objects, being the fundamental object upon which a graph is drawn.

But don’t listen to me describe it. Observe this lovely diagram which explains all.


Taxonomy of a matplotlib plot. I’ve lost the source for this image, sorry.

Read Jakevdp’s manual for some pedagogic advice.

Here are some miscellaneous tips:

Grammar of graphics

Plotnine implements a best-effort clone of R’s ggplot2 library for matplotlib I believe plotnine supersedes the abandoned(?) by yhat (ggplot source, plotnine source).

Matplotlib styling

The default matplotlib stylesheet aspires to look like 80s spreadsheet defaults, but if you are not a retrofuturist, you want to change the stylesheet. Some of the built-in stylesheets are OK.

Here is an ugly gallery of sometimes-beautiful graph styles. Here is an ugly gallery of sometimes-beautiful colour maps.

Seaborn is another vaunted extension, which I would describe as an “Edward Tufterizer”. Extends matplotlib with modern apperance and some missing plot types.

A cute hack to justify matplotlib’s existence: xkcd graphs.

Axis labels


ax = plt.gca()


Someone made the idiosyncratic choice that default font is sans serif, even for mathematical text. You can change this by setting serif fonts also for mathtext.

from matplotlib import rc

Supported math fonts are reputedly

  • dejavusans (horrible default)
  • dejavuserif (beware of odd greek letters)
  • cm (”Computer Modern”. Classic, dated. )
  • stix (not sure)
  • stixsans (sounds like sans serif to me)

Alternatively you can render your graph labels with TeX which leads to some weird spacing but allows you to match fonts better. It is also fragile and character set issues are terrible. Is this better if I use XeLaTeX/LuaLaTeX?


Yellowbrick is a matplotlib specialisation for hyperparameter optimisation.

Yellowbrick extends the Scikit-Learn API to make model selection and hyperparameter tuning easier. Under the hood, it’s using Matplotlib.


3b1b’s manim is a curious passion project to create interactive matplotlib animations in code, targeting videos. It is famous on e.g. youtube. Here is a powerful example. I believe the community edition(source) is recommended for newcomers.

No comments yet. Why not leave one?

GitHub-flavored Markdown & a sane subset of HTML is supported.