Multiple Figs Demo

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, you will learn how to manage multiple figures in Matplotlib's pyplot. Matplotlib is a popular data visualization library in Python, and pyplot is a sub-library within Matplotlib that provides a simple interface for creating, customizing, and organizing plots. You will learn how to create and switch between multiple figures, create subplots within each figure, and make changes to specific subplots.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedPlottingGroup(["`Advanced Plotting`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/AdvancedPlottingGroup -.-> matplotlib/subplots("`Subplots`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48841{{"`Multiple Figs Demo`"}} matplotlib/importing_matplotlib -.-> lab-48841{{"`Multiple Figs Demo`"}} matplotlib/figures_axes -.-> lab-48841{{"`Multiple Figs Demo`"}} matplotlib/line_plots -.-> lab-48841{{"`Multiple Figs Demo`"}} matplotlib/subplots -.-> lab-48841{{"`Multiple Figs Demo`"}} python/lists -.-> lab-48841{{"`Multiple Figs Demo`"}} python/tuples -.-> lab-48841{{"`Multiple Figs Demo`"}} python/importing_modules -.-> lab-48841{{"`Multiple Figs Demo`"}} python/numerical_computing -.-> lab-48841{{"`Multiple Figs Demo`"}} python/data_visualization -.-> lab-48841{{"`Multiple Figs Demo`"}} end

Import necessary libraries

The first step is to import the necessary libraries. In this case, we need matplotlib.pyplot and numpy.

import matplotlib.pyplot as plt
import numpy as np

Create data

Next, we need to create some data to plot. We will create two sine waves that we will plot in separate figures.

t = np.arange(0.0, 2.0, 0.01)
s1 = np.sin(2*np.pi*t)
s2 = np.sin(4*np.pi*t)

Create figure 1

We will begin by creating the first figure, which will contain two subplots. We will plot the first sine wave in the top subplot and twice the amplitude of the first sine wave in the bottom subplot.

plt.figure(1)

## Top subplot
plt.subplot(211)
plt.plot(t, s1)

## Bottom subplot
plt.subplot(212)
plt.plot(t, 2*s1)

Create figure 2

Next, we will create a second figure that will contain a single plot of the second sine wave.

plt.figure(2)
plt.plot(t, s2)

Make changes to figure 1

Now, we will switch back to the first figure and make some changes. We will plot the second sine wave in the top subplot using square markers, and remove the x-axis tick labels from the top subplot.

plt.figure(1)

## Top subplot
plt.subplot(211)
plt.plot(t, s2, 's')
ax = plt.gca()
ax.set_xticklabels([])

Display the figures

Finally, we will display the figures using the plt.show() function.

plt.show()

Summary

In this lab, you learned how to manage multiple figures in Matplotlib's pyplot. You learned how to create figures and subplots, switch between figures, and make changes to specific subplots. With this knowledge, you can create more complex plots with multiple figures and subplots, and customize each plot to your liking.

Other Python Tutorials you may like