Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Subplots | Plots Customization
course content

Зміст курсу

Ultimate Visualization with Python

SubplotsSubplots

Up until now we only created multiple plots on one Axes object using multiple calls of the plotting functions (we can combine different plot types).

Now it’s time to learn how to create multiple Axes objects and thus multiple plots on different Axes objects.

pyplot has a subplots() function exactly for this purpose. We have already used this function when we created a canvas in the first section, now we'll have a more detailed look at it.

Rows and Columns

The two most important arguments of this function are nrows and ncolumns which specify the number of rows and columns of the subplot grid respectively (their default values are 1 and 1 resulting in just one Axes object).

subplots() returns a Figure object and either an Axes object or an array of Axes objects.

Let’s have a look at an example:

We have just created a 2 by 2 subplot grid.

Note

The subplots function here returns an array of Axes objects, since there is more than one subplot. When there is an array of Axes returned the variable for storing it is often called axs (ax is mostly used for a single Axes object).

axs in our case is a two-dimensional array, hence why we should use both a row index and a column index to access a particular Axes object.

Let’s create a few plots:

The first row of the subplot grid (row 0) has two line plots and the second row (row 1) has two scatter plots.

Remember that we cannot use plt.plot() or plt.scatter() here, since we want to place each plot on a separate Axes object (subplot).

Converting to 1D Array

It is also possible to use the .ravel() method to convert 2D Axes array to 1D contiguous flattened array:

Since we have a 2x2 array, axs.ravel() returns a 1D array with four elements.

Sharing an Axis

Another two important parameters of the subplots() function are sharex and sharey which specify whether the properties should be shared across the x or y axes respectively. They both have a default value of False. Let’s change one of them in our example:

With the True value for sharex x-axis will be shared among all subplots, which makes sense here, since we have the same x-axis coordinates for all of the subplots.

Moreover, we can set sharex or sharey parameters to row ( each subplot row will share a respective axis) or column (each subplot column will share a respective axis).

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

Завдання

  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.

Все було зрозуміло?

Секція 3. Розділ 6
toggle bottom row
course content

Зміст курсу

Ultimate Visualization with Python

SubplotsSubplots

Up until now we only created multiple plots on one Axes object using multiple calls of the plotting functions (we can combine different plot types).

Now it’s time to learn how to create multiple Axes objects and thus multiple plots on different Axes objects.

pyplot has a subplots() function exactly for this purpose. We have already used this function when we created a canvas in the first section, now we'll have a more detailed look at it.

Rows and Columns

The two most important arguments of this function are nrows and ncolumns which specify the number of rows and columns of the subplot grid respectively (their default values are 1 and 1 resulting in just one Axes object).

subplots() returns a Figure object and either an Axes object or an array of Axes objects.

Let’s have a look at an example:

We have just created a 2 by 2 subplot grid.

Note

The subplots function here returns an array of Axes objects, since there is more than one subplot. When there is an array of Axes returned the variable for storing it is often called axs (ax is mostly used for a single Axes object).

axs in our case is a two-dimensional array, hence why we should use both a row index and a column index to access a particular Axes object.

Let’s create a few plots:

The first row of the subplot grid (row 0) has two line plots and the second row (row 1) has two scatter plots.

Remember that we cannot use plt.plot() or plt.scatter() here, since we want to place each plot on a separate Axes object (subplot).

Converting to 1D Array

It is also possible to use the .ravel() method to convert 2D Axes array to 1D contiguous flattened array:

Since we have a 2x2 array, axs.ravel() returns a 1D array with four elements.

Sharing an Axis

Another two important parameters of the subplots() function are sharex and sharey which specify whether the properties should be shared across the x or y axes respectively. They both have a default value of False. Let’s change one of them in our example:

With the True value for sharex x-axis will be shared among all subplots, which makes sense here, since we have the same x-axis coordinates for all of the subplots.

Moreover, we can set sharex or sharey parameters to row ( each subplot row will share a respective axis) or column (each subplot column will share a respective axis).

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

Завдання

  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.

Все було зрозуміло?

Секція 3. Розділ 6
toggle bottom row
some-alt