0

I am self-learning python, and I am very confused by the matplotlib package.

  1. when plotting, is it really necessary to use the below code?

    fig = plt.figure()
    

I thought the code was to define an empty canvas, then we can add subplots to the canvas, but I also see lots of cases where this code is not included, and plt.plot() or plt.subplot() is directly used.

  1. what are the differences between ax and axes? I used to think both were subplot variables, but later I find that axes is sometimes used as variables and sometimes as arguments in plotting, for instance:

as variables, from this post:

fig, axes = plt.subplots(nrows=3, ncols=2, figsize=(12, 8))

as an argument, from this post

n = len(fig.axes)

Any help would be much appreciated.

Elizabeth
  • 195
  • 1
  • 6
  • There is a pyplot (matlab-like) API and a so called "object oriented" API. The official documentation has the details: https://matplotlib.org/stable/api/index.html. As for ax vs axes, those are just variable names. The function `subplots` returns a tuple where the second element is a list of axes objects https://matplotlib.org/stable/api/axes_api.html#matplotlib.axes.Axes. – Mr. Fegur May 27 '22 at 07:57
  • 2
    In general, if you stick to the object oriented pattern (the one with `fig, axs = plt.subplots()`, you will ~always be able to do what you want. I only use the short method (`plt.plot()` etc) for quick few-liners. – Matt Hall May 27 '22 at 14:43

1 Answers1

2

It is possible to plot without setting any variables. Example:

plt.figure()
plt.plot([1, 2], [5, 8])
plt.show()

You must initialize your figure somewhere, hence plt.figure(). As you pointed out, you can also use plt.subplots():

fig, ax = plt.subplots()
ax.plot([1, 2], [5, 8])
plt.show()

Note that we did not set the ncols and nrows keyword arguments. As the default value for both is 1, we get a single axis (which is why I chose the variable name ax). For n × 1 or 1 × n subplots, the second variable returned by plt.subplots() is a one-dimensional array.

fig, axes = plt.subplots(ncols=1, nrows=2)
axes[0].plot([1, 2], [5, 8])
axes[1].plot([2, 3], [9, 3])

In the case of m × n subplots (m, n > 1), axes is a two-dimensional array.

fig, axes = plt.subplots(ncols=2, nrows=2)
axes[0][0].plot([1, 2], [5, 8])
axes[1][0].plot([2, 3], [9, 3])
axes[0][1].plot([3, 4], [5, 8])
axes[1][1].plot([4, 5], [9, 3])

Whether you use ax or axes as the name for the second variable is your own choice, but axes suggests that there are multiple axes and ax that there is only one. Of course, there are also other ways to construct subplots, as shown in your linked post.

tim
  • 171
  • 6
  • hi tim, thank you so much for your answer, I have two follow up questions. Re fig, my question is if we can plot without initialising fig, why bother to initialise fig? seems to me it is quite unnecessary. Re axes, can you explain a bit more why axes in 'n = len(fig.axes)' is also a variable? in the original post, axes has never been initialised. – Elizabeth May 27 '22 at 08:52
  • Someday in the future you might need more than one figure. So it's good to be able to explicitly create them both and control which one you're plotting to. – DavidW May 27 '22 at 09:13
  • 1
    As @DavidW pointed out, initialising the figure is helpful when you have more than one. This way the library knows what figure you are referring to when plotting (the one you initialised most recently). For the linked example, `axes` is a property of `fig`, not a variable per se, i.e. we did not set it. – tim May 27 '22 at 09:33
  • 2
    Good answer. One remark: if you omit `plt.figure()` in the first example, it will happen implicitly. But explicit is "better than implicit". – Matt Hall May 27 '22 at 14:42