我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用matplotlib.widgets.Button()。
def create_interface(): fig = plt.figure() ax = fig.add_subplot(211)#Obj ax2 = fig.add_subplot(212)#Acquisition ax.set_xlim(BOUND_L,BOUND_U) ax2.set_xlim(BOUND_L,BOUND_U) callback = Index() axnext = plt.axes([0.81, 0.01, 0.05, 0.05])#ButtonPosition bnext = Button(axnext, '+')#Button bnext.on_clicked(callback.next)#ButtonCallbackset return fig,ax,ax2,callback,bnext #%% Functions definition
def updatePlot(self): self.ax.clear() self.ax.plot(self.x_real,self.y_real) self.ax.scatter(self.x_sample,self.y_sample,s=100) self.ax.plot(self.x_pred,self.y_pred) if self.constraint: self.ax.plot(self.x_real,np.zeros(len(self.x_real))) else: self.ax.fill_between(self.x_pred.ravel(),self.y_pred-5*np.sqrt(self.sigma2_pred),self.y_pred+5*np.sqrt(self.sigma2_pred),color='black',alpha=0.1) #Confidence intervals ax2.clear() if ACQUIS==0: ax2.fill_between(self.x_acquis.ravel(),np.zeros(len(self.x_acquis)),[(1-x)*(max(self.y_acquis)) for x in infeasibility],color='red',alpha=0.1) ax2.fill_between(self.x_acquis.ravel(),np.zeros(len(self.y_acquis)),self.y_acquis,color='green',alpha=0.1) #%% Index #Button class for interactive simulation
def create_interface(): fig = plt.figure() ax = fig.add_subplot(231)#Obj ax2 = fig.add_subplot(234)#Acquisition axg1 = fig.add_subplot(232)#Constraint1 axg2 = fig.add_subplot(233)#Constraint2 ax.set_xlim(BOUND_L,BOUND_U) ax2.set_xlim(BOUND_L,BOUND_U) axg1.set_xlim(BOUND_L,BOUND_U) axg2.set_xlim(BOUND_L,BOUND_U) callback = Index() axnext = plt.axes([0.81, 0.01, 0.05, 0.05])#ButtonPosition bnext = Button(axnext, '+')#Button bnext.on_clicked(callback.next)#ButtonCallbackset return fig,ax,ax2,axg1,axg2,callback,bnext #%% Functions definition
def mybut(text, dummy, xl, yb, xw=0, yh=0, axisbg=None, color=0.85, fun=None, bspace=0.005): """ create axes and populate button with text, automatically adjusting xw if not given. Has a side effect on xl. (button_layout_cursor) dummy is for if and when I can place these on an obect rather than using pylab """ if axisbg==None: axisbg='lightgoldenrodyellow' global button_layout_cursor if xw==0: xw=0.015*(len(text)+1) if yh==0: yh=0.05 ## thisax=fig.add_axes([xl, yb, xw, yh], axisbg=axisbg) fundamentally wrong thisax=pl.axes([xl, yb, xw, yh], axisbg=axisbg) thisbut=Button(thisax, text) thisbut.on_clicked(fun) button_layout_cursor += xw+bspace return(thisbut)
def __init__(self, t, fig, axim, tracker, occ, pt_xz, pt_zy, body, bodies, occultors, interval = 50, gifname = None, xy = None, quiet = False): ''' ''' self.t = t self.fig = fig self.axim = axim self.tracker = tracker self.occ = occ self.pt_xz = pt_xz self.pt_zy = pt_zy self.body = body self.bodies = bodies self.occultors = occultors self.xy = xy self.pause = True self.animation = animation.FuncAnimation(self.fig, self.animate, frames = 100, interval = interval, repeat = True) # Save? if gifname is not None: self.pause = False if not gifname.endswith('.gif'): gifname += '.gif' if not quiet: print("Saving %s..." % gifname) self.animation.save(gifname, writer = 'imagemagick', fps = 20, dpi = 150) self.pause = True # Toggle button self.axbutton = pl.axes([0.185, 0.12, 0.1, 0.03]) self.button = Button(self.axbutton, 'Play', color = 'lightgray') self.button.on_clicked(self.toggle)
def draw_empty_plot(self, ax): h, = ax.plot([], [], '.', markersize=50) self.button = Button(plt.axes([0.7, 0.01, 0.1, 0.05]), 'Zero') self.button.on_clicked(self.clicked) return h,
def show(self, source): """ This function ... :param source: :return: """ # Create a plot for the source source.plot(title="Is this a " + self.description + "? (" + str(self.processed) + " out of " + str(self.number_of_files) + ")", show=False, scale="log") # Set current and previous source self.previous_source = self.current_source self.current_source = source # Axes axyes = plt.axes([0.6, 0.05, 0.1, 0.075]) axno = plt.axes([0.7, 0.05, 0.1, 0.075]) axunsure = plt.axes([0.8, 0.05, 0.1, 0.075]) axback = plt.axes([0.1, 0.05, 0.1, 0.075]) # Buttons yes_button = Button(axyes, 'Yes') yes_button.on_clicked(self.save_yes) no_button = Button(axno, 'No') no_button.on_clicked(self.save_no) unsure_button = Button(axunsure, 'Unsure') unsure_button.on_clicked(self.dont_save) back_button = Button(axback, 'Back') back_button.on_clicked(self.go_back) # Show the plot plt.show() # Increment the counter self.processed += 1 # -----------------------------------------------------------------
def _initialize_buttons(self): self._buttons = {} for key, action in self._actions.iteritems(): if action.axis_pos is None: continue button_name = '%s\n(%s)' % (action.name, action.kb) if ROS_ENABLED and action.pb: ps3_buttons = [config['inverted_ps3_button'][i] for i in action.pb] button_name += '\n(%s)' % ',\n'.join(ps3_buttons) self._buttons[key] = Button(self._axarr[action.axis_pos], button_name) self._buttons[key].on_clicked(action.func)
def updatePlot(self): self.ax.clear() self.ax.plot(self.x_real,self.y_real) self.ax.scatter(self.x_sample,self.y_sample,s=100) self.ax.plot(self.x_pred,self.y_pred) self.ax.fill_between(self.x_pred.ravel(),self.y_pred-2*np.sqrt(self.sigma2_pred),self.y_pred+2*np.sqrt(self.sigma2_pred),color='black',alpha=0.1) #Confidence intervals ax2.clear() ax2.fill_between(self.x_acquis.ravel(),np.zeros(len(self.y_acquis)),self.y_acquis,color='green',alpha=0.1) #%% Index #Button class for interactive simulation
def ShotWid(): """ this simple widget accepts a shot and sets the current one It is a function in the IntegerCtl class, so it communicates with its vars easily and calls do_shot to update the shot. THe shot pulldown stops working in python (ordinary) after 1 pulldown? """ global hist_box, select_box, wild_box # root=Tix.Tk(className='ShotSelect') # was here but needs to # be in effect before Tix.StringVar() is called top = Tix.Frame(root, bd=1, relief=Tix.RAISED) hist_box=Tix.ComboBox(top, label="Shot", editable=True, history=True, variable=shot_string, command=do_shot, options='entry.width 8 listbox.height 10 ') hist_box.pack(side=Tix.TOP, anchor=Tix.W) hist_box.set_silent('33373') hist_balloon=Tix.Balloon(top) hist_balloon.bind_widget(hist_box, balloonmsg='Choose or enter shot number, valid ones are saved here') wild_box=Tix.ComboBox(top, label="Filter", editable=1, history=1, variable=wild_string, command=update_select, options='entry.width 20 listbox.height 5 ') # allow room for expressions wild_box.pack(side=Tix.TOP, anchor=Tix.W) wild_balloon=Tix.Balloon(top) wild_balloon.bind_widget(wild_box, balloonmsg='Choose or enter new filter in one of three forms,' + 'a Python expression (must have () or []), '+ 'a directory specification including a * or ' + 'the name of a file containing lines beginning with a shot number. ' 'Results can be chosen using "Filtered Shots"') select_box=Tix.ComboBox(top, label="Filtered Shots", history=False, variable=select_string, command=do_shot, options='entry.width 8 listbox.height 40 ') btn = Tix.Button(select_box, text='Clear',command=clear_select) btn.pack(anchor=Tix.CENTER) select_box.pack(side=Tix.TOP, anchor=Tix.W) select_balloon=Tix.Balloon(top) select_balloon.bind_widget(select_box, balloonmsg='pull down to find a shot selected by "Filter""') #wild_box.set_silent('MP1') # not silent - want it all to happen, but setvar doesn't work update_select(partial_name=wild_card) top.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1) # no need in pylab provided tkagg is used root.mainloop() # in fact, may conflict and block - hard to sort out what blocks and when, why