Question #660

With Matplolib, why using plt.subplots() instead of plt.plot?

Answer

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:

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('My awesome title')

5 events in history
Question by Alphonsio 02/08/2021 at 06:24:56 PM
What is fig, ax = plt.subplots()
Question by Alphonsio 02/08/2021 at 06:24:37 PM
Why do many examples use `fig, ax = plt.subplots()` in Matplotlib/pyplot/python?
Question by Alphonsio 02/08/2021 at 06:24:30 PM
Why do many examples use fig, ax = plt.subplots() in Matplotlib?
Answer by Alphonsio 02/08/2021 at 06:23:57 PM

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:

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('My awesome title')

Question by Alphonsio 02/08/2021 at 06:17:08 PM
With Matplolib, why using plt.subplots() instead of plt.plot?
# ID Query URL Count

Icons proudly provided by Friconix.