Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Subplots | Plots Customization
Quizzes & Challenges
Quizzes
Challenges
/
Ultimate Visualization with Python

bookSubplots

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.

12345
import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 2) plt.show()
copy

A 2 by 2 subplot grid was created.

Note
Note

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.

1234567891011121314
import 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()
copy

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:

123456789
fig, 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()
copy

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.

123456789
fig, 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()
copy

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.

Note
Study More

As usual feel free to explore more in the subplots() documentation in case you want to.

Task

Swipe to start coding

  1. Use the correct function to create a subplot grid.
  2. The grid should have 3 rows and 1 column (specify the first two parameters).
  3. Specify the rightmost keyword argument, so that x-axis will be shared among all the subplots.
  4. Store the result of the function for creating subplots in the fig and axs variables (from left to right).
  5. Place the first line plot for data_linear on the first row (row 0) of the subplot grid.
  6. Place the second line plot for data_squared on the second row (row 1) of the subplot grid.
  7. Place the third line plot for data_exp on the third row (row 2) of the subplot grid.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 6
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

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?

close

Awesome!

Completion rate improved to 3.85

bookSubplots

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.

12345
import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 2) plt.show()
copy

A 2 by 2 subplot grid was created.

Note
Note

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.

1234567891011121314
import 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()
copy

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:

123456789
fig, 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()
copy

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.

123456789
fig, 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()
copy

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.

Note
Study More

As usual feel free to explore more in the subplots() documentation in case you want to.

Task

Swipe to start coding

  1. Use the correct function to create a subplot grid.
  2. The grid should have 3 rows and 1 column (specify the first two parameters).
  3. Specify the rightmost keyword argument, so that x-axis will be shared among all the subplots.
  4. Store the result of the function for creating subplots in the fig and axs variables (from left to right).
  5. Place the first line plot for data_linear on the first row (row 0) of the subplot grid.
  6. Place the second line plot for data_squared on the second row (row 1) of the subplot grid.
  7. Place the third line plot for data_exp on the third row (row 2) of the subplot grid.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 6
single

single

some-alt