Interactive Data Exploration with Matplotlib Cursor

PythonPythonBeginner
Practice Now

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

Introduction

The matplotlib.widgets.Cursor is a useful tool for exploring the data plotted on a Matplotlib graph. It allows you to interactively display the x and y values of the data point under the cursor.

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`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) 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`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48636{{"`Interactive Data Exploration with Matplotlib Cursor`"}} matplotlib/importing_matplotlib -.-> lab-48636{{"`Interactive Data Exploration with Matplotlib Cursor`"}} matplotlib/figures_axes -.-> lab-48636{{"`Interactive Data Exploration with Matplotlib Cursor`"}} matplotlib/line_plots -.-> lab-48636{{"`Interactive Data Exploration with Matplotlib Cursor`"}} python/booleans -.-> lab-48636{{"`Interactive Data Exploration with Matplotlib Cursor`"}} python/for_loops -.-> lab-48636{{"`Interactive Data Exploration with Matplotlib Cursor`"}} python/tuples -.-> lab-48636{{"`Interactive Data Exploration with Matplotlib Cursor`"}} python/importing_modules -.-> lab-48636{{"`Interactive Data Exploration with Matplotlib Cursor`"}} python/standard_libraries -.-> lab-48636{{"`Interactive Data Exploration with Matplotlib Cursor`"}} python/math_random -.-> lab-48636{{"`Interactive Data Exploration with Matplotlib Cursor`"}} python/numerical_computing -.-> lab-48636{{"`Interactive Data Exploration with Matplotlib Cursor`"}} python/data_visualization -.-> lab-48636{{"`Interactive Data Exploration with Matplotlib Cursor`"}} end

Import Required Libraries

In this step, we import the required libraries: matplotlib.pyplot and numpy.

import matplotlib.pyplot as plt
import numpy as np

Generate Data

In this step, we generate random data points using numpy.

## Fixing random state for reproducibility
np.random.seed(19680801)

## Generate random data points
x, y = 4*(np.random.rand(2, 100) - .5)

Create a Figure and Axes

In this step, we create a figure and axes object using plt.subplots.

fig, ax = plt.subplots(figsize=(8, 6))

Plot Data Points

In this step, we plot the generated data points on the axes object.

ax.plot(x, y, 'o')
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)

Create a Cursor

In this step, we create a cursor object using Cursor class and pass the axes object as an argument. We also specify the cursor color and line width.

cursor = Cursor(ax, useblit=True, color='red', linewidth=2)

Display the Plot

In this step, we display the plot using plt.show().

plt.show()

Summary

In this tutorial, we learned how to use the matplotlib.widgets.Cursor to interactively display the x and y values of the data point under the cursor. We generated random data points using numpy, created a figure and axes object, plotted the data points, created a cursor object and displayed the plot using plt.show().

Other Python Tutorials you may like