Set Alternative Cursor in Matplotlib

PythonPythonBeginner
Practice Now

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

Introduction

This lab will guide you on how to set an alternative cursor on a figure canvas using Matplotlib. The alternative cursor can be any of the available cursors in the Matplotlib backend tools.

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/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) 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/AdvancedTopicsGroup -.-> matplotlib/custom_backends("`Custom Backends`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/event_handling("`Event Handling`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} matplotlib/importing_matplotlib -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} matplotlib/figures_axes -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} matplotlib/custom_backends -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} matplotlib/event_handling -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} python/variables_data_types -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} python/conditional_statements -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} python/for_loops -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} python/lists -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} python/tuples -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} python/dictionaries -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} python/function_definition -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} python/importing_modules -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} python/using_packages -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} python/data_collections -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} python/data_visualization -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} python/build_in_functions -.-> lab-48830{{"`Set Alternative Cursor in Matplotlib`"}} end

Import the necessary libraries

First, we need to import the required libraries. We will be using matplotlib.pyplot and matplotlib.backend_tools.

import matplotlib.pyplot as plt
from matplotlib.backend_tools import Cursors

Create a figure and set alternative cursors

Next, we create a figure and set the alternative cursors for each subplot using a loop. We also add text to each subplot to indicate the cursor being used.

fig, axs = plt.subplots(len(Cursors), figsize=(6, len(Cursors) + 0.5), gridspec_kw={'hspace': 0})
fig.suptitle('Hover over an Axes to see alternate Cursors')

for cursor, ax in zip(Cursors, axs):
    ax.cursor_to_use = cursor
    ax.text(0.5, 0.5, cursor.name,
            horizontalalignment='center', verticalalignment='center')
    ax.set(xticks=[], yticks=[])

Set the cursor on hover

We need to set the cursor to the alternative cursor when the user hovers over a subplot. We achieve this using the motion_notify_event event and the set_cursor() function.

def hover(event):
    if fig.canvas.widgetlock.locked():
        ## Don't do anything if the zoom/pan tools have been enabled.
        return

    fig.canvas.set_cursor(
        event.inaxes.cursor_to_use if event.inaxes else Cursors.POINTER)

fig.canvas.mpl_connect('motion_notify_event', hover)

Display the figure

Finally, we display the figure using the show() function.

plt.show()

Summary

In this lab, we have learned how to set an alternative cursor on a figure canvas using Matplotlib. We created a figure and set the alternative cursors for each subplot, and then set the cursor to the alternative cursor when the user hovers over a subplot. We then displayed the figure.

Other Python Tutorials you may like