Matplotlib - Lasso Selector Widget - GeeksforGeeks

23 Feb, 2021
Matplotlib provides us with a variety of widgets. In this article, we will be learning about Lasso Selector Widget Demo. A Lasso Selector Widget is a tool that helps us to make a selection curve of arbitrary space.
We will be adding the axes manually to our plot and then use the lasso-selector-widget tool.
Implementation:
Python3
# importing matplotlib packageimport matplotlib.pyplot as plt  # importing LassoSelector from# Matplotlib.widgetsfrom matplotlib.widgets import LassoSelector  # Creating a figure of the plotfig = plt.figure()  # Add set of axes to figure(Manually)# left, bottom, width, height (ranging in between 0 and 1)axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])  # Set the label of X-Axisaxes.set_xlabel('X-axis')  # Set the label of Y-Axisaxes.set_ylabel('Y-Axis')  # Set the title of the plotaxes.set_title('LassoSelector-Demo-Widget')  # OnSelect function:Gets triggered# as soon as the mouse is pressed# in the plotdef onSelect(geeksforgeeks):    print(geeksforgeeks)  # line defines the color, width and opacity# of the line to be drawnline = {'color': 'green',         'linewidth': 8, 'alpha': 1}  # Three parameters are passed inside the lasso# Selector class defining the axis, line# property and on select functionlsso = LassoSelector(ax=axes, onselect=onSelect,                      lineprops=line, button=2)  # Show the above plotplt.show()
Output:
Explanation: 
In the above code, we are importing matplotlib package to our python project along with the LassoSelector tool from the matplotlib.widgets module. After importing the packages, we are creating a figure(i.e an empty canvas) and adding axes to it manually. Then, we are defining a function onSelect() that gets triggered as soon as the mouse is pressed in the plot. Then, we are creating line that defines the properties of the line, and then comes LassoSelector which helps us to draw inside the plots. Now inside LassoSelector there are four parameters, the first one defines the axes that we have created, the second one defines the onSelect() function, the third parameter is defining the properties of the line(line) and the last parameter defines which click of the mouse will be used to draw the plot(left, right, middle).
Here, Instead of manually adding axes, we can also do it using plt.subplots which automatically creates axes.
Python3
# importing matplotlib packageimport matplotlib.pyplot as plt  # importing LassoSelector from# Matplotlib.widgetsfrom matplotlib.widgets import LassoSelector  # Creating a Subplot in matplotlibfig, axes = plt.subplots()  # Set the label of X-Axisaxes.set_xlabel('X-axis')  # Set the label of Y-Axisaxes.set_ylabel('Y-Axis')  # Set the title of the plotaxes.set_title(    'LassoSelector-Demo-Widget with axes created automatically with subplots')  # onSelect function gets triggered# as soon as the mouse is pressed# in the plotdef onSelect(geeksforgeeks):    print(geeksforgeeks)  # line defines the color, width and opacity# of the line to be drawnline = {'color': 'green',        'linewidth': 8, 'alpha': 1}  # Three parameters are passed inside the lasso# Selector class defining the axis, line# property and on select functionlsso = LassoSelector(ax=axes, onselect=onSelect,                      lineprops=line, button=2)  # If you want to print x and y while pressing# and releasing mouse, then use mpl_connect# and replace pressed and released with event  # Shows the above plotplt.show()
Output:
Explanation:
The same series of events are taking place just like our previous approach. The only difference is that here we are automatically creating axes with the help of plt.subplots(). If you go through the terminal, you can see a large number of coordinates which are the points on which we have drawn in the graph. 
Picked
Python-matplotlib
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python