Member-only story
Matplotlib: the Object-Oriented API Explained
Stop using plt.plot() and get better control over your charts
Matplotlib has been a powerhouse of the data visualization world for many years and is the standard for generating document-ready plots and charts. But it can be a little confusing to begin with; the terms used for some chart objects are a little odd, and it has two APIs.
In this article, we are going to cut through this confusion and use the Object-Oriented Matplotlib interface to generate professional-looking charts.
How everybody starts
Just about everybody’s introduction to Matplotlib will have involved a variation of the following:
plt.plot(x, y)Where x and y are lists (or similar) and the result is a line chart.
This looks easy, but there’s something going on behind the scenes that this code hides: four objects are created:
- a
figureobject which represents the entire chart - an
axesobject, which despite its name, is a single plotting area - two
axisobjects which are the vertical and horizontal rulers (the 'x' and 'y' axes) - an
artistobject which is a drawable object, for example, a plot line.







