Why do many examples use `fig, ax = plt.subplots()` in Matplotlib/pyplot/python

I’m learning to use matplotlib by studying examples, and a lot of examples seem to include a line like the following before creating a single plot…

fig, ax = plt.subplots()

Here are some examples…

  • Modify tick label text
  • http://matplotlib.org/examples/pylab_examples/boxplot_demo2.html

I see this function used a lot, even though the example is only attempting to create a single chart. Is there some other advantage? The official demo for subplots() also uses f, ax = subplots when creating a single chart, and it only ever references ax after that. This is the code they use.

# Just a figure and one subplot
f, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')

5 Answers
5

Leave a Comment