Subplots
The subplots() function from pyplot is used to create multiple plots in a single figure. You have already seen it when creating a canvas; now we examine it in more detail.
Rows and Columns
The key arguments are nrows and ncolumns, which define the subplot grid. By default, both are 1, producing a single Axes.
subplots() returns a Figure and either one Axes object or an array of them, depending on the layout.
12345import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 2) plt.show()
A 2 by 2 subplot grid was created.
Since there are multiple subplots, subplots returns an array of Axes objects, usually stored in a variable called axs (singular ax is for one plot).
In this case, axs is a two-dimensional array, so you need both a row and a column index to access a specific subplot.
1234567891011121314import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(1, 11) data_squared = data_linear**2 fig, axs = plt.subplots(2, 2) axs[0, 0].plot(data_linear) axs[0, 1].plot(data_squared) axs[1, 0].scatter(data_linear, data_linear) axs[1, 1].scatter(data_linear, data_squared) plt.show()
The first row contains two line plots; the second row contains two scatter plots.
Each plot must be drawn using the method of the corresponding Axes object, not plt.plot() or plt.scatter().
Converting to 1D Array
You can flatten the 2D Axes array using .ravel() to simplify indexing:
123456789fig, axs = plt.subplots(2, 2) axs = axs.ravel() axs[0].plot(data_linear) axs[1].plot(data_squared) axs[2].scatter(data_linear, data_linear) axs[3].scatter(data_linear, data_squared) plt.show()
With a 2x2 array, axs.ravel() converts it into a 1D array containing four elements.
Sharing an Axis
The subplots() function also has sharex and sharey parameters. They control whether the x or y axes are shared across subplots. Both are set to False by default.
123456789fig, axs = plt.subplots(2, 2, sharex=True) axs = axs.ravel() axs[0].plot(data_linear) axs[1].plot(data_squared) axs[2].scatter(data_linear, data_linear) axs[3].scatter(data_linear, data_squared) plt.show()
Setting sharex=True shares the x-axis across all subplots.
You may also pass 'row' or 'col' to share axes only within rows or columns.
As usual feel free to explore more in the
subplots() documentation in case you want to.
Swipe to start coding
- Use the correct function to create a subplot grid.
- The grid should have 3 rows and 1 column (specify the first two parameters).
- Specify the rightmost keyword argument, so that x-axis will be shared among all the subplots.
- Store the result of the function for creating subplots in the
figandaxsvariables (from left to right). - Place the first line plot for
data_linearon the first row (row0) of the subplot grid. - Place the second line plot for
data_squaredon the second row (row1) of the subplot grid. - Place the third line plot for
data_expon the third row (row2) of the subplot grid.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain more about how to access individual subplots in the 2D array?
What happens if I create a grid with more rows or columns?
How do I customize the appearance of each subplot?
Awesome!
Completion rate improved to 3.85
Subplots
Swipe to show menu
The subplots() function from pyplot is used to create multiple plots in a single figure. You have already seen it when creating a canvas; now we examine it in more detail.
Rows and Columns
The key arguments are nrows and ncolumns, which define the subplot grid. By default, both are 1, producing a single Axes.
subplots() returns a Figure and either one Axes object or an array of them, depending on the layout.
12345import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 2) plt.show()
A 2 by 2 subplot grid was created.
Since there are multiple subplots, subplots returns an array of Axes objects, usually stored in a variable called axs (singular ax is for one plot).
In this case, axs is a two-dimensional array, so you need both a row and a column index to access a specific subplot.
1234567891011121314import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(1, 11) data_squared = data_linear**2 fig, axs = plt.subplots(2, 2) axs[0, 0].plot(data_linear) axs[0, 1].plot(data_squared) axs[1, 0].scatter(data_linear, data_linear) axs[1, 1].scatter(data_linear, data_squared) plt.show()
The first row contains two line plots; the second row contains two scatter plots.
Each plot must be drawn using the method of the corresponding Axes object, not plt.plot() or plt.scatter().
Converting to 1D Array
You can flatten the 2D Axes array using .ravel() to simplify indexing:
123456789fig, axs = plt.subplots(2, 2) axs = axs.ravel() axs[0].plot(data_linear) axs[1].plot(data_squared) axs[2].scatter(data_linear, data_linear) axs[3].scatter(data_linear, data_squared) plt.show()
With a 2x2 array, axs.ravel() converts it into a 1D array containing four elements.
Sharing an Axis
The subplots() function also has sharex and sharey parameters. They control whether the x or y axes are shared across subplots. Both are set to False by default.
123456789fig, axs = plt.subplots(2, 2, sharex=True) axs = axs.ravel() axs[0].plot(data_linear) axs[1].plot(data_squared) axs[2].scatter(data_linear, data_linear) axs[3].scatter(data_linear, data_squared) plt.show()
Setting sharex=True shares the x-axis across all subplots.
You may also pass 'row' or 'col' to share axes only within rows or columns.
As usual feel free to explore more in the
subplots() documentation in case you want to.
Swipe to start coding
- Use the correct function to create a subplot grid.
- The grid should have 3 rows and 1 column (specify the first two parameters).
- Specify the rightmost keyword argument, so that x-axis will be shared among all the subplots.
- Store the result of the function for creating subplots in the
figandaxsvariables (from left to right). - Place the first line plot for
data_linearon the first row (row0) of the subplot grid. - Place the second line plot for
data_squaredon the second row (row1) of the subplot grid. - Place the third line plot for
data_expon the third row (row2) of the subplot grid.
Solution
Thanks for your feedback!
single