- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Store mouse click event coordinates with Matplotlib
To store mouse event coordinates with matplotlib, we can use "button_press_event" event.−
- Set the figure size and adjust the padding between and around the subplots.
- Create a figure and a set of subplots.
- Plot a line in the range of 10
- Bind the function *onclick* to the event *button_press_event*.
- Print the x and y data of the event.
- To display the figure, use show() method.
from matplotlib import pyplot as plt plt.rcParams['backend'] = 'TkAgg' plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Function to print mouse click event coordinates def onclick(event): print([event.xdata, event.ydata]) # Create a figure and a set of subplots fig, ax = plt.subplots() # Plot a line in the range of 10 ax.plot(range(10)) # Bind the button_press_event with the onclick() method fig.canvas.mpl_connect('button_press_event', onclick) # Display the plot plt.show()
On execution, it will produce the following output:
Now, click anywhere on the plot and it will display that particular point's coordinates on the console:
[6.277811659536052 6.218189947945731] [4.9416949672083685 3.7079096112932475] [8.221254287227506 3.4145010811941963]
- Related Articles
- How to show mouse release event coordinates with Matplotlib?
- Matplotlib – How to show the coordinates of a point upon mouse click?
- How to handle a mouse right click event using jQuery?
- Binding mouse double click in Tkinter
- How to draw a line following mouse coordinates with tkinter?
- Plot parallel coordinates in Matplotlib
- How to implement click event on toolbar icon?
- Best way to store weekly event in MySQL?
- How to handle a link click event using jQuery?
- How to handle a double click event using jQuery?
- How to submit a form using jQuery click event?