def onselect(vmin, vmax): if vmin == vmax: # check if the user clicked on existing rectangle for r in reversed(onselect.rects): if r.get_x() <= vmin <= r.get_x() + r.get_width(): # delete the rectangle r.text.remove() onselect.rects.remove(r) r.remove() onselect.ax.figure.canvas.draw() break return # find a frame index which have a smallest total RMSd subset = onselect.data[vmin:vmax, vmin:vmax] totals = np.sum(subset, axis=0) mmrms = np.min(totals) / len(totals) found = np.argmin(totals) + vmin bboxprops = dict( boxstyle='round', facecolor='w', linewidth=0, alpha=0.5, ) t = onselect.ax.text( vmin + (vmax - vmin) / 2, vmin + (vmax - vmin) / 2, "RMSd: %.2f\nFrame: %d\nIn: %d-%d" % (mmrms, found, vmin, vmax), fontsize=10, va='center', ha='center', color='k', bbox=bboxprops, ) r = Rectangle( (vmin, vmin), vmax-vmin, vmax-vmin, alpha=0.5, color=next(onselect.colors), fill=False, ) r.text = t onselect.rects.append(r) onselect.ax.add_patch(r) onselect.ax.figure.canvas.draw()