Understanding matplotlib: plt, figure, ax(arr)?

Question:

I’m not really new to matplotlib and I’m deeply ashamed to admit I have always used it as a tool for getting a solution as quick and easy as possible. So I know how to get basic plots, subplots and stuff and have quite a few code which gets reused from time to time…but I have no “deep(er) knowledge” of matplotlib.

Recently I thought I should change this and work myself through some tutorials. However, I am still confused about matplotlibs plt, fig(ure) and ax(arr). What is really the difference?

In most cases, for some “quick’n’dirty’ plotting I see people using just pyplot as plt and directly plot with plt.plot. Since I am having multiple stuff to plot quite often, I frequently use f, axarr = plt.subplots()…but most times you see only code putting data into the axarr and ignoring the figure f.

So, my question is: what is a clean way to work with matplotlib? When to use plt only, what is or what should a figure be used for? Should subplots just containing data? Or is it valid and good practice to everything like styling, clearing a plot, …, inside of subplots?

I hope this is not to wide-ranging. Basically I am asking for some advice for the true purposes of plt <-> fig <-> ax(arr) (and when/how to use them properly).

Tutorials would also be welcome. The matplotlib documentation is rather confusing to me. When one searches something really specific, like rescaling a legend, different plot markers and colors and so on the official documentation is really precise but rather general information is not that good in my opinion. Too much different examples, no real explanations of the purposes…looks more or less like a big listing of all possible API methods and arguments.

Asked By: daniel451

||

Answers:

  • pyplot is matlab like API for those who are familiar with matlab and want to make quick and dirty plots
  • figure is object-oriented API for those who doesn’t care about matlab style plotting

So you can use either one but perhaps not both together.

Answered By: Hun

pyplot is the ‘scripting’ level API in matplotlib (its highest level API to do a lot with matplotlib). It allows you to use matplotlib using a procedural interface in a similar way as you can do it with Matlab. pyplot has a notion of ‘current figure’ and ‘current axes’ that all the functions delegate to (@tacaswell dixit). So, when you use the functions available on the module pyplot you are plotting to the ‘current figure’ and ‘current axes’.

If you want ‘fine-grain’ control of where/what your are plotting then you should use an object oriented API using instances of Figure and Axes.

Functions available in pyplot have an equivalent method in the Axes.

From the repo anatomy of matplotlib:

  • The Figure is the top-level container in this hierarchy. It is the overall window/page that everything is drawn on. You can have multiple independent figures and Figures can contain multiple Axes.

But…

  • Most plotting occurs on an Axes. The axes is effectively the area that we plot data on and any ticks/labels/etc associated with it. Usually we’ll set up an Axes with a call to subplot (which places Axes on a regular grid), so in most cases, Axes and Subplot are synonymous.

  • Each Axes has an XAxis and a YAxis. These contain the ticks, tick locations, labels, etc.

If you want to know the anatomy of a plot you can visit this link.

Answered By: kikocorreoso

I think that this tutorial explains well the basic notions of the object hierarchy of matplotlib like Figure and Axes, as well as the notion of current figure and current Axes.

If you want a quick answer: There is the Figure object which is the container that wraps multiple Axes(which is different from axis) which also contains smaller objects like legends, line, tick marks … as shown in this image taken from matplotlib documentation

enter image description here

So when we do

>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> type(fig)
<class 'matplotlib.figure.Figure'>
>>> type(ax)
<class 'matplotlib.axes._subplots.AxesSubplot'>

We have created a Figure object and an Axes object that is contained in that figure.

Answered By: Firas Omrane
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.