Ask Your Question
4

What is the method to retrieve the values of the coordinates that have been clicked on a multiplot matplotlib graph?

asked 2023-05-09 13:11:15 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-05-09 13:24:02 +0000

djk gravatar image

One method to retrieve the values of the coordinates that have been clicked on a multiplot matplotlib graph is to use the mpl_connect function to connect a function to the button_press_event.

Here is an example code snippet:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)

# define the function to handle mouse clicks
def onclick(event):
    if event.inaxes:
        x, y = event.xdata, event.ydata
        print("Clicked at x = {:.2f}, y = {:.2f}".format(x, y))

# connect the function to the button_press_event
for ax in axs.flatten():
    ax.plot([1, 2, 3], [4, 5, 6])
    cid = ax.figure.canvas.mpl_connect('button_press_event', onclick)

plt.show()

In this example, the onclick function prints the x and y coordinates of the clicked point. The mpl_connect function is called for each subplot to connect the function to the button press event for each subplot.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-09 13:11:15 +0000

Seen: 11 times

Last updated: May 09 '23