我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用matplotlib.pyplot.connect()。
def main(): args = mkArguments() image = io.imread(args.image) plt.imshow(image) points = [] def onButton(event): x, y = int(event.xdata), int(event.ydata) points.append((x, y)) print('Rectangle[{}]: at {},{}'.format('start' if len(points) % 2 != 0 else 'end', x, y)) def onKey(event): if event.key == 'q' and points: points.pop() elif event.key == 'w': exit('Error: discarding rectangles, label again') elif event.key == 'e': plt.close() plt.connect('button_press_event', onButton) plt.connect('key_press_event', onKey) plt.title('mouse press: add point, q: pop point, w: discard, e: next') plt.axis('off') plt.show() if not points or len(points) % 2 != 0: exit('Error: unable to make up rectangles from no or odd number of points') with open(args.labelFile, 'a') as handle: writer = CSVWriter(handle) for i in range(0, len(points) - 1, 2): p0, p1 = points[i], points[i+1] (x0, y0) = np.min([p0, p1], axis=0) (x1, y1) = np.max([p0, p1], axis=0) # Region is made up of: path, x0, y0, x1, y1 (bottom left, top right) writer.writerow([abspath(args.image), x0, y0, x1, y1])
def change_line_visibility(linehandlers, leg, fig): """ changes line visibility through click on legend lines plotrange must be a line handlers list e.g: linehandlers,=ax.plot(...) change_line_visibility(line[plotrange4[0]:plotrange4[-1]],leg,fig) """ # change visibility of subplots if (leg.draggable == True): leg.draggable(False) lines = linehandlers lined = dict() for legline, origline in zip(leg.get_lines(), lines): legline.set_picker(5) # 5 pts tolerance lined[legline] = origline def onpick(event): lines = linehandlers lined = dict() for legline, origline in zip(leg.get_lines(), lines): legline.set_picker(5) # 5 pts tolerance lined[legline] = origline # on the pick event, find the origz line corresponding to the # legend proxy line, and toggle the visibility legline = event.artist # print(legline) origline = lined[legline] vis = not origline.get_visible() origline.set_visible(vis) # Change the alpha on the line in the legend so we can see what lines # have been toggled if vis: legline.set_alpha(1.0) else: legline.set_alpha(0.2) fig.canvas.draw() plt.connect('pick_event', onpick)
def __init__(self, ax, x, y,ttype, ith=0, formatter=fmt): try: x = np.asarray(x, dtype='float') except (TypeError, ValueError): x = np.asarray(mdates.date2num(x), dtype='float') y = np.asarray(y, dtype='float') mask = ~(np.isnan(x) | np.isnan(y)) x = x[mask] y = y[mask] self._points = np.column_stack((x, y)) # All plots use the same pointer now # if(ith==0): self.offsets =(-20,20) # else: # self.offsets=(-20-10*ith,20+25*ith) self.type=ttype y = y[np.abs(y - y.mean()) <= 3 * y.std()] self.scale = x.ptp() self.scale = y.ptp() / self.scale if self.scale else 1 self.tree = spatial.cKDTree(self.scaled(self._points)) self.formatter = formatter self.ax = ax self.fig = ax.figure self.ax.xaxis.set_label_position('top') self.dot = ax.scatter( [x.min()], [y.min()], s=130, color='green', alpha=0.7) self.annotation = self.setup_annotation() plt.connect('motion_notify_event', self)