def overlay(self, plot, gc, view_bounds=None, mode="normal"): if self.pt is not None: x, y = self.pt_plot.map_data(self.pt) #x,y = plot.map_screen((x,y)) #gc.draw_rect((int(x)-2, int(y)-2, 4, 4)) d = 0 t = 0 if (len(labelItems) > 0): for n, e, depth, dtime in labelItems: if np.sqrt(np.add(np.square(x - e), np.square(y - n))) < 1e-3: d = depth t = dtime break text = 'Depth & Time (max): %d, %d' % (d, t) label = DataLabel( component=plot, data_point=(x, y), label_position="bottom right", border_visible=False, bgcolor="transparent", marker_color="blue", marker_line_color="transparent", marker="diamond", marker_size=2, font='modern 14', label_text=text, #label_format="%(x).2f, %(y).2f", label_format="", arrow_visible=False) label.overlay(plot, gc)
def overlay(self, plot, gc, view_bounds=None, mode="normal"): if self.pt is not None: x,y = self.pt_plot.map_data(self.pt) #x,y = plot.map_screen((x,y)) #gc.draw_rect((int(x)-2, int(y)-2, 4, 4)) d = 0 t = 0 if(len(labelItems) > 0): for n, e, depth, dtime in labelItems: if np.sqrt(np.add(np.square(x-e),np.square(y-n))) < 1e-3: d = depth t = dtime break text = 'Depth & Time (max): %d, %d' % (d, t) label = DataLabel(component=plot, data_point=(x, y), label_position="bottom right", border_visible=False, bgcolor="transparent", marker_color="blue", marker_line_color="transparent", marker="diamond", marker_size=2, font='modern 14', label_text=text, #label_format="%(x).2f, %(y).2f", label_format="", arrow_visible=False) label.overlay(plot, gc)
def _plot_default(self): container = OverlayPlotContainer(padding = 50, fill_padding = True, bgcolor = "lightgray", use_backbuffer=True) # Create the initial X-series of data numpoints = self.numpoints low = self.low high = self.high x = arange(low, high+0.001, (high-low)/numpoints) y = jn(0, x) plot = create_line_plot((x,y), color=tuple(COLOR_PALETTE[0]), width=2.0) plot.index.sort_order = "ascending" plot.bgcolor = "white" plot.border_visible = True add_default_grids(plot) add_default_axes(plot) # Add some tools plot.tools.append(PanTool(plot)) zoom = ZoomTool(plot, tool_mode="box", always_on=False) plot.overlays.append(zoom) # Add a dynamic label. This can be dragged and moved around using the # right mouse button. Note the use of padding to offset the label # from its data point. label = DataLabel(component=plot, data_point=(x[40], y[40]), label_position="top left", padding=40, bgcolor = "lightgray", border_visible=False) plot.overlays.append(label) tool = DataLabelTool(label, drag_button="right", auto_arrow_root=True) label.tools.append(tool) # Add some static labels. label2 = DataLabel(component=plot, data_point=(x[20], y[20]), label_position="bottom right", border_visible=False, bgcolor="transparent", marker_color="blue", marker_line_color="transparent", marker = "diamond", arrow_visible=False) plot.overlays.append(label2) label3 = DataLabel(component=plot, data_point=(x[80], y[80]), label_position="top", padding_bottom=20, marker_color="transparent", marker_size=8, marker="circle", arrow_visible=False) plot.overlays.append(label3) container.add(plot) return container
def _rebuild_labels(self): for dl in self.labels: if dl in self.plot.overlays: self.plot.overlays.remove(dl) self.labels = [] if self.showlabels: majorlines = self.majorlineeditor.selectedobjs linestrs = [str(l) for l in majorlines] linelocs = [l.loc for l in majorlines] lfact = self.z + 1 xd, yd = self.plot.data.get_data('x'), self.plot.data.get_data( 'flux') for s, l in zip(linestrs, linelocs): x = l * lfact y = np.interp(x, xd, yd) # xs,ys = self.plot.map_screen((x,y)) # x,y = self.plot.map_data((xs,ys+50)) dl = DataLabel(self.plot, data_point=(x, y), lines=[s], label_position="top", padding_bottom=10, arrow_visible=False) self.labels.append(dl) self.plot.overlays.append(dl)
def findMinMax(widget): ''' Find the min and max value within all shown plots and display them ''' for (modelNumber, modelName, variableName), data in widget.getData(): # print "var:", var, "data: ", data minVal = (0, float("inf")) maxVal = (0, float("-inf")) for time, value in data: if (not widget.plot.selection) or ( time > widget.plot.selection[0] and time < widget.plot.selection[1]): if value < minVal[1]: minVal = (time, value) if value > maxVal[1]: maxVal = (time, value) print "minimum of ", modelNumber, modelName, variableName, " at ", minVal, "maximum at ", maxVal, " in interval: ", widget.plot.selection minLabel = DataLabel(component=widget.plot, data_point=minVal, label_position="top", padding_bottom=20, marker_color="transparent", marker_size=8, marker="circle", arrow_visible=False, label_format=str('Min: (%(x)f, %(y)f)')) widget.plot.overlays.append(minLabel) maxLabel = DataLabel(component=widget.plot, data_point=maxVal, label_position="top", padding_bottom=20, marker_color="transparent", marker_size=8, marker="circle", arrow_visible=False, label_format=str('Max: (%(x)f, %(y)f)')) widget.plot.overlays.append(maxLabel) labels[":".join( (modelNumber, modelName, variableName))] = (minLabel, maxLabel) widget.variableRemoved.connect(removeLabels)
def update_peak_labels(self,peak_labels,peak_list,peak_profile): for label in peak_labels: self.plot.overlays.remove(label) # for dsp in editor.dataset_peaks: peak_labels=[] new_peak_list=[] for peak in peak_list: new_peak_list.append(peak) pos_index=np.where(peak_profile.data[:,0]==peak.position) label_intensity=peak_profile.data[pos_index[0],1] label=DataLabel(component=self.plot, data_point=[peak.position,label_intensity],\ label_position="right", padding=20, arrow_visible=True, label_format='(%(x)f') self.plot.overlays.append(label) peak_labels.append(label) return peak_labels
def test_data_label_arrow_not_visible(self): # Regression test for https://github.com/enthought/chaco/issues/281 # Before the problem was fixed, this test (specifically, using # arrow_visible=False in the DataLabel constructor) would raise an # exception because of an undefined reference. size = (50, 50) plot = create_scatter_plot(data=[range(10), range(10)]) label = DataLabel(component=plot, data_point=(4, 4), marker_color="red", marker_size=3, label_position=(20, 50), label_style='bubble', label_text="Something interesting", label_format="at x=%(x).2f, y=%(y).2f", arrow_visible=False) plot.overlays.append(label) plot.outer_bounds = list(size) gc = PlotGraphicsContext(size) gc.render_component(plot)
def set_label(self, key, coordinates, **kwargs): plot = self.scan_plot if self.constant_axis == 'x': point = (coordinates[2], coordinates[1]) elif self.constant_axis == 'y': point = (coordinates[0], coordinates[2]) elif self.constant_axis == 'z': point = (coordinates[0], coordinates[1]) defaults = { 'component': plot, 'data_point': point, 'label_format': key, 'label_position': 'top right', 'bgcolor': 'transparent', 'text_color': 'black', 'border_visible': False, 'padding_bottom': 8, 'marker': 'cross', 'marker_color': 'black', 'marker_line_color': 'black', 'marker_line_width': 1.5, 'marker_size': 6, 'arrow_visible': False, 'clip_to_plot': False, 'visible': self.show_labels } defaults.update(kwargs) label = DataLabel(**defaults) index = self.get_label_index(key) if index is None: plot.overlays.append(label) else: plot.overlays[index] = label self.labels[key] = coordinates plot.request_redraw()
def _add_plot_data_labels(self, plot_render, point_data, labels, color): xname, yname = point_data x = self.data.get_data(xname) y = self.data.get_data(yname) for i, label in enumerate(labels): label_obj = DataLabel( component=plot_render, data_point=(x[i], y[i]), label_format=unicode(label), visible=self.visible_new_labels, ## marker_color = pt_color, # text_color = 'black', text_color=color, border_visible=False, marker_visible=False, # bgcolor = color, bgcolor=(0.5, 0.5, 0.5, 0.0), ## label_position = 'bottom left', ## bgcolor = 'transparent', ) plot_render.overlays.append(label_obj)
def add_target(self, key, coordinates=None): Info = self.confocal.figure.index_range.get() coordinates2 = [[], []] x1 = Info['_low_setting'] Info = self.confocal.figure.value_range.get() y1 = Info['_low_setting'] coordinates[0] = coordinates[0] * self.resolution[0] + y1 coordinates[1] = coordinates[1] * self.resolution[1] + x1 plot = self.scan_plot point = (coordinates[1], coordinates[0]) defaults = { 'component': plot, 'data_point': point, 'label_format': key, 'label_position': 'top right', 'bgcolor': 'transparent', 'text_color': 'red', 'border_visible': False, 'padding_bottom': 8, 'marker': 'cross', 'marker_color': 'red', 'marker_line_color': 'red', 'marker_line_width': 1.4, 'marker_size': 6, 'arrow_visible': False, 'clip_to_plot': False, 'visible': True } self.labels[key] = coordinates label = DataLabel(**defaults) plot.overlays.append(label) plot.request_redraw() self.labels[key] = coordinates
def add_visuals(self): """ create plot components for a rectangle view """ r = self.rectangle points = [(r.l0, r.l1), (r.h0, r.l1), (r.h0, r.h1), (r.l0, r.h1)] px, py = zip(*points) nx = 'px_{0}'.format(r.name) ny = 'py_{0}'.format(r.name) data = self.plot.data data.set_data(nx, px) data.set_data(ny, py) self.polygon = self.plot.plot( (nx, ny), #need to match image conventions type='polygon', name=r.name, edge_width=5, edge_color='white')[0] self.datalabel = DataLabel( component=self.plot, data_point=(px[3], py[3]), label_position="top right", padding_bottom=10, border_visible=False, #bgcolor=(1, 1, 0.75, 1), bgcolor="white", marker_color="transparent", marker_line_color="transparent", show_label_coords=False, #label_style='bubble', marker="diamond", font='modern 20', label_text=self.rectangle.label) self.plot.overlays.append(self.datalabel) self.set_selection()
def _render_at_indices(self, gc, screen_pts, inspect_type): """ Reimplemented to render a data label on hover. """ if inspect_type == 'hover': plot = self.component if self._hover_label is not None: plot.overlays.remove(self._hover_label) self._hover_label = None point = plot.map_data(screen_pts[0]) group_index = plot.map_index(screen_pts[0], threshold=5.0) group = self.model.map_to_row(group_index) label = DataLabel(component=plot, data_point=point, label_text='Group %s' % group, marker='circle', show_label_coords=False) plot.overlays.append(label) self._hover_label = label else: super(SafeScatterInspectorOverlay, self)._render_at_indices(gc, screen_pts, inspect_type)
def _create_label(base_plot, point, label_text, show_label_coords=False, movable=True): # Another 'bubble' label. This one sets arrow_min_length=20, so # the arrow is not drawn when the label is close to the data point. label = DataLabel(component=base_plot, data_point=point, border_padding=10, marker_color="green", marker_size=2, show_label_coords=show_label_coords, label_style='bubble', label_position=(25, 5), label_text=label_text, border_visible=False, arrow_min_length=20, font='modern 14', bgcolor=(0.75, 0.75, 0.75, 1), ) if movable: tool = DataLabelTool(label, drag_button="left", auto_arrow_root=True) label.tools.append(tool) return label
def add_visuals(self): """ create plot components for a bounding rectangle """ m = self.marker px, py = [[m.c0], [m.c1]] nx = 'px_{0}'.format(m.name) ny = 'py_{0}'.format(m.name) data = self.plot.data data.set_data(nx, px) data.set_data(ny, py) self.point = self.plot.plot( (nx, ny), #need to match image conventions type='scatter', color='white', name=m.name)[0] self.datalabel = DataLabel( component=self.plot, data_point=(px[0], py[0]), label_position="top", padding_bottom=10, border_visible=False, #bgcolor=(1, 1, 0.75, 1), bgcolor="white", marker_color="transparent", marker_line_color="transparent", show_label_coords=False, #label_style='bubble', marker="diamond", font='modern 20', label_text=self.marker.label) self.plot.overlays.append(self.datalabel) self.set_selection()
class Confocal( ManagedJob, GetSetItemsMixin ): (Item('submit_gsd_button', show_label=False), Item('remove_gsd_button', show_label=False), Item('priority_gsd'), Item('state_gsd', style='readonly'), Item('history_back_gsd', show_label=False), Item('history_forward_gsd', show_label=False), # scanner position x = Range(low=scanner.getXRange()[0], high=scanner.getXRange()[1], value=0.5*(scanner.getXRange()[0]+scanner.getXRange()[1]), desc='x [micron]', label='x [micron]', mode='slider') y = Range(low=scanner.getYRange()[0], high=scanner.getYRange()[1], value=0.5*(scanner.getYRange()[0]+scanner.getYRange()[1]), desc='y [micron]', label='y [micron]', mode='slider') z = Range(low=scanner.getZRange()[0], high=scanner.getZRange()[1], value=0.5*(scanner.getZRange()[0]+scanner.getZRange()[1]), desc='z [micron]', label='z [micron]', mode='slider') # imagging parameters x1 = Range(low=scanner.getXRange()[0], high=scanner.getXRange()[1], value=scanner.getXRange()[0], desc='x1 [micron]', label='x1', editor=TextEditor(auto_set=False, enter_set=True, evaluate=float, format_str='%.2f')) y1 = Range(low=scanner.getYRange()[0], high=scanner.getYRange()[1], value=scanner.getYRange()[0], desc='y1 [micron]', label='y1', editor=TextEditor(auto_set=False, enter_set=True, evaluate=float, format_str='%.2f')) z1 = Range(low=scanner.getZRange()[0], high=scanner.getZRange()[1], value=scanner.getZRange()[0], desc='z1 [micron]', label='z1', editor=TextEditor(auto_set=False, enter_set=True, evaluate=float, format_str='%.2f')) x2 = Range(low=scanner.getXRange()[0], high=scanner.getXRange()[1], value=scanner.getXRange()[1], desc='x2 [micron]', label='x2', editor=TextEditor(auto_set=False, enter_set=True, evaluate=float, format_str='%.2f')) y2 = Range(low=scanner.getYRange()[0], high=scanner.getYRange()[1], value=scanner.getYRange()[1], desc='y2 [micron]', label='y2', editor=TextEditor(auto_set=False, enter_set=True, evaluate=float, format_str='%.2f')) z2 = Range(low=scanner.getZRange()[0], high=scanner.getZRange()[1], value=scanner.getZRange()[1], desc='z2 [micron]', label='z2', editor=TextEditor(auto_set=False, enter_set=True, evaluate=float, format_str='%.2f')) resolution = Range(low=1, high=1000, value=100, desc='Number of point in long direction', label='resolution', auto_set=False, enter_set=True) seconds_per_point = Range(low=1e-3, high=10, value=0.005, desc='Seconds per point [s]', label='Seconds per point [s]', mode='text', auto_set=False, enter_set=True) bidirectional = Bool( True ) return_speed = Range(low=1.0, high=100., value=10., desc='Multiplier for return speed of Scanner if mode is monodirectional', label='return speed', mode='text', auto_set=False, enter_set=True) constant_axis = Enum('z', 'x', 'y', label='constant axis', desc='axis that is not scanned when acquiring an image', editor=EnumEditor(values={'x':'1:x','y':'2:y','z':'3:z',},cols=3),) # buttons history_back = Button(label='Back') history_forward = Button(label='Forward') reset_range = Button(label='reset range') reset_cursor = Button(label='reset position') # plot parameters thresh_high = Trait( 'auto', Str('auto'), Float(10000.), desc='High Limit of image plot', editor=TextEditor(auto_set=False, enter_set=True, evaluate=float)) thresh_low = Trait( 'auto', Str('auto'), Float(0.), desc='Low Limit of image plot', editor=TextEditor(auto_set=False, enter_set=True, evaluate=float)) colormap = Enum('Spectral','gray') show_labels = Bool(False) # scan data X = Array() Y = Array() image = Array() # plots plot_data = Instance( ArrayPlotData ) scan_plot = Instance( CMapImagePlot ) cursor = Instance( CursorTool2D ) zoom = Instance( AspectZoomTool ) figure = Instance( Plot ) figure_container = Instance( HPlotContainer, editor=ComponentEditor() ) z_label_text = Str('z:0.0') cursor_position = Property(depends_on=['x','y','z','constant_axis']) get_set_items=['constant_axis', 'X', 'Y', 'thresh_high', 'thresh_low', 'seconds_per_point', 'return_speed', 'bidirectional', 'history', 'image', 'z_label_text', 'resolution', 'x', 'x1', 'x2', 'y', 'y1', 'y2', 'z', 'z1', 'z2'] def __init__(self): super(Confocal, self).__init__() self.X = numpy.linspace(scanner.getXRange()[0], scanner.getXRange()[-1], self.resolution+1) self.Y = numpy.linspace(scanner.getYRange()[0], scanner.getYRange()[-1], self.resolution+1) self.image = numpy.zeros((len(self.X), len(self.Y))) self._create_plot() self.figure.index_range.on_trait_change(self.update_axis_li, '_low_value', dispatch='ui') self.figure.index_range.on_trait_change(self.update_axis_hi, '_high_value', dispatch='ui') self.figure.value_range.on_trait_change(self.update_axis_lv, '_low_value', dispatch='ui') self.figure.value_range.on_trait_change(self.update_axis_hv, '_high_value', dispatch='ui') self.zoom.on_trait_change(self.check_zoom, 'box', dispatch='ui') self.on_trait_change(self.set_mesh_and_aspect_ratio, 'X,Y', dispatch='ui') self.on_trait_change(self.update_image_plot, 'image', dispatch='ui') self.sync_trait('cursor_position', self.cursor, 'current_position') self.sync_trait('thresh_high', self.scan_plot.value_range, 'high_setting') self.sync_trait('thresh_low', self.scan_plot.value_range, 'low_setting') self.on_trait_change(self.scan_plot.request_redraw, 'thresh_high', dispatch='ui') self.on_trait_change(self.scan_plot.request_redraw, 'thresh_low', dispatch='ui') self.history = History(length = 10) self.history.put( self.copy_items(['constant_axis', 'X', 'Y', 'image', 'z_label_text', 'resolution'] ) ) self.labels = {} self.label_list = [] # scanner position @on_trait_change('x,y,z') def _set_scanner_position(self): if self.state != 'run': scanner.setPosition(self.x, self.y, self.z) @cached_property def _get_cursor_position(self): if self.constant_axis == 'x': return self.z, self.y elif self.constant_axis == 'y': return self.x, self.z elif self.constant_axis == 'z': return self.x, self.y def _set_cursor_position(self, position): if self.constant_axis == 'x': self.z, self.y = position elif self.constant_axis == 'y': self.x, self.z = position elif self.constant_axis == 'z': self.x, self.y = position # image acquisition def _run(self): """Acquire a scan""" try: self.state='run' self.update_mesh() X = self.X Y = self.Y XP = X[::-1] self.image=numpy.zeros((len(Y),len(X))) """ if not self.bidirectional: scanner.initImageScan(len(X), len(Y), self.seconds_per_point, return_speed=self.return_speed) else: scanner.initImageScan(len(X), len(Y), self.seconds_per_point, return_speed=None) """ for i,y in enumerate(Y): if threading.current_thread().stop_request.isSet(): break if i%2 != 0 and self.bidirectional: XL = XP else: XL = X YL = y * numpy.ones(X.shape) if self.constant_axis == 'x': const = self.x * numpy.ones(X.shape) Line = numpy.vstack( (const, YL, XL) ) elif self.constant_axis == 'y': const = self.y * numpy.ones(X.shape) Line = numpy.vstack( (XL, const, YL) ) elif self.constant_axis == 'z': const = self.z * numpy.ones(X.shape) Line = numpy.vstack( (XL, YL, const) ) if self.bidirectional: c = scanner.scanLine(Line, self.seconds_per_point) else: c = scanner.scanLine(Line, self.seconds_per_point, return_speed=self.return_speed) if i%2 != 0 and self.bidirectional: self.image[i,:] = c[-1::-1] else: self.image[i,:] = c[:] """ scanner.doImageLine(Line) self.image = scanner.getImage() """ self.trait_property_changed('image', self.image) if self.constant_axis == 'x': self.z_label_text='x:%.2f'%self.x elif self.constant_axis == 'y': self.z_label_text='y:%.2f'%self.y elif self.constant_axis == 'z': self.z_label_text='z:%.2f'%self.z """ # block at the end until the image is ready if not threading.current_thread().stop_request.isSet(): self.image = scanner.getImage(1) self._image_changed() """ scanner.setPosition(self.x, self.y, self.z) #save scan data to history self.history.put( self.copy_items(['constant_axis', 'X', 'Y', 'image', 'z_label_text', 'resolution'] ) ) finally: self.state = 'idle' # plotting def _create_plot(self): plot_data = ArrayPlotData(image=self.image) plot = Plot(plot_data, width=500, height=500, resizable='hv', aspect_ratio=1.0, padding=8, padding_left=32, padding_bottom=32) plot.img_plot('image', colormap=Spectral, xbounds=(self.X[0],self.X[-1]), ybounds=(self.Y[0],self.Y[-1]), name='image') image = plot.plots['image'][0] image.x_mapper.domain_limits = (scanner.getXRange()[0],scanner.getXRange()[1]) image.y_mapper.domain_limits = (scanner.getYRange()[0],scanner.getYRange()[1]) zoom = AspectZoomTool(image, enable_wheel=False) cursor = CursorTool2D(image, drag_button='left', color='blue', marker_size=1.0, line_width=1.0 ) image.overlays.append(cursor) image.overlays.append(zoom) colormap = image.color_mapper colorbar = ColorBar(index_mapper=LinearMapper(range=colormap.range), color_mapper=colormap, plot=plot, orientation='v', resizable='v', width=16, height=320, padding=8, padding_left=32) container = HPlotContainer() container.add(plot) container.add(colorbar) z_label = PlotLabel(text='z=0.0', color='red', hjustify='left', vjustify='bottom', position=[10,10]) container.overlays.append(z_label) self.plot_data = plot_data self.scan_plot = image self.cursor = cursor self.zoom = zoom self.figure = plot self.figure_container = container self.sync_trait('z_label_text', z_label, 'text') def set_mesh_and_aspect_ratio(self): self.scan_plot.index.set_data(self.X,self.Y) x1=self.X[0] x2=self.X[-1] y1=self.Y[0] y2=self.Y[-1] self.figure.aspect_ratio = (x2-x1) / float((y2-y1)) self.figure.index_range.low = x1 self.figure.index_range.high = x2 self.figure.value_range.low = y1 self.figure.value_range.high = y2 def check_zoom(self, box): li,lv,hi,hv=box if self.constant_axis == 'x': if not li<self.z<hi: self.z = 0.5*(li+hi) if not lv<self.y<hv: self.y = 0.5*(lv+hv) elif self.constant_axis == 'y': if not li<self.x<hi: self.x = 0.5*(li+hi) if not lv<self.z<hv: self.z = 0.5*(lv+hv) elif self.constant_axis == 'z': if not li<self.x<hi: self.x = 0.5*(li+hi) if not lv<self.y<hv: self.y = 0.5*(lv+hv) def center_cursor(self): i = 0.5 * (self.figure.index_range.low + self.figure.index_range.high) v = 0.5 * (self.figure.value_range.low + self.figure.value_range.high) if self.constant_axis == 'x': self.z = i self.y = v elif self.constant_axis == 'y': self.x = i self.z = v elif self.constant_axis == 'z': self.x = i self.y = v def _constant_axis_changed(self): self.update_mesh() self.image = numpy.zeros((len(self.X), len(self.Y))) self.update_axis() self.set_mesh_and_aspect_ratio() def update_image_plot(self): self.plot_data.set_data('image', self.image) def _colormap_changed(self, new): data = self.figure.datasources['image'] func = getattr(chaco.api,new) self.figure.color_mapper=func(DataRange1D(data)) self.figure.request_redraw() def _show_labels_changed(self, name, old, new): for item in self.scan_plot.overlays: if isinstance(item, DataLabel) and item.label_format in self.labels: item.visible = new self.scan_plot.request_redraw() def get_label_index(self, key): for index, item in enumerate(self.scan_plot.overlays): if isinstance(item, DataLabel) and item.label_format == key: return index return None def set_label(self, key, coordinates, **kwargs): plot = self.scan_plot if self.constant_axis == 'x': point = (coordinates[2],coordinates[1]) elif self.constant_axis == 'y': point = (coordinates[0],coordinates[2]) elif self.constant_axis == 'z': point = (coordinates[0],coordinates[1]) defaults = {'component':plot, 'data_point':point, 'label_format':key, 'label_position':'top right', 'bgcolor':'transparent', 'text_color':'black', 'border_visible':False, 'padding_bottom':8, 'marker':'cross', 'marker_color':'black', 'marker_line_color':'black', 'marker_line_width':1.5, 'marker_size':6, 'arrow_visible':False, 'clip_to_plot':False, 'visible':self.show_labels} defaults.update(kwargs) label = DataLabel(**defaults) index = self.get_label_index(key) if index is None: plot.overlays.append(label) else: plot.overlays[index] = label self.labels[key] = coordinates plot.request_redraw() def remove_label(self, key): plot = self.scan_plot index = self.get_label_index(key) plot.overlays.pop(index) plot.request_redraw() self.labels.pop(key) def remove_all_labels(self): plot = self.scan_plot new_overlays = [] for item in plot.overlays: if not ( isinstance(item, DataLabel) and item.label_format in self.labels ) : new_overlays.append(item) plot.overlays = new_overlays plot.request_redraw() self.labels.clear() @on_trait_change('constant_axis') def relocate_labels(self): for item in self.scan_plot.overlays: if isinstance(item, DataLabel) and item.label_format in self.labels: coordinates = self.labels[item.label_format] if self.constant_axis == 'x': point = (coordinates[2],coordinates[1]) elif self.constant_axis == 'y': point = (coordinates[0],coordinates[2]) elif self.constant_axis == 'z': point = (coordinates[0],coordinates[1]) item.data_point = point def update_axis_li(self): if self.constant_axis == 'x': self.z1 = self.figure.index_range.low elif self.constant_axis == 'y': self.x1 = self.figure.index_range.low elif self.constant_axis == 'z': self.x1 = self.figure.index_range.low def update_axis_hi(self): if self.constant_axis == 'x': self.z2 = self.figure.index_range.high elif self.constant_axis == 'y': self.x2 = self.figure.index_range.high elif self.constant_axis == 'z': self.x2 = self.figure.index_range.high def update_axis_lv(self): if self.constant_axis == 'x': self.y1 = self.figure.value_range.low elif self.constant_axis == 'y': self.z1 = self.figure.value_range.low elif self.constant_axis == 'z': self.y1 = self.figure.value_range.low def update_axis_hv(self): if self.constant_axis == 'x': self.y2 = self.figure.value_range.high elif self.constant_axis == 'y': self.z2 = self.figure.value_range.high elif self.constant_axis == 'z': self.y2 = self.figure.value_range.high def update_axis(self): self.update_axis_li() self.update_axis_hi() self.update_axis_lv() self.update_axis_hv() def update_mesh(self): if self.constant_axis == 'x': x1=self.z1 x2=self.z2 y1=self.y1 y2=self.y2 elif self.constant_axis == 'y': x1=self.x1 x2=self.x2 y1=self.z1 y2=self.z2 elif self.constant_axis == 'z': x1=self.x1 x2=self.x2 y1=self.y1 y2=self.y2 if (x2-x1) >= (y2-y1): self.X = numpy.linspace(x1,x2,self.resolution) self.Y = numpy.linspace(y1,y2,int(self.resolution*(y2-y1)/(x2-x1))) else: self.Y = numpy.linspace(y1,y2,self.resolution) self.X = numpy.linspace(x1,x2,int(self.resolution*(x2-x1)/(y2-y1))) # GUI buttons def _history_back_fired(self): self.stop() self.set_items( self.history.back() ) def _history_forward_fired(self): self.stop() self.set_items( self.history.forward() ) def _reset_range_fired(self): self.x1 = scanner.getXRange()[0] self.x2 = scanner.getXRange()[1] self.y1 = scanner.getYRange()[0] self.y2 = scanner.getYRange()[1] self.z1 = scanner.getZRange()[0] self.z2 = scanner.getZRange()[1] def _reset_cursor_fired(self): self.center_cursor() # saving images def save_image(self, filename=None): self.save_figure(self.figure_container, filename) traits_view = View(VGroup(Hsplit(HGroup(HGroup(Item('submit_button', show_label=False), Item('remove_button', show_label=False), Item('priority'), Item('state', style='readonly'), Item('history_back', show_label=False), Item('history_forward', show_label=False), ), Item('figure_container', show_label=False, resizable=True,enabled_when='state != "run"'), HGroup(Item('thresh_low', width=-80), Item('thresh_high', width=-80), Item('colormap', width=-100), Item('show_labels'), ), ), HGroup(HGroup(Item('submit_gsd_button', show_label=False), Item('remove_button', show_label=False), Item('history_back_gsd', show_label=False), Item('history_forward_gsd', show_label=False), ), Item('figure_container_gsd', show_label=False, resizable=True,enabled_when='state != "run"'), HGroup(Item('thresh_low_gsd', width=-80), Item('thresh_high_gsd', width=-80), Item('colormap_gsd', width=-100), Item('show_labels'), ), ), ), HGroup(Item('resolution', enabled_when='state != "run"', width=-60), Item('x1', width=-60), Item('x2', width=-60), Item('y1', width=-60), Item('y2', width=-60), Item('z1', width=-60), Item('z2', width=-60), Item('reset_range', show_label=False), ), HGroup(Item('constant_axis', style='custom', show_label=False, enabled_when='state != "run"'), Item('bidirectional', enabled_when='state != "run"'), Item('seconds_per_point', width=-80), Item('return_speed', width=-80), Item('reset_cursor', show_label=False), ), Item('x', enabled_when='state != "run" or (state == "run" and constant_axis == "x")'), Item('y', enabled_when='state != "run" or (state == "run" and constant_axis == "y")'), Item('z', enabled_when='state != "run" or (state == "run" and constant_axis == "z")'), ), menubar = MenuBar(Menu(Action(action='save_image', name='Save Image (.png)'), Action(action='save', name='Save (.pyd or .pys)'), Action(action='load', name='Load'), Action(action='_on_close', name='Quit'), name='File'),), title='Confocal', width=880, height=800, buttons=[], resizable=True, x=0, y=0, handler=GetSetSaveImageHandler)
def __init__(self, link, trk_view): self._trk_view = trk_view self.legend_visible = False self.gps_visible = True self.glo_visible = True self.gal_visible = True self.bds_visible = True self.qzss_visible = True self.sbas_visible = True self.x_circle_0, self.y_circle_0 = self.create_circle(0) self.x_circle_30, self.y_circle_30 = self.create_circle(30) self.x_circle_60, self.y_circle_60 = self.create_circle(60) # draw radial lines at 0, 30, 60, 90, 120, 150 degrees self.x_radial_0, self.y_radial_0 = self.create_radial(0) self.x_radial_30, self.y_radial_30 = self.create_radial(30) self.x_radial_60, self.y_radial_60 = self.create_radial(60) self.x_radial_90, self.y_radial_90 = self.create_radial(90) self.x_radial_120, self.y_radial_120 = self.create_radial(120) self.x_radial_150, self.y_radial_150 = self.create_radial(150) self.plot_data = ArrayPlotData( x_gps=[], x_glo=[], x_gal=[], x_bds=[], x_qzss=[], x_sbas=[], y_gps=[], y_glo=[], y_gal=[], y_bds=[], y_qzss=[], y_sbas=[], x_0=self.x_circle_0, y_0=self.y_circle_0, x_30=self.x_circle_30, y_30=self.y_circle_30, x_60=self.x_circle_60, y_60=self.y_circle_60, xr_0=self.x_radial_0, yr_0=self.y_radial_0, xr_30=self.x_radial_30, yr_30=self.y_radial_30, xr_60=self.x_radial_60, yr_60=self.y_radial_60, xr_90=self.x_radial_90, yr_90=self.y_radial_90, xr_120=self.x_radial_120, yr_120=self.y_radial_120, xr_150=self.x_radial_150, yr_150=self.y_radial_150, ) self.plot = Plot(self.plot_data) gps = self.plot.plot(('x_gps', 'y_gps'), type='scatter', name='', color='green', marker='dot', line_width=0.0, marker_size=5.0) glo = self.plot.plot(('x_glo', 'y_glo'), type='scatter', name='', color='red', marker='dot', line_width=0.0, marker_size=5.0) gal = self.plot.plot(('x_gal', 'y_gal'), type='scatter', name='', color='blue', marker='dot', line_width=0.0, marker_size=5.0) bds = self.plot.plot(('x_bds', 'y_bds'), type='scatter', name='', color='yellow', marker='dot', line_width=0.0, marker_size=5.0) qzss = self.plot.plot(('x_qzss', 'y_qzss'), type='scatter', name='', color='pink', marker='dot', line_width=0.0, marker_size=5.0) sbas = self.plot.plot(('x_sbas', 'y_sbas'), type='scatter', name='', color='purple', marker='dot', line_width=0.0, marker_size=5.0) self.plot.plot(('x_0', 'y_0'), type='line', name='', color='black', line_width=1) self.plot.plot(('x_30', 'y_30'), type='line', name='', color='black', line_width=1) self.plot.plot(('x_60', 'y_60'), type='line', name='', color='black', line_width=1) self.plot.plot(('xr_0', 'yr_0'), type='line', name='', color='black', line_width=0.25) self.plot.plot(('xr_30', 'yr_30'), type='line', name='', color='black', line_width=0.25) self.plot.plot(('xr_60', 'yr_60'), type='line', name='', color='black', line_width=0.25) self.plot.plot(('xr_90', 'yr_90'), type='line', name='', color='black', line_width=0.25) self.plot.plot(('xr_120', 'yr_120'), type='line', name='', color='black', line_width=0.25) self.plot.plot(('xr_150', 'yr_150'), type='line', name='', color='black', line_width=0.25) plot_labels = ['GPS', 'GLONASS', 'GALILEO', 'BEIDOU', 'QZSS', 'SBAS'] plots_legend = dict(zip(plot_labels, [gps, glo, gal, bds, qzss, sbas])) self.plot.legend.plots = plots_legend self.plot.legend.labels = plot_labels # sets order self.plot.legend.visible = False self.plot.index_range.low_setting = -110 self.plot.index_range.high_setting = 110 self.plot.value_range.low_setting = -110 self.plot.value_range.high_setting = 110 self.plot.padding = (5, 5, 5, 5) self.plot.aspect_ratio = 1.0 self.plot.x_axis.visible = False self.plot.y_axis.visible = False self.plot.x_grid.visible = False self.plot.y_grid.visible = False self.default_overlays = self.plot.overlays self.axis_overlays = [] north_label = DataLabel(component=self.plot, data_point=(0, 90), label_text="N", label_position="top", border_visible=False, bgcolor="transparent", marker_visible=False, font='modern 20', arrow_visible=False, show_label_coords=False) self.axis_overlays.append(north_label) east_label = DataLabel(component=self.plot, data_point=(90, 0), label_text="E", label_position="right", border_visible=False, bgcolor="transparent", marker_visible=False, font='modern 20', arrow_visible=False, show_label_coords=False) self.axis_overlays.append(east_label) south_label = DataLabel(component=self.plot, data_point=(0, -90), label_text="S", label_position="bottom", border_visible=False, bgcolor="transparent", marker_visible=False, font='modern 20', arrow_visible=False, show_label_coords=False) self.axis_overlays.append(south_label) west_label = DataLabel(component=self.plot, data_point=(-90, 0), label_text="W", label_position="left", border_visible=False, bgcolor="transparent", marker_visible=False, font='modern 20', arrow_visible=False, show_label_coords=False) self.axis_overlays.append(west_label) el_0_label = DataLabel(component=self.plot, data_point=(0, 90), label_text="0" + DEG_SIGN, label_position="bottom right", border_visible=False, bgcolor="transparent", marker_visible=False, font='modern 10', arrow_visible=False, show_label_coords=False) self.axis_overlays.append(el_0_label) el_30_label = DataLabel(component=self.plot, data_point=(0, 60), label_text="30" + DEG_SIGN, label_position="bottom right", border_visible=False, bgcolor="transparent", marker_visible=False, font='modern 10', arrow_visible=False, show_label_coords=False) self.axis_overlays.append(el_30_label) el_60_label = DataLabel(component=self.plot, data_point=(0, 30), label_text="60" + DEG_SIGN, label_position="bottom right", border_visible=False, bgcolor="transparent", marker_visible=False, font='modern 10', arrow_visible=False, show_label_coords=False) self.axis_overlays.append(el_60_label) self.plot.overlays += self.axis_overlays self.link = link self.link.add_callback(self.azel_callback, [SBP_MSG_SV_AZ_EL]) self.python_console_cmds = {'skyplot': self}
def _plot_default(self): container = OverlayPlotContainer(padding=50, fill_padding=True, bgcolor="lightgray", use_backbuffer=True) # Create the initial X-series of data numpoints = self.numpoints low = self.low high = self.high x = linspace(low, high, numpoints + 1) y = jn(0, x) plot = create_line_plot((x, y), color=tuple(COLOR_PALETTE[0]), width=2.0) plot.index.sort_order = "ascending" plot.bgcolor = "white" plot.border_visible = True add_default_grids(plot) add_default_axes(plot) # Add some tools plot.tools.append(PanTool(plot)) zoom = ZoomTool(plot, tool_mode="box", always_on=False) plot.overlays.append(zoom) # Add a dynamic label. This can be dragged and moved around using the # right mouse button. Note the use of padding to offset the label # from its data point. label = DataLabel(component=plot, data_point=(x[40], y[40]), label_position="top left", padding=40, bgcolor="lightgray", border_visible=False) plot.overlays.append(label) tool = DataLabelTool(label, drag_button="right", auto_arrow_root=True) label.tools.append(tool) # Add some static labels. label2 = DataLabel(component=plot, data_point=(x[20], y[20]), label_position="bottom right", border_visible=False, bgcolor="transparent", marker_color="blue", marker_line_color="transparent", marker="diamond", font='modern 14', arrow_visible=False) plot.overlays.append(label2) label3 = DataLabel(component=plot, data_point=(x[80], y[80]), label_position="top", padding_bottom=20, marker_color="transparent", marker_size=8, marker="circle", arrow_visible=False) plot.overlays.append(label3) # This label uses label_style='bubble'. label4 = DataLabel( component=plot, data_point=(x[60], y[60]), border_padding=10, marker_color="red", marker_size=3, label_position=(20, 50), label_style='bubble', label_text="Something interesting", label_format="at x=%(x).2f, y=%(y).2f", font='modern 18', bgcolor=(1, 1, 0.75, 1), ) plot.overlays.append(label4) tool4 = DataLabelTool(label4, drag_button="right", auto_arrow_root=True) label4.tools.append(tool4) # Another 'bubble' label. This one sets arrow_min_length=20, so # the arrow is not drawn when the label is close to the data point. label5 = DataLabel( component=plot, data_point=(x[65], y[65]), border_padding=10, marker_color="green", marker_size=4, show_label_coords=False, label_style='bubble', label_position=(25, 5), label_text="Label with\narrow_min_length=20", border_visible=False, arrow_min_length=20, font='modern 14', bgcolor=(0.75, 0.75, 0.75, 1), ) plot.overlays.append(label5) tool5 = DataLabelTool(label5, drag_button="right", auto_arrow_root=True) label5.tools.append(tool5) container.add(plot) return container
def azel_callback(self, sbp_msg, **metadata): svazelmsg = MsgSvAzEl(sbp_msg) tracked = self._trk_view.get_tracked_sv_labels() pending_update = { 'x_gps': [], 'x_glo': [], 'x_gal': [], 'x_bds': [], 'x_qzss': [], 'x_sbas': [], 'y_gps': [], 'y_glo': [], 'y_gal': [], 'y_bds': [], 'y_qzss': [], 'y_sbas': [] } # store new label updates, and display at once overlay_update = [] for azel in svazelmsg.azel: sid = azel.sid az = azel.az * 2 el = azel.el x, y = self.azel_to_xy(az, el) sat_string = "" if code_is_gps(sid.code) and self.gps_visible: pending_update['x_gps'].append(x) pending_update['y_gps'].append(y) elif code_is_glo(sid.code) and self.glo_visible: pending_update['x_glo'].append(x) pending_update['y_glo'].append(y) elif code_is_galileo(sid.code) and self.gal_visible: pending_update['x_gal'].append(x) pending_update['y_gal'].append(y) elif code_is_bds(sid.code) and self.bds_visible: pending_update['x_bds'].append(x) pending_update['y_bds'].append(y) elif code_is_qzss(sid.code) and self.qzss_visible: pending_update['x_qzss'].append(x) pending_update['y_qzss'].append(y) elif code_is_sbas(sid.code) and self.sbas_visible: pending_update['x_sbas'].append(x) pending_update['y_sbas'].append(y) sat_string = get_label((sid.code, sid.sat))[2] if sat_string in tracked: sat_string += TRK_SIGN label = DataLabel(component=self.plot, data_point=(x, y), label_text=sat_string, label_position="bottom right", border_visible=False, bgcolor="transparent", marker_visible=False, font='modern 14', arrow_visible=False, show_label_coords=False) overlay_update.append(label) # display label updates self.plot.overlays = (self.axis_overlays + overlay_update + self.default_overlays) self.plot_data.update(pending_update)