SpanSelector documentation mentions: "To guarantee that the selector remains responsive, keep a reference to it."
Not sure how to incorporate it inside code that I'm writing. My code is below. Would appreciate direct modifications to the code as I'm not able to understand what "keep a reference to it" exactly means.
The idea is to initialize two graphs based on data x and y, select a span from the first graph that is then plotted onto the second graph.
def peaks(x,y):
%matplotlib qt
def onselect(xmin, xmax):
print(xmin,xmax)
indmin, indmax = np.searchsorted(x, (xmin, xmax))
indmax = min(len(x) - 1, indmax)
print(indmin, indmax)
region_x = x[indmin:indmax]
region_y = y[indmin:indmax]
if len(region_x) >= 2:
graph2.set_data(region_x, region_y)
ax2.set_xlim(region_x[0], region_x[-1])
ax2.set_ylim(region_y.min(), region_y.max())
fig.canvas.draw_idle()
fig, (ax, ax2) = plt.subplots(2, figsize = (5,2.5), dpi=300)
ax.plot(x, y)
ax.grid()
graph2, = ax2.plot([], [])
span = SpanSelector(
ax,
onselect,
"horizontal",
useblit=True,
props=dict(alpha=0.5, facecolor="tab:blue"),
interactive=True,
drag_from_anywhere=True
)
Works perfectly well when the code is not bundled into the method 'peaks'. I.e. when all the code is indented one tab to the left and without "peaks(x,y):" as the parents method.