Code:
import numpy as np
import matplotlib.pyplot as plt
def onclick(event):
global values
values.append([event.xdata, event.ydata])
values = []
x = np.arange(0,10,1)
y = np.arange(0,10,1)
fig, ax = plt.subplots()
plt.scatter(x,y)
fig.canvas.mpl_connect('button_press_event', onclick)
plt.show(block=False)
plt.pause(5)
plt.close()
Explanation:
First we have to write a function to get values by clicking in the plot. That is:
def onclick(event):
global values
values.append([event.xdata, event.ydata])
The following syntax helps to pick data from the plot:
fig.canvas.mpl_connect('button_press_event', onclick)
We need to define a list outside the function to save the values. That is
values = []
and declare it as a global variable inside the function. That is
global values
and append values to the variable inside the function. That is
values.append([event.xdata, event.ydata])
We need to pause plot for sometime to get values saved in the variable. For that the following script is used:
plt.show(block=False)
plt.pause(5)
plt.close()
Here, plot will the open for 5 seconds. You can also increase the time by changing the value in plt.pause().
If you any questions, please comment here.
No comments:
Post a Comment