Event Handling in Matplotlib
Last Updated :
18 Oct, 2021
Event based System usually is part of a recurring set of patterns. Often, they comprise of the following :
- An incoming event
- A mechanism that is used to respond to an event
- A looping construct (e.g. while loop, listener, and the message dispatch mechanism)
The events that are triggered are also a bit richer, including information like which Axes the event occurred in. The events also understand the Matplotlib coordinate system and report event locations in both pixel and data coordinates.
Syntax:
figure.canvas.mpl_connect( Event_name , callback function or method)
Parameters:
- Event_name: It could be any from the below table
- callback_function: It will define to handle the event.
List of events
Event_name | class | Description |
---|
button_press_event | MouseEvent | The mouse button is pressed |
button_release_event | MouseEvent | The mouse button is released |
draw_event | DrawEvent | The canvas draw occurs |
key_press_event | KeyEvent | A key is pressed |
key_release_event | KeyEvent | A key is released |
motion_notify_event | MouseEvent | The motion of the mouse |
pick_event | PickEvent | An object in the canvas is selected |
resize_event | ResizeEvent | The figure canvas is resized |
scroll_event | MouseEvent | The scroll wheel of the mouse is rolled |
figure_enter_event | LocationEvent | The mouse enters a figure |
axes_enter_event | LocationEvent | The mouse enters an axes object |
axes_leave_event | LocationEvent | The mouse leaves an axes object |
figure_leave_event | LocationEvent | The mouse leaves a figure |
Note: That the classes are defined in matplotlib.backend_bases
MOUSE EVENT
- button_press_event: This event involves a mouse button press
- button_release_event: This event involves a mouse button release
- scroll_event: This event involves scrolling of the mouse
- motion_notify_event: This event involves a notification pertaining to the mouse movement
Example:
We have used the mpl_connect method, which must be called if you want to provide custom user interaction features along with your plots. This method will take two arguments:
- A string value for the event, which can be any of the values listed in the Event Name column of the preceding table
- A callback function or method
Python3
# importing the necessary modules
from IPython.display import Image
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import time
import sys
import random
import matplotlib
matplotlib.use('nbagg')
class MouseEvent:
# initialization
def __init__(self):
(figure, axes) = plt.subplots()
axes.set_aspect(1)
figure.canvas.mpl_connect('button_press_event', self.press)
figure.canvas.mpl_connect('button_release_event', self.release)
# start event to show the plot
def start(self):
plt.show() # display the plot
# press event will keep the starting time when u
# press mouse button
def press(self, event):
self.start_time = time.time()
# release event will keep the track when you release
# mouse button
def release(self, event):
self.end_time = time.time()
self.draw_click(event)
# drawing the plot
def draw_click(self, event):
# size = square (4 * duration of the time button
# is keep pressed )
size = 4 * (self.end_time - self.start_time) ** 2
# create a point of size=0.002 where mouse button
# clicked on the plot
c1 = plt.Circle([event.xdata, event.ydata], 0.002,)
# create a circle of radius 0.02*size
c2 = plt.Circle([event.xdata, event.ydata], 0.02 * size, alpha=0.2)
event.canvas.figure.gca().add_artist(c1)
event.canvas.figure.gca().add_artist(c2)
event.canvas.figure.show()
cbs = MouseEvent()
# start the event
cbs.start()
Output :
Example 2:
We are going to add color using the draw_click method
Python3
def draw_click(self, event):
# you can specified your own color list
col = ['magneta', 'lavender', 'salmon', 'yellow', 'orange']
cn = random.randint(0, 5)
# size = square (4 * duration of the time button
# is keep pressed )
size = 4 * (self.end_time - self.start_time) ** 2
# create a point of size=0.002 where mouse button
# clicked on the plot
c1 = plt.Circle([event.xdata, event.ydata], 0.002,)
# create a circle of radius 0.02*size
c2 = plt.Circle([event.xdata, event.ydata], 0.02 *
size, alpha=0.2, color=col[cn])
event.canvas.figure.gca().add_artist(c1)
event.canvas.figure.gca().add_artist(c2)
event.canvas.figure.show()
Output:
Similar Reads
Matplotlib.pyplot.eventplot() in Python Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. Matplotlib.pyplot.eventplot() This function is often used to plot identical lines at a g
4 min read
Line chart in Matplotlib - Python Matplotlib is a data visualization library in Python. The pyplot, a sublibrary of Matplotlib, is a collection of functions that helps in creating a variety of charts. Line charts are used to represent the relation between two data X and Y on a different axis. In this article, we will learn about lin
6 min read
Customizing Styles in Matplotlib Here, we'll delve into the fundamentals of Matplotlib, exploring its various classes and functionalities to help you unleash the full potential of your data visualization projects. From basic plotting techniques to advanced customization options, this guide will equip you with the knowledge needed t
12 min read
Matplotlib.axes.Axes.eventplot() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
2 min read
Introduction to Matplotlib Matplotlib is a powerful and versatile open-source plotting library for Python, designed to help users visualize data in a variety of formats. Developed by John D. Hunter in 2003, it enables users to graphically represent data, facilitating easier analysis and understanding. If you want to convert y
4 min read