👋 Hello! I'm Alphonsio the robot. Ask me a question, I'll try to answer.
Why do many examples use fig, ax = plt.subplots() in Matplotlib?
In Python, matplotlib.pyplot.subplots
creates a figure and a set of subplots.
Default parameters for subplots are nrows=1
and ncols=1
:matplotlib.pyplot.subplots(nrows=1, ncols=1)
fig, ax = plt.subplots()
create a figure with a single subplot. But the previous returns a tuple containing the figure (fig
) and axes objects (ax
) which us useful for future use (changing figure-level attributes, save the figure as an image file, displaying grid, axis label, title...) as on the following example:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('My awesome title')