Retrieving the Matplotlib Artist from a Mouse Click Event

What will you learn?

In this tutorial, you will learn how to identify and retrieve the specific Matplotlib artist (e.g., line, patch, image) that received a mouse click event in the click handler. This knowledge will allow you to interact with and manipulate individual elements within your Matplotlib plots effectively.

Introduction to the Problem and Solution

When working with Matplotlib plots, it is common to encounter scenarios where you need to pinpoint which artist on the plot is being interacted with through a mouse click event. By solving this problem, you can enable interactive features in your visualizations and provide users with the ability to engage directly with plotted elements.

To address this challenge, we leverage event handling functions provided by Matplotlib. By accessing the artists embedded in our plot and implementing appropriate event listeners, we can precisely determine the artist associated with each mouse click.


import matplotlib.pyplot as plt

def onclick(event):
    print("Artist clicked:", event.artist)

fig, ax = plt.subplots()
line, = ax.plot([1, 2, 3], [4, 5, 6]) # Example artist - a line

fig.canvas.mpl_connect('button_press_event', onclick)

# Credits:

# Copyright PHD


The code snippet above illustrates how to retrieve the Matplotlib artist that received a mouse click event: – We import matplotlib.pyplot as plt. – A function onclick is defined to display information about the clicked artist. – A simple plot is created with a line element using ax.plot. – The onclick function is connected to the ‘button_press_event’ of the figure canvas. – Upon clicking on any part of the plot, details about the corresponding artist are printed.

    1. How do I handle clicks on multiple artists in a plot? You can manage clicks on multiple artists by distinguishing between them within your event handling function based on their properties.

    2. Can I use this technique for other types of Matplotlib artists like patches or images? Yes, similar methods can be applied to patches or images by accessing their specific attributes in the event handler.

    3. Is it possible to customize actions for different clicked artists? Absolutely! You have full flexibility in defining custom responses tailored to interactions with distinct artists.

    4. What if there are no artists initially present on my plot? If no artists are initially added to your plot object, interactions through mouse events won’t be feasible until elements are incorporated.

    5. Are there advanced techniques beyond printing out clicked artists? Certainly! Advanced implementations involve updating data related to specific artists or triggering animations based on user interactions for enriched visualization experiences.

    6. Can I trigger different actions based on various types of clicked artists? Yes. By identifying each artist type within your click handler function, you can execute diverse actions based on specific artist categories (line/patch/image).

    7. How do I access additional information about the clicked point beyond its associated artist? Additional details like coordinates (event.xdata, event.ydata) and metadata concerning axes or figures can be accessed from event attributes for further processing needs.

    8. Is it possible to disable clicks for certain parts of my plot? Yes. Conditional logic within your callback function allows you to selectively enable or disable responses for particular regions in your plot according to requirements.


Mastering the retrieval of Matplotlib artists from mouse click events empowers you to create interactive visualizations where users can directly interact with individual plotted elements. Experimenting further with customized actions based on these interactions enhances user engagement and expands data exploration capabilities.

Leave a Comment