Skip to content Skip to sidebar Skip to footer

Creating A Dict Of Labels And Their Point Objects From A Scatter Plot

I am creating an interactive graph where I can select the points of a scatter plot for further operations e.g. swapping positions with another point. When the point is selected, it

Solution 1:

In order to get back the original color after clicking the point you may use the event.ind and the list of colors you have used initially to colorize the points. I do not see the need for a dictionary here at all.

import matplotlib.pyplot as plt
import numpy as np

x = np.random.rand(6)
y = np.random.rand(6)

fig, ax = plt.subplots()
ax.set_facecolor("k")

fcolor = plt.cm.RdYlBu(x)
ecolor = ["k"]*6

scatter = ax.scatter(x,y, s=100, facecolors=fcolor,edgecolors=ecolor , picker=True)

def select_point(event):
    if event.mouseevent.button == 1:
        facecolor = scatter._facecolors[event.ind,:]

        if (facecolor == np.array([[0, 0, 0, 1]])).all():
            scatter._facecolors[event.ind,:] = fcolor[event.ind]
            scatter._edgecolors[event.ind,:] = (0, 0, 0, 1)
        else:
            scatter._facecolors[event.ind,:] = (0, 0, 0, 1)
            scatter._edgecolors[event.ind,:] = (1, 1, 1, 1)

        fig.canvas.draw_idle()

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

plt.show()

Post a Comment for "Creating A Dict Of Labels And Their Point Objects From A Scatter Plot"