Description
I would like to know if it is recommended to always use the OOP API if
matplotlib plots need to be created in a library.
The reason why I'm asking is the following: We have lots of libraries that
create matplotlib plots non-interactively if the code is run as part of a
(multi-threaded) service on a head-less system or interactively if a
console-script is started manually. For these libraries we run unit tests on
a CI server. Since we don't explicilty specify a backend (via
matplotlib.use), we either have to mock the pylot API a bit or run a fake
xserver (Xvfb) in the background to fix issues due to missing DISPLAY env
var. (On our headless systems we neither use a custom matplotlibrc nor do we
set the MATPLOTLIB_BACKEND env var. pyqt/pyside is installed on these
systems because the console scripts depend on qt libs)
The matplotlib documentation of the mpl API
(https://matplotlib.org/3.1.1/api/index.html) contains the following two
sentences:
At its core, Matplotlib is object-oriented. We recommend directly working
with the objects, if you need more control and customization of your plots.
pyplot is mainly intended for interactive plots and simple cases of
programmatic plot generation.
Do you recommend to use pyplot in our case or rather recommend us to use the OOP API
directly? Independent of the recommendation I think it makes sense to extend the docs a bit and mention
this use-case, since our use-case is probably very common.
BTW There are unfortunately very few (currently only 1:
https://matplotlib.org/gallery/api/agg_oo_sgskip.html) examples that show
how the OOP API is supposed to be used. What is missing IMO is an example
that shows how to use plotting functions both interactively and
non-interactively.