Question #394   Submitted by Answiki on 02/08/2021 at 06:24:30 PM UTC

Why do many examples use fig, ax = plt.subplots() in Matplotlib?

Answer   Submitted by Answiki on 09/23/2022 at 12:06:55 PM UTC

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')

6 events in history
Answer by Answiki on 09/23/2022 at 12:06:55 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:

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

Question by Answiki 02/08/2021 at 06:24:56 PM
What is fig, ax = plt.subplots()
Question by Answiki 02/08/2021 at 06:24:37 PM
Why do many examples use `fig, ax = plt.subplots()` in Matplotlib/pyplot/python?
Question by Answiki 02/08/2021 at 06:24:30 PM
Why do many examples use fig, ax = plt.subplots() in Matplotlib?
Answer by Answiki on 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 Answiki 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.