To create a scatterplot with lasso selection in matplotlib, you can first import the necessary libraries:
main.py125 chars5 lines
Next, you can create your scatterplot using plt.scatter
:
main.py121 chars5 lines
To allow the user to make a lasso selection on the scatterplot, you can create a LassoSelector
object and pass it the scatterplot:
main.py285 chars10 lines
In the on_select
function, we first create a path
object from the selected vertices using path.Path(verts)
. Then, we get the offsets of the scatterplot (i.e. the x and y values) using scatter.get_offsets()
. We use the contains_points
method of the path
object to find the points in the scatterplot that fall within the lasso selection. Finally, we create a pandas DataFrame from the selected points and print it to the console.
Note that you can replace the print statement with any code that processes the selected data.
Here's the full code:
main.py545 chars22 lines
gistlibby LogSnag