I’ve used matplotlib since 2011 and came from MATLAB so I know that it began its life as python replacement for MATLAB plotting functions.
In the intervening years, the object-oriented interface has evolved and I’ve seen it recommended over pyplot in more than one place, for example here: https://matplotlib.org/stable/tutorials/introductory/lifecycle.html
It says:
A note on the Object-Oriented API vs. Pyplot
Matplotlib has two interfaces. The first is an object-oriented (OO) interface. In this case, we utilize an instance of
axes.Axes
in order to render visualizations on an instance offigure.Figure
.The second is based on MATLAB and uses a state-based interface. This is encapsulated in the
pyplot
module. See the pyplot tutorials for a more in-depth look at the pyplot interface.Most of the terms are straightforward but the main thing to remember is that:
- The Figure is the final image that may contain 1 or more Axes.
- The Axes represent an individual plot (don’t confuse this with the word “axis”, which refers to the x/y axis of a plot).
We call methods that do the plotting directly from the Axes, which gives us much more flexibility and power in customizing our plot.
Note: In general, try to use the object-oriented interface over the pyplot interface.
Ok! I find working with objects natural so I prefer that. Sold!
But then the tutorial goes on to start with:
import numpy as np
import matplotlib.pyplot as plt
and
fig, ax = plt.subplots()
So what is it now? Are we using pyplot or Axes and Figure? I have yet to find an ostensibly object-oriented example that does not rely on pyplot.
This mixing of paradigms in apparent contravention of the recommendations is, at the very least, confusing.
I have tried to work without relying on pyplot. Working in a jupyter notebook, I tried creating a figure object like this:
import matplotlib as mpl
fig = mpl.figure.Figure()
ax = mpl.axis.Axes(fig, [0.1, 0.1, 0.7, 0.7])
This results in no visible output (using the inline backend). If I try and make the figure appear:
fig.show()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-12-73d731ead0b0> in <module>
----> 1 fig.show()
/usr/lib/python3.8/site-packages/matplotlib/figure.py in show(self, warn)
405 """
406 if self.canvas.manager is None:
--> 407 raise AttributeError(
408 "Figure.show works only for figures managed by pyplot, "
409 "normally created by pyplot.figure()")
AttributeError: Figure.show works only for figures managed by pyplot, normally created by pyplot.figure()
tl; dr - how can I routinely create empty Axes and display them in a jupyter notebook without having to use pyplot?