Simultaneous Cursor Display in Matplotlib

PythonPythonBeginner
Practice Now

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

Introduction

This tutorial will show how to use the matplotlib.widgets.MultiCursor function to display a cursor on multiple plots simultaneously.

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 matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") 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 matplotlib/importing_matplotlib -.-> lab-48837{{"`Simultaneous Cursor Display in Matplotlib`"}} matplotlib/figures_axes -.-> lab-48837{{"`Simultaneous Cursor Display in Matplotlib`"}} matplotlib/line_plots -.-> lab-48837{{"`Simultaneous Cursor Display in Matplotlib`"}} python/booleans -.-> lab-48837{{"`Simultaneous Cursor Display in Matplotlib`"}} python/tuples -.-> lab-48837{{"`Simultaneous Cursor Display in Matplotlib`"}} python/importing_modules -.-> lab-48837{{"`Simultaneous Cursor Display in Matplotlib`"}} python/numerical_computing -.-> lab-48837{{"`Simultaneous Cursor Display in Matplotlib`"}} python/data_visualization -.-> lab-48837{{"`Simultaneous Cursor Display in Matplotlib`"}} end

Importing Libraries

The first step is to import the necessary libraries which are matplotlib.pyplot and numpy.

import matplotlib.pyplot as plt
import numpy as np

Creating Data

Next, we will create some data for our plots. In this example, we will create three sine waves with different frequencies.

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

Creating Plots

Now, we will create three subplots using the plt.subplots function. Two plots will be created in one figure, while the third plot will be created in a separate figure.

fig, (ax1, ax2) = plt.subplots(2, sharex=True)
ax1.plot(t, s1)
ax2.plot(t, s2)
fig, ax3 = plt.subplots()
ax3.plot(t, s3)

Adding MultiCursor

Finally, we will add the MultiCursor function to display a cursor on all three plots when hovering over a data point.

multi = MultiCursor(None, (ax1, ax2, ax3), color='r', lw=1)
plt.show()

Summary

In this tutorial, we learned how to use the matplotlib.widgets.MultiCursor function to display a cursor on multiple plots simultaneously. We created three sine waves with different frequencies, created three subplots, and added the MultiCursor function to display a cursor on all three plots.

Other Python Tutorials you may like