1 | initial version |
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.