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.
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.