1

I've made a small predator and prey simulation (here: foxes and hares) in Python. Consider the following piece of code:

import matplotlib.pyplot as plt

plt.plot(foxchronicle, harechronicle)
plt.show()

It outputs a plot of the trajectory of the populations in fox-hare space, for example one run produced this picture:

Small plot of the population

Now I wanted the picture to be larger, so I googled and someone on the Internet said I should add this line right after importing the library:

fig = plt.figure(figsize=(18, 14), dpi=80)

The resulting code is this:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(18, 14), dpi=80)
plt.plot(foxchronicle, harechronicle)
plt.show()

It did work and produced this picture:

enter image description here

(Note that this picture captures a different data set, because my program generates them afresh at each run.)

Now I'm confused, because the variable "fig" does not at all occur in the two commands that follow and that use "plt". How can this be? As I read the code, I am merely creating a variable and then never using it.

Note: In contrast to the questions in the closing vote, this question asks neither about the general workings of matplotlib nor about any plot function, but very specifically about how matplotlib manages the figure that it's going to draw next.

In particular, it is not about the object-oriented interface, which I used only by accident.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I should perhaps add that I'm using the Spyder IDE. – AlgebraicsAnonymous Apr 05 '23 at 09:19
  • 2
    you don't even need the `fig =` part in your case (i.e. defining `fig`, all that matters is calling the function). `plt` does a lot of magic behind the scene. In most cases it guesses right what users want (like here), and on rarer occasions it trips you... – Julien Apr 05 '23 at 09:23
  • Another trial just confirmed your theory. – AlgebraicsAnonymous Apr 05 '23 at 09:27
  • Still, I would be happy to get some insight as to what does happen behind the scenes of ```plt```. – AlgebraicsAnonymous Apr 05 '23 at 09:28
  • 1
    `plt.plot` first looks whether there is already a figure created. If not, it creates a fresh one. Then it looks whether there is a subplot (`ax`) available. If not, it creates one. The functions `plt.gcf()` (get current figure) and `plt.gca()` (get current ax) gives you access to that figure and subplot. See also [this tutorial](https://matplotlib.org/matplotblog/posts/pyplot-vs-object-oriented-interface/) – JohanC Apr 05 '23 at 09:59
  • ...and if there is a created figure, ```plt.plot``` presumably plots that one. Right? – AlgebraicsAnonymous Apr 05 '23 at 10:00
  • Indeed. It is important to know both the concept of `figure` and of `ax` to make sense of creating more complex plots. – JohanC Apr 05 '23 at 10:03
  • I think there are some extraneous questions among the ones this one is supposed to be a "duplicate" of. It looks like only https://stackoverflow.com/questions/43482191/matplotlib-axes-plot-vs-pyplot-plot and its answers actually match this question, and they still kind of gloss over the action-at-a-distance a bit ... – SamB Apr 07 '23 at 17:52

0 Answers0