1

Currently I am working on a side project that will allow users to click points on scatter plots and change their colour. I'm trying to implement a method where users can highlight these points using the RectangleSelector in matplotlib but I've hit a brick wall.

I read through a solution posted here that works independently: Choosing a box of data points from a plot How can I get the same results with that solution? Thanks! Open to using other packages as well.

testdata=df2.to_numpy()

fig, ax = plt.subplots()
fig.set_size_inches(7.5, 8.25)
coll = ax.scatter(testdata[:,0], testdata[:,1], color=["black"]*len(testdata), picker = 5,s=[10]*len(testdata))


def on_pick(event):
    if event.mouseevent.button == 1:
        print (testdata[event.ind], "clicked")
        coll._facecolors[event.ind,:] = (1, 0, 0, 1)
        coll._edgecolors[event.ind,:] = (0, 0, 0, 1)
        fig.canvas.draw()
    elif event.mouseevent.button == 3:
        print (testdata[event.ind], "unclicked")
        coll._facecolors[event.ind,:] = (0, 0, 0, 1)
        coll._edgecolors[event.ind,:] = (0, 0, 0, 1)
        fig.canvas.draw()


fig.canvas.mpl_connect('pick_event', on_pick)


plt.show()

Here's a snippet of my code. Right now I can change the colours of the scatterplot by using the Left click and Right clicks.

  • You're looking for a `on_selection`, `'plotly_selected'`, or `'plotly_selecting'` event if you want to use the box or lasso for point selection. Here's [Plotly's example](https://plotly.com/python/v3/selection-events/). – Kat Dec 12 '22 at 17:40

0 Answers0