我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用matplotlib.widgets.Cursor()。
def _connect_callbacks(self): """ Connects all of the callbacks for the motion and click events """ self._disconnect_callbacks() self._cur = Cursor(self._im_ax, useblit=True, color='red', linewidth=2) self._move_cid = self._fig.canvas.mpl_connect('motion_notify_event', self._move_cb) self._click_cid = self._fig.canvas.mpl_connect('button_press_event', self._click_cb) self._clear_cid = self._fig.canvas.mpl_connect('draw_event', self._clear) self._fig.tight_layout() self._fig.canvas.draw_idle()
def init_qt(self): """docstring for set_qt""" self.in_qt = True if len(self.axes) == 1: self.cross_cursor = Cursor(self.axes[0], useblit=True, color='red', linewidth=2, vertOn=True, horizOn=True) else: self.v_cursor = MultiCursor(self.fig.canvas, self.fig.axes, color='r', lw=2, horizOn=False, vertOn=True)
def enter_axes(self, event): #event.inaxes.patch.set_facecolor('yellow') # ????axes???? if not self.in_qt: axes = [ax for ax in self.fig.axes if ax is not event.inaxes] self.v_cursor = MultiCursor(event.canvas, axes, color='r', lw=2, horizOn=False, vertOn=True) self.cross_cursor = Cursor(event.inaxes, useblit=True, color='red', linewidth=2, vertOn=True, horizOn=True) event.canvas.draw() print("enter---")
def __init__(self,canvas_,parentframe_,parclass): self.parclass = parclass # images: Python34\Lib\site-packages\matplotlib\mpl-data\images self.toolitems = ( ('Home', 'original scale', 'home', 'home'), #('Back', 'consectetuer adipiscing elit', 'back', 'back'), #('Forward', 'sed diam nonummy nibh euismod', 'forward', 'forward'), (None, None, None, None), ('Pan', 'pan', 'move', 'pan'), (None, None, None, None), ('Zoom', 'zoom', 'zoom_to_rect', 'zoom'), (None, None, None, None), ('Print','Print','printer','onPrint'), (None, None, None, None), ('PDF','PDF','pdf','onPDF'), (None, None, None, None), #('Subplots', 'putamus parum claram', 'subplots', 'configure_subplots'), ('Save', 'save to file', 'filesave', 'save_figure'), (None, None, None, None), ('ShowPage1', 'show page 1', 'page1', 'onShowPage1'), (None, None, None, None), ('ShowPage2', 'show page 2', 'page2', 'onShowPage2'), (None, None, None, None), ('NoVS', 'no VS', 'novs', 'onNoVS'), (None, None, None, None), (None, None, None, None), ('Cursor','Cursor','cursor','onCursor'), (None, None, None, None), ) NavigationToolbar.__init__(self,canvas_,parentframe_,False)
def _init_toolbar(self): self.basedir = os.path.join(matplotlib.rcParams['datapath'], 'images') for text, tooltip_text, image_file, callback in self.toolitems: if text is None: self.addSeparator() else: a = self.addAction(self._icon(image_file + '.png'), text, getattr(self, callback)) self._actions[callback] = a if callback in ['zoom', 'pan','onCursor']: a.setCheckable(True) if tooltip_text is not None: a.setToolTip(tooltip_text) self.buttons = {} self.cbA = QRadioButton('Cursor A ->',self) self.cbA.setChecked(True) self.labelcA = QLabel('') self.labelcA.setMinimumSize(100,30) self.cbB = QRadioButton('Cursor B ->',self) self.labelcB = QLabel('') self.labelcB.setMinimumSize(100, 30) self.labelcC = QLabel('Delta ->') self.labelcC.setMinimumSize(100,30) self.labelcD = QLabel('Mouse ->') self.labelcD.setMinimumSize(100,30) self.addWidget(self.cbA) self.addWidget(self.labelcA) self.addWidget(self.cbB) self.addWidget(self.labelcB) self.addWidget(self.labelcC) self.addWidget(self.labelcD) self.cbA.toggled.connect(self.onRBtoggled) self.adj_window = None
def toggleCursor(self): print('toggleCursor') if self.cursorActive: inv = self.host.transData.inverted() _xlim = self.host.get_xlim() _ylim = self.host.get_ylim() _xy1 = self.host.transData.transform((_xlim[0],_ylim[1])) _xy2 = self.host.transData.transform((_xlim[1],_ylim[0])) #_xy1 = self.host.transData.transform((_posx[0],_posy[0])) #ä_xy2 = self.host.transData.transform((_posx[1],_posy[1])) _x1 = _xy1[0]+20 _x2 = _xy1[0]+40 _y1 = _xy2[1]+20 _y2 = _xy2[1]+40 _xy1t = inv.transform((_x1,_y1)) _xy2t = inv.transform((_x2,_y2)) self.cursorA = CursorStatic(self.host,_xy1t[0],_xy1t[1],'gray',self) self.cursorB = CursorStatic(self.host,_xy2t[0],_xy2t[1],'black',self) self.showCursorPos(self.cursorA.x,self.cursorA.y,'A') self.showCursorPos(self.cursorB.x,self.cursorB.y,'B') self.showCursorPos(math.fabs(self.cursorB.x-self.cursorA.x),math.fabs(self.cursorB.y-self.cursorA.y),'C') # # self.line = Line2D(_xy1,_xy2) # self.line1 = Line2D(_xlim, (_xy2t[1],_xy2t[1])) # self.line2 = Line2D((_xy1t[0],_xy1t[0]), _ylim) # self.host.add_line(self.line1) # self.host.add_line(self.line2) # self.signalGraphUpdate.emit() print ('cursor added') # self.cursorD = Cursor(self.host, useblit=False, color='blue', linewidth=2) else: self.cursorA.delLine() self.cursorA = None self.cursorB.delLine() self.cursorB = None
def onMouseMotion(self,event): inv = self.host.transData.inverted() pos = inv.transform((event.x,event.y)) if self.cursorActive: if event.button == 1: #QtGui.QApplication.setOverrideCursor((QtGui.QCursor(Qt.CrossCursor))) #activate Crossline Cursor if self.cursorD is None: self.cursorD = Cursor(self.host, useblit=False) else: self.cursorD.set_active(True) if self.mpl_toolbar.cbA.isChecked(): if not self.cursorA is None: #delete static Cursorline if Cursor is active self.cursorA.delLine() self.cursorA = None self.showCursorPos(pos[0], pos[1], 'A') self.showCursorPos(math.fabs(pos[0]-self.cursorB.x), math.fabs(pos[1]-self.cursorB.y), 'C') self.cursorD.lineh.set_color('gray') self.cursorD.linev.set_color('gray') else: if not self.cursorB is None: #delete static Cursorline if Cursor is active self.cursorB.delLine() self.cursorB = None self.showCursorPos(pos[0], pos[1], 'B') self.showCursorPos(math.fabs(pos[0]-self.cursorA.x), math.fabs(pos[1]-self.cursorA.y), 'C') self.cursorD.lineh.set_color('black') self.cursorD.linev.set_color('black') event.inaxes = self.host self.cursorD.onmove(event) else: self.showCursorPos(pos[0],pos[1],'D') #QtGui.QApplication.restoreOverrideCursor() else: self.showCursorPos(pos[0], pos[1], 'D')
def plot_entry(fig, exit_profit, entry_best, entry_worst, entry_nbar_best, entry_nbar_worst, nbar, binwidth=1): fig.canvas.set_window_title(u'??') axescolor = '#f6f6f6' # the axes background color left, width = 0.1, 0.8 rect1 = [left, 0.7, width, 0.2]#left, bottom, width, height rect2 = [left, 0.3, width, 0.4] rect3 = [left, 0.1, width, 0.2] ax1 = fig.add_axes(rect1, axisbg=axescolor) ax2 = fig.add_axes(rect2, axisbg=axescolor, sharex = ax1) ax3 = fig.add_axes(rect3, axisbg=axescolor, sharex = ax1) (entry_best-exit_profit).plot(ax=ax1, kind='bar', grid = False, use_index = False, label=u"?") entry_worst.plot(ax=ax1, kind='bar', grid = False, use_index = False, color = 'y', label=u"?") if nbar>0: entry_nbar_best.plot(ax=ax3, kind='bar', color='red', grid=False, use_index=False, label=u"%s"%nbar) #ax3.bar(range(len(entry_nbar_best)), entry_nbar_best, color='r', label=u"%s"%nbar) entry_nbar_worst.plot(ax=ax3, kind='bar', color='y', grid=False, use_index=False, label=u"%s"%nbar) temp = entry_nbar_worst[entry_nbar_worst<0] ax3.plot(range(len(entry_nbar_best)), [temp.mean()]*len(entry_nbar_best), 'y--', label=u"?: %s"%temp.mean()) temp = entry_nbar_best[entry_nbar_best>0] ax3.plot(range(len(entry_nbar_best)), [temp.mean()]*len(entry_nbar_best), 'r--', label=u'?: %s'%temp.mean() ) ax3.legend(loc='upper left',prop=font).get_frame().set_alpha(0.5) for i in xrange(len(exit_profit)): if(entry_best[i]>0 and exit_profit[i]>0): px21 = ax2.bar(i, exit_profit[i], width=binwidth, color='blue') px22 = ax2.bar(i, entry_best[i]-exit_profit[i], width=binwidth, color='red', bottom = exit_profit[i]) elif(entry_best[i]<0 and exit_profit[i]<0): ax2.bar(i, entry_best[i], width=binwidth, color='red') ax2.bar(i, exit_profit[i]-entry_best[i], width=binwidth, color='blue', bottom = entry_best[i]) else: ax2.bar(i, entry_best[i], width=binwidth, color='red') ax2.bar(i, exit_profit[i], width=binwidth, color='blue') ax2.legend((px21[0], px22[0]), (u'??', u'?'), loc='upper left', prop=font).get_frame().set_alpha(0.5) ax1.legend(loc='upper left', prop=font).get_frame().set_alpha(0.5) ax1.set_ylabel(u"???", fontproperties = font) ax2.set_ylabel(u"??", fontproperties = font) for ax in ax1, ax2, ax3: #if ax!=ax3: ax.set_xticklabels([]) ax3.set_xlabel("") ax1.set_title(u"??", fontproperties=font_big) c1 = Cursor(ax2, useblit=True, color='red', linewidth=1, vertOn = True, horizOn = True) multi = MultiCursor(fig.canvas, fig.axes, color='r', lw=1, horizOn=False, vertOn=True) #handle = EventHandler(exit_profit, fig) #fig.canvas.mpl_connect('motion_notify_event', handle.on_move) #fig.canvas.mpl_connect('pick_event', handle.on_pick) def format_coord(x, y): """ ???? """ i = int(x)/1 c = pd.to_datetime(exit_profit.index[i]).strftime("%Y-%m-%d %H:%M:%S") + " Profit: %s MAE: %s"%(exit_profit[i], entry_worst[i]) return str(c) ax1.format_coord = format_coord ax2.format_coord = format_coord ax3.format_coord = format_coord return [ax1, ax2, ax3], [multi, c1]
def plot_scatter(fig, x, y, x2, y2, binnum): '''docstring for plot_test''' fig.canvas.set_window_title(u'?') # definitions for the axes left, width = 0.1, 0.65 bottom, height = 0.1, 0.65 bottom_h = left_h = left+width+0.02 rect_scatter = [left, bottom, width, height] rect_histx = [left, bottom_h, width, 0.2] rect_histy = [left_h, bottom, 0.2, height] # start with a rectangular Figure axScatter = plt.axes(rect_scatter) axHistx = plt.axes(rect_histx) axHisty = plt.axes(rect_histy) cursor = Cursor(axScatter, useblit=True, color='red', linewidth=1 ) axScatter.plot(x, y, 'o', color = 'red') axScatter.plot(x2, y2, 'o', color = 'blue') # now determine nice limits by hand: xmax = np.max(x+x2) xmin = np.min(x+x2) binwidth = xmax / binnum lim = ( int(xmax/binwidth) + 1) * binwidth bins = np.arange(-lim, lim + binwidth, binwidth) axHistx.hist(x+x2, bins=bins) ymax = np.max(y+y2) ymin = np.min(y+y2) binwidth = ymax/binnum lim = ( int(ymax/binwidth) + 1) * binwidth bins = np.arange(-lim, lim + binwidth, binwidth) axHisty.hist(y, bins=bins, orientation='horizontal', color = 'red' ) axHisty.hist(y2, bins=bins, orientation='horizontal', color = 'blue' ) xymax = np.max( [np.max(np.fabs(x+x2)), np.max(np.fabs(y+y2))] ) lim = ( int(xymax/binwidth) + 1) * binwidth axScatter.axhline(color='black') #axScatter.set_xlim( (-xmin-10, xmax+10)) #axScatter.set_ylim((-ymin-10, ymax+10)) axHistx.set_xlim( axScatter.get_xlim() ) axHisty.set_ylim( axScatter.get_ylim() ) axHisty.set_xlabel(u"??", fontproperties = font_big) axHistx.set_ylabel(u"??", fontproperties = font_big) axScatter.set_xlabel(u"???", fontproperties = font_big) axScatter.grid(True) axHistx.grid(True) axHisty.grid(True) c = Cursor(axScatter, useblit=True, color='red', linewidth=1, vertOn = True, horizOn = True) return [axScatter, axHistx, axHisty], [c]