OO vs. pyplot

OO vs. pyplot#

If you start exploring matplotlib documentation or other internet resources/tutorials, you will likely run into matplotlib syntax that is different than what’s presented here. That is because Matplotlib has two interfaces.

  • The first is an object-oriented (OO) interface. When working in the OO interface, we utilize an instance of axes.Axes to render visualizations on an instance of figure.Figure.

  • The second is based on MATLAB and uses a state-based interface. This second interface is encapsulated in the pyplot module. pyplot is a collection of functions that make matplotlib work like MATLAB.

At first glance, pyplot can seem like a much easier alternative to the object-oriented interface.

import matplotlib.pyplot as plt # import statement
plt.plot([1,2,3,4]) # create plot
plt.ylabel('some numbers') # add label
plt.show() # show output

No figure, no axis- what magic! Except…pyplot doesn’t scale with multiple plots or plots that require significant customization. The pyplot interface hs much less flexible than the OO interface. Plus, matplotlib’s own documentation recommends that you use the OO interface.

Additional Resources#

For more on the differences between these two interfaces and how to translate sample code across interfaces: