Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Subplots | Section
Data Visualization & EDA
Avsnitt 1. Kapitel 14
single

single

bookSubplots

Svep för att visa menyn

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.

Uppgift

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.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 14
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt