I want to use the onclick method to select a range of data on my matplotlib graph. But the thing is, I can't do that more than once and update the plot. I have some ideas to do this, one of them would be to make a list of plots where I jump to new indices after I appended the new picture ... but mostly I would like to be able to store the information from the click (event.xdata) two times to color the area under the graph in that section - but for starters it would already be an achievement to draw points wherever I click. But I kind of think there is a better solution than putting a plt.draw() in the onclick function?
import numpy as np
import matplotlib.pyplot as plt
from itertools import islice
class ReadFile():
def __init__(self, filename):
self._filename = filename
def read_switching(self):
return np.genfromtxt(self._filename, unpack=True, usecols={0}, delimiter=',')
def onclick(event):
global ix, iy
ix, iy = event.xdata, event.ydata
global coords
coords.append((ix, iy))
print(coords)
fig.canvas.mpl_disconnect(cid)
return coords
coords = []
filename = 'test.csv'
fig = plt.figure()
ax = fig.add_subplot(111)
values = (ReadFile(filename).read_switching())
steps = np.arange(1, len(values)+1)*2
graph_1, = ax.plot(steps, values, label='original curve')
cid = fig.canvas.mpl_connect('button_press_event', onclick)
print(coords)
graph_2, = ax.plot(coords, marker='o')
plt.show()
For example I have the following function (picture) and I want to click on two coordinates and color the area under the graph, probably with a plt.draw().
