0

I have a scatter plot in python with the option picker=True

Now I want to open another python Script with a parameter based on the point I picked.

I do have a working event

self.fig.canvas.mpl_connect('pick_event', self.onpick)
def onpick(self, event):
    artist = event.artist
    print("Hello")
...
# The scatter plot is created with
plt.scatter(data[:, 0], data[:, 1], cmap=cmap, c=data[:, 2], s=100, picker=True)

Always when I click on a scatter point python prints Hello.

Now I would love to add a simple information to each artist (e.g. the order they were created as an int), so when I click it, I can access this information and use it as my parameter for the python script I want to open on click.

An other (worse) idea would be, to get the Position of that artist in my plot and then calculate my parameter based on the Position of that artist.

Sadly, I couldn't find any information if one of the ideas is possible. I found at https://matplotlib.org/2.0.0/users/artists.html that artists have an x and an y Value, but I couldn't access it.

Any suggestions?

1

1 Answer 1

0

You could try expanding on something like

import matplotlib.pyplot as plt

def onpick(event):
    artist = event.artist
    for scatter_plot in scatter_plots:
        if artist is scatter_plot.plot:
            print(scatter_plot.order)

class MyScatterPlot():
    def __init__(self, order):
        self.order = order
        self.plot = None

fig, ax = plt.subplots()

scatter_plot1 = MyScatterPlot("first")
scatter_plot2 = MyScatterPlot("second")

scatter_plot1.plot = ax.scatter([1,1,1],[1,2,3], picker=True)
scatter_plot2.plot = ax.scatter([3,3,3],[1,2,3], picker=True)

scatter_plots = [scatter_plot1, scatter_plot2]

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

fig.show()

This idea is based on the fact that you can compare your picked artist to the objects returned by the scatter function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.