class DataPicker(object): """Allows for brushing of flowers.csv data set """ def __init__(self, figure, axis=None, tab=None, catNames=None, flowerColor=None): self.x1, self.y1=[],[] self.x2, self.y2=[],[] self.reset=[] self.patch=[] self.tab=tab self.catNames=catNames self.flowerColor=flowerColor self.textListSt=np.copy(textList) # Connect figure to events self.cidclick = figure.canvas.mpl_connect('button_press_event', self) self.cidrelease = figure.canvas.mpl_connect('button_release_event', self.on_release) self.keypress=figure.canvas.mpl_connect('key_press_event', self.onpress) if axis is None: axis = figure.axes[0] self.axis=axis self.figure=figure self.rect = Rectangle((0,0), 1, 1) def __call__(self, event): # Get first set of x and y values self.x1, self.y1 = event.xdata, event.ydata # Get axis location of the mouse self.axis=event.inaxes # make sure the rectangle drawn in that subplot self.rect.set_axes(event.inaxes) # 0,0 = bottom def on_release(self, event): if event.inaxes != self.axis: return # If rectangle is still present, remove it if self.patch !=[] and self.reset !=[]: self.patch.remove() newtab=[] # Get second set of x and y values self.x2, self.y2=event.xdata, event.ydata # Calculate rectangle width and height rectWidth=self.x2-self.x1 rectHeight=self.y2-self.y1 # Make a new rectangle self.rect=Rectangle((self.x1, self.y1), rectWidth, rectHeight, color='.75', alpha=.5) # Add a new rectangle to the axis self.patch=self.axis.add_patch(self.rect) self.reset=1 # Find indices in rectangle, set those indices to NullFlow in data datCopy=np.copy(self.tab) newtab=findPoints(self.axis, self.x1, self.y1, self.x2, self.y2, datCopy, self.catNames) # Draw a new scatter plot with indices outside of the rectangle grayed out drawScatter(self.axis, newtab, self.catNames, self.flowerColor) self.axis.figure.canvas.draw() def onpress(self, event): # Quit if the user hits 'q' if event.key in ('q','Q'): plt.close(); # Remove the rectangle if the user hits 'd' if event.key in ('d','D'): drawScatter(self.axis, self.tab, self.catNames, self.flowerColor) if self.patch !=[]: self.patch.remove() self.reset=[]