Matplotlib is a python library for visualization. It provides various widgets to make the visualization of data simple. There are some cases when there is a need of selection of particular region of a graph. For this interactivity, Matplotlib provides RectangleSelector widget. This widget helps to select a rectangular region of given axes. Also, it provides a way to perform actions according to the selection.
RectangleSelector(): Selects a rectangular region of given axes.
Syntax: class matplotlib.widgets.RectangleSelector(ax, onselect, drawtype=’box’, minspanx=0, minspany=0, useblit=False, lineprops=None, rectprops=None, spancoords=’data’, button=None, maxdist=10, marker_props=None, interactive=False, state_modifier_keys=None)
- ax: A matplotlib.axes.Axes instance where widget is placed.
- onselect: Function to connect to the selection event. When the selection is completed, respective function is called. onselect function takes mouse click and mouse release events as arguments.
- drawtype: It specifies how to show selection. If “box”, then draws the full rectangle box. If “line”, then draws line of rectangle. If “none”, then draws nothing. Default value is “box”.
- button: It provides list of mouse buttons that can trigger rectangle selection. By deafult all buttons are allowed.
- maxdist: It is a distance in pixels within which the interactive tool handles can be activated. Default value is 10.
- interactive: It is a boolean value to specify whether to draw a set of handles for interaction with the widget.
- center: Provides center of rectangle drawn
- corners: Provides corners of rectangle starting from lower left, moving clockwise.
- edge_centers: Provides midpoint of rectangle edges starting from left moving clockwise.
- extents: Returns (xmin, xmax, ymin, ymax).
- geometry: Returns an array containing the x and y co-ordinates of the four corners of the rectangle (starting and ending in the top left corner). Array shape is (2, 5). all x coordinates can be obtained using RectangleSelector.geometry[1, :] and y coordinates using RectangleSelector.geometry[0, :].
- draw_shape(self, extents): draws a rectangular shape using (xmin, xmax, ymin, ymax) values.
Following program demonstrates simple RectangleSelector used to select a region and zoom the selected area.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.