def add_default_axes(plot, orientation="normal", vtitle="", htitle=""): """ Creates left and bottom axes for a plot. Assumes that the index is horizontal and value is vertical by default; set orientation to something other than "normal" if they are flipped. """ if orientation in ("normal", "h"): v_mapper = plot.value_mapper h_mapper = plot.index_mapper else: v_mapper = plot.index_mapper h_mapper = plot.value_mapper yticks = ScalesTickGenerator() left = PlotAxis( orientation='left', title=vtitle, mapper=v_mapper, component=plot, tick_generator=yticks, ) xticks = ScalesTickGenerator() bottom = PlotAxis( orientation='bottom', title=htitle, mapper=h_mapper, component=plot, tick_generator=xticks, ) plot.underlays.append(left) plot.underlays.append(bottom) return left, bottom
def __init__(self): super(StackedPlot, self).__init__() self.container = OverlayPlotContainer(bgcolor='white', use_backbuffer=True, border_visible=True, padding=50, padding_left=110, fill_padding=True ) self.data = ArrayPlotData() self.chaco_plot = None self.value_mapper = None self.index_mapper = None self.x_axis = PlotAxis(component=self.container, orientation='bottom', title=u'Angle (2\u0398)', title_font=settings.axis_title_font, tick_label_font=settings.tick_font) y_axis_title = 'Normalized intensity (%s)' % get_value_scale_label('linear') self.y_axis = PlotAxis(component=self.container, orientation='left', title=y_axis_title, title_font=settings.axis_title_font, tick_label_font=settings.tick_font) self.container.overlays.extend([self.x_axis, self.y_axis]) self.container.tools.append( TraitsTool(self.container, classes=[LinePlot,PlotAxis])) self.colors = [] self.last_flip_order = self.flip_order
def add(self,series,limit=None): broadcaster = BroadcasterTool() for name,line in series._plot.line.items(): if limit is not None and name not in limit: continue if line.time==[]: print "Graph.add> empty:", name continue plot=create_line_plot((seconds(line.time),line.data),color=line.color) self.plot_area.add(plot) axis = PlotAxis(orientation="left", resizable="v", mapper = plot.y_mapper, bgcolor="white", title = name, title_color = line.color, title_spacing = -4.0, border_visible = True,) ## Visual style axis.bounds = [60,0] axis.padding_left = 1 axis.padding_right = 1 self.container.add(axis) ## Tools (attach to all for now) plot.tools.append(broadcaster) broadcaster.tools.append(PanTool(plot)) broadcaster.tools.append(DragZoom(plot,maintain_aspect_ratio=False,drag_button='right',restrict_domain=True))
def create_plot(self): # Create the mapper, etc self._image_index = GridDataSource(array([]), array([]), sort_order=("ascending", "ascending")) image_index_range = DataRange2D(self._image_index) # self._image_index.on_trait_change(self._metadata_changed, # "metadata_changed") self._image_value = ImageData(data=array([]), value_depth=1) image_value_range = DataRange1D(self._image_value) # Create the colormapped scalar plot self.plot = CMapImagePlot( index=self._image_index, index_mapper=GridMapper(range=image_index_range), value=self._image_value, value_mapper=self._cmap(image_value_range)) # Add a left axis to the plot left = PlotAxis(orientation='left', title="y", mapper=self.plot.index_mapper._ymapper, component=self.plot) self.plot.overlays.append(left) # Add a bottom axis to the plot bottom = PlotAxis(orientation='bottom', title="x", mapper=self.plot.index_mapper._xmapper, component=self.plot) self.plot.overlays.append(bottom) # Add some tools to the plot self.plot.tools.append(PanTool(self.plot, constrain_key="shift")) self.plot.overlays.append( ZoomTool(component=self.plot, tool_mode="box", always_on=False)) # Create a colorbar cbar_index_mapper = LinearMapper(range=image_value_range) self.colorbar = ColorBar(index_mapper=cbar_index_mapper, plot=self.plot, padding_top=self.plot.padding_top, padding_bottom=self.plot.padding_bottom, padding_right=40, resizable='v', width=10) # Create a container and add components self.container = HPlotContainer(padding=40, fill_padding=True, bgcolor="white", use_backbuffer=False) self.container.add(self.colorbar) self.container.add(self.plot)
def _create_plot_component(): # Create some x-y data series to plot plot_area = OverlayPlotContainer(border_visible=True) container = HPlotContainer(padding=50, bgcolor="transparent") #container.spacing = 15 x = linspace(-2.0, 10.0, 100) for i in range(5): color = tuple(COLOR_PALETTE[i]) y = jn(i, x) renderer = create_line_plot((x, y), color=color) plot_area.add(renderer) #plot_area.padding_left = 20 axis = PlotAxis(orientation="left", resizable="v", mapper = renderer.y_mapper, axis_line_color=color, tick_color=color, tick_label_color=color, title_color=color, bgcolor="transparent", title = "jn_%d" % i, border_visible = True,) axis.bounds = [60,0] axis.padding_left = 10 axis.padding_right = 10 container.add(axis) if i == 4: # Use the last plot's X mapper to create an X axis and a # vertical grid x_axis = PlotAxis(orientation="bottom", component=renderer, mapper=renderer.x_mapper) renderer.overlays.append(x_axis) grid = PlotGrid(mapper=renderer.x_mapper, orientation="vertical", line_color="lightgray", line_style="dot") renderer.underlays.append(grid) # Add the plot_area to the horizontal container container.add(plot_area) # Attach some tools to the plot broadcaster = BroadcasterTool() for plot in plot_area.components: broadcaster.tools.append(PanTool(plot)) # Attach the broadcaster to one of the plots. The choice of which # plot doesn't really matter, as long as one of them has a reference # to the tool and will hand events to it. plot.tools.append(broadcaster) return container
def _init_components(self): # Since this is called after the HasTraits constructor, we have to make # sure that we don't blow away any components that the caller may have # already set. if self.range2d is None: self.range2d = DataRange2D() if self.index_mapper is None: if self.index_scale == "linear": imap = LinearMapper(range=self.range2d.x_range) else: imap = LogMapper(range=self.range2d.x_range) self.index_mapper = imap if self.value_mapper is None: if self.value_scale == "linear": vmap = LinearMapper(range=self.range2d.y_range) else: vmap = LogMapper(range=self.range2d.y_range) self.value_mapper = vmap if self.x_ticks is None: self.x_ticks = ScalesTickGenerator( scale=self._make_scale(self.index_scale)) if self.y_ticks is None: self.y_ticks = ScalesTickGenerator( scale=self._make_scale(self.value_scale)) if self.x_grid is None: self.x_grid = PlotGrid(mapper=self.x_mapper, orientation="vertical", line_color="lightgray", line_style="dot", component=self, tick_generator=self.x_ticks) if self.y_grid is None: self.y_grid = PlotGrid(mapper=self.y_mapper, orientation="horizontal", line_color="lightgray", line_style="dot", component=self, tick_generator=self.y_ticks) if self.x_axis is None: self.x_axis = PlotAxis(mapper=self.x_mapper, orientation="bottom", component=self, tick_generator=self.x_ticks) if self.y_axis is None: self.y_axis = PlotAxis(mapper=self.y_mapper, orientation="left", component=self, tick_generator=self.y_ticks)
def _ts_data_changed(self): """ Dataset has changed: update the plot. ENH: add the possibility to pass a dict to ArrayPlotData. """ print "data changed: updating the plot..." arr_data = ArrayPlotData() for k, v in self.ts_data.items(): arr_data.set_data(k, v) self.ts_plot = ToolbarPlot(arr_data) for i, k in enumerate([k for k in self.ts_data.keys() if k != "index"]): self.ts_plot.plot(("index", k), name=k, color=colors[i % len(colors)]) if self.index_is_dates: # Index was an array of datetime: overwrite the x axis self.ts_plot.x_axis = None x_axis = PlotAxis(self.ts_plot, orientation="bottom", tick_generator=ScalesTickGenerator( scale=CalendarScaleSystem())) self.ts_plot.overlays.append(x_axis) self.ts_plot.x_grid.tick_generator = x_axis.tick_generator if self.data_file: self.ts_plot.title = "Time series visualization from %s" % self.data_file else: self.ts_plot.title = "Time series visualization" attach_tools(self.ts_plot)
def make_plot(self): # ============================== #self.pd = ArrayPlotData(P_data=self.data.m_Pressures_array) # we store the array plotdata in the data object and update it there self.p_obj.values_array_pd = ArrayPlotData( P_data=self.p_obj.values_array) self.plot_ren = Plot(self.p_obj.values_array_pd) self.plot_ren.padding_left = 70 #80 self.plot_ren.padding_right = 5 self.plot_ren.padding_top = 5 self.plot_ren.padding_bottom = 40 self.plot_ren.x_axis.title = self.p_obj.x_axis self.plot_ren.y_axis.visible = False self.plot_ren.plot(("P_data"), type="line", color="blue", render_style='connectedhold') tick_gen = ScalesTickGenerator(scale=DefaultScale()) y_axis = PlotAxis(orientation='left', title=self.p_obj.y_axis, mapper=self.plot_ren.value_mapper, component=self.plot_ren, tick_generator=tick_gen) self.plot_ren.underlays.append(y_axis)
def _plot_default(self): container = OverlayPlotContainer(bgcolor="white") plots = self._make_curves() for plot in plots: plot.padding = 60 container.add(plot) bottom_axis = PlotAxis(plot, orientation='bottom') label_list = [ 'var a', 'var b', 'var c', 'var d', 'var e', 'var f', 'var g', 'var h', 'var i' ] vertical_axis = LabelAxis(plot, orientation='left', title='Categories', positions=list(range(1, 10)), labels=label_list) vertical2_axis = LabelAxis(plot, orientation='right', positions=list(range(1, 10)), labels=label_list) plot.underlays.append(vertical_axis) plot.underlays.append(vertical2_axis) plot.underlays.append(bottom_axis) return container
def add_tools_title(self, plot, title_keyword): """Used to add same tools to multiple plots""" plot.title = title_keyword plot.legend.visible = True bottom_axis = PlotAxis( plot, orientation='bottom', title=self.specparms.working_x_unit, # label_color='red', # label_font='Arial', # tick_color='green', tick_weight=1) # CUSTOM YLABEL, why bother? # vertical_axis = PlotAxis(plot, orientation='left', # title='Relative'+str(title_keyword)) # plot.underlays.append(vertical_axis) plot.underlays.append(bottom_axis) # Attach some tools to the plot plot.tools.append(PanTool(plot)) zoom = ZoomTool(component=plot, tool_mode="box", always_on=False) plot.overlays.append(zoom)
def update_main_plot(self): """ Build main plot """ self.ts_plot = ToolbarPlot(self.arr_plot_data) for i, k in enumerate([k for k in self.ts_data.keys() if k != "index"]): renderer = self.ts_plot.plot(("index", k), name = k, color = colors[i % len(colors)])[0] if self.index_is_dates: # Index was an array of datetime: overwrite the x axis self.ts_plot.x_axis = None x_axis = PlotAxis(self.ts_plot, orientation="bottom", tick_generator=ScalesTickGenerator(scale=CalendarScaleSystem())) self.ts_plot.overlays.append(x_axis) self.ts_plot.x_grid.tick_generator = x_axis.tick_generator if self.data_file: self.ts_plot.title = ("Time series visualization from %s" % (os.path.split(self.data_file)[1])) else: self.ts_plot.title = "Time series visualization" attach_tools(self.ts_plot) # Attach the range selection to the last renderer; any one will do self.ts_plot.tools.append(RangeSelection(renderer, left_button_selects = False, auto_handle_event = False)) # Attach the corresponding overlay self._range_selection_overlay = RangeSelectionOverlay(renderer, metadata_name="selections") self.ts_plot.overlays.append(self._range_selection_overlay) # Grab a reference to the Time axis datasource and add a listener to its # selections metadata self.times_ds = renderer.index self.times_ds.on_trait_change(self._selections_changed)
def _dipole_plot_default(self): print('_dipole_plot_default') """Create the Plot instance.""" #pd = ArrayPlotData(index = self.dipole_data_model.x_index) #pd.set_data("y", self.dipole_data_model.data) plot = create_line_plot( (self.dipole_data_model.x_index, self.dipole_data_model.data), color='black') #plot.add(self.dipole_renderer) #plot.plot(("index", "y")) x_axis = PlotAxis(component=plot, mapper=plot.index_mapper, orientation='bottom') # # y_axis = PlotAxis(component=plot, # # mapper=self.signals_renderer.value_mapper, # # orientation='left') plot.overlays.extend([x_axis]) plot.index = self.signals_renderer.index plot.overlays.append( LineInspector(plot, write_metadata=True, is_listener=True)) # plot.overlays.append(LineInspector(plot, axis="value", # is_listener=True)) plot.origin_axis_visible = False plot.padding_top = 0 plot.padding_left = 0 plot.padding_right = 0 plot.padding_bottom = 50 plot.border_visible = False plot.bgcolor = "white" plot.use_downsampling = True return plot
def _signals_plot_default(self): print('_signals_plot_default') """Create the Plot instance.""" plot = Plot() plot.add(self.signals_renderer) x_axis = PlotAxis(component=plot, mapper=self.signals_renderer.index_mapper, orientation='bottom') # y_axis = PlotAxis(component=plot, # mapper=self.signals_renderer.value_mapper, # orientation='left') plot.overlays.extend([x_axis]) plot.origin_axis_visible = False plot.padding_top = 0 plot.padding_left = 0 plot.padding_right = 0 plot.padding_bottom = 50 plot.border_visible = False plot.bgcolor = "white" plot.use_downsampling = True return plot
def _plot_default(self): """Create the Plot instance.""" plot = Plot(title="MultiLinePlot Demo") plot.add(self.multi_line_plot_renderer) x_axis = PlotAxis(component=plot, mapper=self.multi_line_plot_renderer.index_mapper, orientation='bottom', title='t (seconds)') y_axis = PlotAxis(component=plot, mapper=self.multi_line_plot_renderer.value_mapper, orientation='left', title='channel') plot.overlays.extend([x_axis, y_axis]) return plot
def _corr_plot_default(self): diag = self.covar.diagonal() corr = self.covar / np.sqrt(np.outer(diag, diag)) N = len(diag) value_range = DataRange1D(low=-1, high=1) color_mapper = cmap(range=value_range) index = GridDataSource() value = ImageData() mapper = GridMapper(range=DataRange2D(index), y_low_pos=1.0, y_high_pos=0.0) index.set_data(xdata=np.arange(-0.5, N), ydata=np.arange(-0.5, N)) value.set_data(np.flipud(corr)) self.corr_data = value cmap_plot = CMapImagePlot(index=index, index_mapper=mapper, value=value, value_mapper=color_mapper, padding=(40, 40, 100, 40)) yaxis = PlotAxis( cmap_plot, orientation='left', tick_interval=1, tick_label_formatter=lambda x: self.header[int(N - 1 - x)], tick_generator=ShowAllTickGenerator(positions=np.arange(N))) xaxis = PlotAxis( cmap_plot, orientation='top', tick_interval=1, tick_label_formatter=lambda x: self.header[int(x)], tick_label_alignment='edge', tick_generator=ShowAllTickGenerator(positions=np.arange(N))) cmap_plot.overlays.append(yaxis) cmap_plot.overlays.append(xaxis) colorbar = ColorBar( index_mapper=LinearMapper(range=cmap_plot.value_range), plot=cmap_plot, orientation='v', resizable='v', width=10, padding=(40, 5, 100, 40)) container = HPlotContainer(bgcolor='transparent') container.add(cmap_plot) container.add(colorbar) return container
def _plots_default(self): plotdata = self.get_data_sets() plots = Plot(plotdata) plotsDict = {} # plot background sonar image and impound lines xbounds = self.model.survey_rng_m ybounds = self.model.depth_m plots.img_plot("sonarimg", colormap=jet, xbounds=xbounds, ybounds=ybounds) ip = plots.plot(('impound1_X','impound1_Y'), type='line', marker='square') plotsDict['Impoundment line'] = ip plots.x_axis.title = 'Distance along survey line (m)' plots.y_axis.title = 'Depth (m)' # add core samples as scatter with separate y-axis corex = plotdata.get_data('coreX') corey = plotdata.get_data('coreY') scatter = create_scatter_plot((corex,corey), marker='diamond', color='red' ) scatter.index_range = plots.index_range axis = PlotAxis(scatter, orientation='right') axis.title = 'Core sample dist from survey line (m)' scatter.underlays.append(axis) plots.add(scatter) # create vertical line for indicating selected core sample position vals1 = [0 for x in corey] vline = create_line_plot((corey,vals1), color='blue', orientation='v') vline.value_range = scatter.index_range plots.add(vline) # Add Legend legend = Legend(component=plots, padding=10, align="ur") legend.tools.append(LegendTool(legend, drag_button="left")) legend.plots = plotsDict plots.overlays.append(legend) # Add tools scatter.tools.append(PickTool(scatter)) plots.tools.append(TraceTool(plots)) plots.tools.append(PanTool(plots)) plots.tools.append(ZoomTool(plots)) return plots
def _create_window(self): # Create the data and datasource objects # In order for the date axis to work, the index data points need to # be in units of seconds since the epoch. This is because we are using # the CalendarScaleSystem, whose formatters interpret the numerical values # as seconds since the epoch. numpoints = 500 index = create_dates(numpoints) returns = random.lognormal(0.01, 0.1, size=numpoints) price = 100.0 * cumprod(returns) volume = abs(random.normal(1000.0, 1500.0, size=numpoints) + 2000.0) time_ds = ArrayDataSource(index) vol_ds = ArrayDataSource(volume, sort_order="none") price_ds = ArrayDataSource(price, sort_order="none") # Create the price plots price_plot, mini_plot = self._create_price_plots(time_ds, price_ds) price_plot.index_mapper.domain_limits = (index[0], index[-1]) self.price_plot = price_plot self.mini_plot = mini_plot # Create the volume plot vol_plot = self._create_vol_plot(time_ds, vol_ds) vol_plot.index_mapper.domain_limits = (index[0], index[-1]) # Set the plot's bottom axis to use the Scales ticking system ticker = ScalesTickGenerator(scale=CalendarScaleSystem()) for plot in price_plot, mini_plot, vol_plot: bottom_axis = PlotAxis(plot, orientation="bottom", tick_generator=ticker) plot.overlays.append(bottom_axis) plot.overlays.append(PlotAxis(plot, orientation="left")) hgrid, vgrid = add_default_grids(plot) vgrid.tick_generator = bottom_axis.tick_generator container = VPlotContainer(bgcolor="lightgray", spacing=40, padding=50, fill_padding=False) container.add(mini_plot, vol_plot, price_plot) return Window(self, -1, component=container)
def add_renderers(self, plot): if not len(self.plot_style.renderer_styles) == 1: msg = "Only 1 renderer supported in image plots." raise ValueError(msg) renderer_style = self.plot_style.renderer_styles[0] data = self.plot_data.get_data(TWO_D_DATA_NAME) renderer = create_img_plot(data=data, **renderer_style.to_plot_kwargs()) plot.add(renderer) self.renderers[TWO_D_DATA_NAME] = renderer plot.x_axis = PlotAxis(component=renderer, orientation="bottom") plot.y_axis = PlotAxis(component=renderer, orientation="left") plot.underlays.append(plot.x_axis) plot.underlays.append(plot.y_axis) if self.plot_style.contour_style.add_contours: self.generate_contours(plot)
def _create_plot_component(): # Create some x-y data series to plot plot_area = OverlayPlotContainer(border_visible=True) container = HPlotContainer(padding=50, bgcolor="transparent") #container.spacing = 15 x = linspace(-2.0, 10.0, 100) for i in range(5): color = tuple(COLOR_PALETTE[i]) y = jn(i, x) renderer = create_line_plot((x, y), color=color) plot_area.add(renderer) #plot_area.padding_left = 20 axis = PlotAxis( orientation="left", resizable="v", mapper=renderer.y_mapper, axis_line_color=color, tick_color=color, tick_label_color=color, title_color=color, bgcolor="transparent", title="jn_%d" % i, border_visible=True, ) axis.bounds = [60, 0] axis.padding_left = 10 axis.padding_right = 10 container.add(axis) if i == 4: # Use the last plot's X mapper to create an X axis and a # vertical grid x_axis = PlotAxis(orientation="bottom", component=renderer, mapper=renderer.x_mapper) renderer.overlays.append(x_axis) grid = PlotGrid(mapper=renderer.x_mapper, orientation="vertical", line_color="lightgray", line_style="dot") renderer.underlays.append(grid) # Add the plot_area to the horizontal container container.add(plot_area) # Attach some tools to the plot broadcaster = BroadcasterTool() for plot in plot_area.components: broadcaster.tools.append(PanTool(plot)) # Attach the broadcaster to one of the plots. The choice of which # plot doesn't really matter, as long as one of them has a reference # to the tool and will hand events to it. plot.tools.append(broadcaster) return container
def _figure_drift_default(self): plot = Plot(self.plot_data_drift, width=70, height=40, padding=8, padding_left=64, padding_bottom=32) plot.plot(('t','x'), type='line', color='blue', name='x') plot.plot(('t','y'), type='line', color='red', name='y') plot.plot(('t','z'), type='line', color='green', name='z') bottom_axis = PlotAxis(plot, orientation="bottom", tick_generator=ScalesTickGenerator(scale=CalendarScaleSystem())) plot.index_axis=bottom_axis plot.index_axis.title = 'time' plot.value_axis.title = 'drift [um]' plot.legend.visible=True return plot
def configure_plot(plot, xlabel='Time (s)'): """ Set up colors, grids, etc. on plot objects. """ plot.bgcolor = 'white' plot.border_visible = True plot.padding = [40, 15, 15, 20] plot.color = 'darkred' plot.line_width = 1.1 vertical_grid = PlotGrid(component=plot, mapper=plot.index_mapper, orientation='vertical', line_color="gray", line_style='dot', use_draw_order=True) horizontal_grid = PlotGrid(component=plot, mapper=plot.value_mapper, orientation='horizontal', line_color="gray", line_style='dot', use_draw_order=True) vertical_axis = PlotAxis(orientation='left', mapper=plot.value_mapper, use_draw_order=True) horizontal_axis = PlotAxis(orientation='bottom', title=xlabel, mapper=plot.index_mapper, use_draw_order=True) plot.underlays.append(vertical_grid) plot.underlays.append(horizontal_grid) # Have to add axes to overlays because we are backbuffering the main plot, # and only overlays get to render in addition to the backbuffer. plot.overlays.append(vertical_axis) plot.overlays.append(horizontal_axis)
def get_plot_component(self): xbounds = self.model.xh[0] / 1000, self.model.xh[-1] / 1000 ybounds = self.model.yh[0] / 1000, self.model.yh[-1] / 1000 self.plotdata.set_data("imagedata", self.model.Z) #self.plot.x_mapper.stretch_data = False #self.plot.y_mapper.stretch_data = False cmap = TransformColorMapper.from_color_map(jet) renderer = self.plot.img_plot("imagedata", xbounds=xbounds, ybounds=ybounds, colormap=cmap)[0] left = PlotAxis(orientation='left', title='km', mapper=self.plot.value_mapper, component=self.plot) bottom = PlotAxis(orientation='bottom', title='km', mapper=self.plot.index_mapper, component=self.plot) self.plot.underlays.append(left) self.plot.underlays.append(bottom) return self.plot
def __init__(self, link): super(TrackingView, self).__init__() self.link = link self.link.add_callback(MSG_TRACKING_SNRS, self.tracking_snrs_callback) # ======= Line Plot ======= self.plot_data = ArrayPlotData(t=[0.0]) self.plot = Plot(self.plot_data, auto_colors=colours_list) self.plot.value_range.tight_bounds = False self.plot.value_range.low_setting = 0.0 for n in range(TRACK_N_CHANNELS): self.plot_data.set_data('ch'+str(n), [0.0]) self.plot.plot(('t', 'ch'+str(n)), type='line', color='auto') # ======= Bar Plot ======= idxs = ArrayDataSource(range(1, len(self.snrs)+1)) self.vals = ArrayDataSource(self.snrs, sort_order='none') # Create the index range index_range = DataRange1D(idxs, low=0.4, high=TRACK_N_CHANNELS+0.6) index_mapper = LinearMapper(range=index_range) # Create the value range value_range = DataRange1D(low=0.0, high=25.0) value_mapper = LinearMapper(range=value_range) plot = BarPlot(index=idxs, value=self.vals, index_mapper=index_mapper, value_mapper=value_mapper, line_color='blue', fill_color='blue', bar_width=0.8) container = OverlayPlotContainer(bgcolor = "white") plot.padding = 10 plot.padding_left = 30 plot.padding_bottom = 30 container.add(plot) left_axis = PlotAxis(plot, orientation='left') bottom_axis = LabelAxis(plot, orientation='bottom', labels = map(str, range(1, TRACK_N_CHANNELS+1)), positions = range(1, TRACK_N_CHANNELS+1), small_haxis_style=True) plot.underlays.append(left_axis) plot.underlays.append(bottom_axis) self.snr_bars = container self.python_console_cmds = { 'track': self }
def add(self, series, limit=None): broadcaster = BroadcasterTool() for name, line in series._plot.line.items(): if limit is not None and name not in limit: continue if line.time == []: print "Graph.add> empty:", name continue plot = create_line_plot((seconds(line.time), line.data), color=line.color) self.plot_area.add(plot) axis = PlotAxis( orientation="left", resizable="v", mapper=plot.y_mapper, bgcolor="white", title=name, title_color=line.color, title_spacing=-4.0, border_visible=True, ) ## Visual style axis.bounds = [60, 0] axis.padding_left = 1 axis.padding_right = 1 self.container.add(axis) ## Tools (attach to all for now) plot.tools.append(broadcaster) broadcaster.tools.append(PanTool(plot)) broadcaster.tools.append( DragZoom(plot, maintain_aspect_ratio=False, drag_button='right', restrict_domain=True))
def run(self): ## Time axis (first one) plot = self.plot_area.components[0] time = PlotAxis(orientation="bottom", component=plot, mapper=plot.x_mapper) plot.overlays.append(time) ## Plot self.container.add(self.plot_area) self.plot = self.container self.configure_traits()
def _create_returns_plot(self): plot = Plot(self.plotdata) plot.legend.visible = True #FIXME: The legend move tool doesn't seem to quite work right now #plot.legend.tools.append(LegendTool(plot.legend)) plot.x_axis = None x_axis = PlotAxis( plot, orientation="bottom", tick_generator=ScalesTickGenerator(scale=CalendarScaleSystem())) plot.overlays.append(x_axis) plot.x_grid.tick_generator = x_axis.tick_generator for i, name in enumerate(self.plotdata.list_data()): if name == "times": continue renderer = plot.plot(("times", name), type="line", name=name, color=tuple(COLOR_PALETTE[i]))[0] # Tricky: need to set auto_handle_event on the RangeSelection # so that it passes left-clicks to the PanTool # FIXME: The range selection is still getting the initial left down renderer.tools.append( RangeSelection(renderer, left_button_selects=False, auto_handle_event=False)) plot.tools.append( PanTool(plot, drag_button="left", constrain=True, constrain_direction="x", restrict_to_data=True)) plot.overlays.append( ZoomTool(plot, tool_mode="range", max_zoom_out=1.0, x_min_zoom_factor=float(1e-3))) # Attach the range selection to the last renderer; any one will do self._range_selection_overlay = RangeSelectionOverlay( renderer, metadata_name="selections") renderer.overlays.append(self._range_selection_overlay) # Grab a reference to the Time axis datasource and add a listener to its # selections metadata self.times_ds = renderer.index self.times_ds.on_trait_change(self._selections_changed, 'metadata_changed') self.returns_plot = plot
def gen_line_plot(series_one, series_two, y_axis_name=''): """ Parameters ---------- series_one : nd array series_two : nd array """ size = min(series_one.shape[0], series_two.shape[0]) idx = ArrayDataSource(arange(size)) series_one_data = ArrayDataSource(series_one[:size]) series_two_data = ArrayDataSource(series_two[:size]) y_range = DataRange1D(series_one_data) y_range.tight_bounds = False y_range.margin = 50 x_mapper = LinearMapper(range=DataRange1D(idx)) y_mapper = LinearMapper(range=y_range) series_one_plot = LinePlot(index=idx, value=series_one_data, index_mapper=x_mapper, value_mapper=y_mapper, color='blue') series_two_plot = LinePlot(index=idx, value=series_two_data, index_mapper=x_mapper, value_mapper=y_mapper, color='red') container = OverlayPlotContainer(bgcolor='white', padding=25, fill_padding=False, border_visible=True) y_axis = PlotAxis(mapper=y_mapper, component=container, orientation='left') x_axis = PlotAxis(mapper=x_mapper, component=container, orientation='bottom') x_axis.title = 'Time' y_axis.title = y_axis_name legend = Legend(component=container, padding=10, align='ur') legend.plots = { 'Predicted': series_one_plot, 'Actual': series_two_plot, } container.add(series_one_plot) container.add(series_two_plot) container.overlays.append(y_axis) container.overlays.append(legend) return container
def _create_returns_plot(self): plot = Plot(self.plotdata) plot.legend.visible = False plot.x_axis = None x_axis = PlotAxis( plot, orientation="bottom", tick_generator=ScalesTickGenerator(scale=CalendarScaleSystem())) plot.overlays.append(x_axis) plot.x_grid.tick_generator = x_axis.tick_generator for i, name in enumerate(self.plotdata.list_data()): if name == "times": continue renderer = plot.plot(("times", name), type="line", name=name, color="auto", line_width=2)[0] self.times_ds = renderer.index print('chaco: %s') % str(time.time() - tic) # Tricky: need to set auto_handle_event on the RangeSelection # so that it passes left-clicks to the PanTool # FIXME: The range selection is still getting the initial left down renderer.tools.append( RangeSelection(renderer, left_button_selects=False, auto_handle_event=False)) plot.tools.append( PanTool(plot, drag_button="left", constrain=True, constrain_direction="x")) plot.overlays.append( ZoomTool(plot, tool_mode="range", max_zoom_out=1.0)) # Attach the range selection to the last renderer; any one will do self._range_selection_overlay = RangeSelectionOverlay( renderer, metadata_name="selections") renderer.overlays.append(self._range_selection_overlay) # Grab a reference to the Time axis datasource and add a listener to its # selections metadata self.times_ds = renderer.index self.times_ds.on_trait_change(self._selections_changed, 'metadata_changed') self.returns_plot = plot
def __init__(self, index, series_a, series_b, series_c, **kw): super(PlotExample, self).__init__(**kw) plot_data = ArrayPlotData(index=index) starting_values = np.ones(10) * 0.5 starting_vals = ArrayDataSource(starting_values, sort_order="none") series_a = series_a + starting_values plot_data.set_data('series_a', series_a) plot_data.set_data('series_b', series_b) plot_data.set_data('series_c', series_c) self.plot = Plot(plot_data) self.plot.plot(('index', 'series_a'), type='bar', bar_width=0.8, color='auto', starting_value=starting_vals) # set the plot's value range to 0, otherwise it may pad too much self.plot.value_range.low = 0 tick_positions_and_labels = { x: 'label_{}'.format(x) for x in range(1, 11) } tick_generator = ShowAllTickGenerator( positions=tick_positions_and_labels.keys()) def formatter(value): return tick_positions_and_labels[int(value)] plot_axis = PlotAxis( component=self.plot, mapper=self.plot.x_mapper, # labels=labels, orientation='bottom', tick_generator=tick_generator, tick_label_rotate_angle=-45., tick_label_alignment='corner', tick_label_formatter=formatter, tick_label_offset=3) self.plot.underlays.remove(self.plot.index_axis) self.plot.index_axis = plot_axis self.plot.overlays.append(plot_axis)
def add_renderer(self, plot, desc, style, first_renderer=False): """ Create and add to plot renderer described by desc and style. If the axis it is displayed along isn't already created, create it too, and add it to the plot's list of underlays. """ # Modify the renderer's style's name so it is displayed in the style # view: style.renderer_name = desc["name"] renderer = self._build_renderer(desc, style) plot.add(renderer) self.renderers[desc["name"]] = renderer if first_renderer: left_axis, bottom_axis = add_default_axes(renderer) # Emulate chaco.Plot interface: plot.x_axis = bottom_axis plot.y_axis = left_axis renderer.underlays = [] plot.underlays = [bottom_axis, left_axis] else: if style.orientation == STYLE_R_ORIENT and \ plot.second_y_axis is None: is_log = self.plot_style.second_y_axis_style.scaling == \ LOG_AXIS_STYLE if is_log: mapper_klass = LogMapper else: mapper_klass = LinearMapper # The range needs to be initialized to the axis can be aligned # with all secondary y axis renderers: mapper = mapper_klass(range=DataRange1D()) second_y_axis = PlotAxis(component=renderer, orientation="right", mapper=mapper) plot.second_y_axis = second_y_axis plot.underlays.append(second_y_axis) return renderer
def _rebuild_plot(self): container = self.plot value_range = DataRange1D(low=-1, high=1.) index_range = DataRange1D(self.index_ds, high='track', tracking_amount=24 * 3600 * 365) color_mapper = cmap(range=value_range) # Remove old plots container.remove(*container.components) for val, row in zip(self.value_ds, self.rows): horizon = HorizonPlot( index=self.index_ds, value=val, index_mapper=LinearMapper(range=index_range, stretch_data=False), value_mapper=BandedMapper(range=DataRange1D(val)), color_mapper=cmap(range=DataRange1D(val)), #color_mapper, negative_bands=False, ) horizon.tools.append( PanTool(horizon, constrain=True, constrain_direction="x")) horizon.overlays.append( PlotLabel(component=horizon, hjustify='right', text=row, overlay_position='outside left')) container.add(horizon) bottom_axis = PlotAxis( horizon, orientation="bottom", tick_generator=ScalesTickGenerator(scale=CalendarScaleSystem())) container.overlays = [bottom_axis] container.request_redraw()
def update_main_plot(self): """ Build main plot """ self.ts_plot = ToolbarPlot(self.arr_plot_data) for i, k in enumerate([k for k in self.ts_data.keys() if k != "index"]): self.ts_plot.plot(("index", k), name=k, color=colors[i % len(colors)]) if self.index_is_dates: # Index was an array of datetime: overwrite the x axis self.ts_plot.x_axis = None x_axis = PlotAxis(self.ts_plot, orientation="bottom", tick_generator=ScalesTickGenerator( scale=CalendarScaleSystem())) self.ts_plot.overlays.append(x_axis) self.ts_plot.x_grid.tick_generator = x_axis.tick_generator if self.data_file: self.ts_plot.title = "Time series visualization from %s" % self.data_file else: self.ts_plot.title = "Time series visualization" attach_tools(self.ts_plot)
def create_gridded_scatter_plot(x, y, orientation="h", color="red", width=1.0, fill_color="red", marker="square", marker_size=2, value_mapper_class=LinearMapper, padding=30): assert len(x) == len(y) # If you know it is monotonically increasing, sort_order can # be set to 'ascending' index = ArrayDataSource(x, sort_order='none') value = ArrayDataSource(y, sort_order="none") index_range = DataRange1D(tight_bounds=False) index_range.add(index) index_mapper = LinearMapper(range=index_range) value_range = DataRange1D(tight_bounds=False) value_range.add(value) value_mapper = value_mapper_class(range=value_range) plot = ScatterPlot( index=index, value=value, index_mapper=index_mapper, value_mapper=value_mapper, orientation=orientation, color=color, fill_color=fill_color, marker=marker, marker_size=marker_size, padding=[40, 15, 15, 20], # left, right, top, bottom border_visible=True, border_width=1, bgcolor="white", use_backbuffer=True, backbuffer_padding=False, unified_draw=True, draw_layer="plot", overlay_border=True) vertical_grid = PlotGrid(component=plot, mapper=index_mapper, orientation='vertical', line_color="gray", line_style='dot', use_draw_order=True) horizontal_grid = PlotGrid(component=plot, mapper=value_mapper, orientation='horizontal', line_color="gray", line_style='dot', use_draw_order=True) vertical_axis = PlotAxis(orientation='left', mapper=plot.value_mapper, use_draw_order=True) horizontal_axis = PlotAxis(orientation='bottom', title='Time (s)', mapper=plot.index_mapper, use_draw_order=True) plot.underlays.append(vertical_grid) plot.underlays.append(horizontal_grid) # Have to add axes to overlays because we are backbuffering the main plot, # and only overlays get to render in addition to the backbuffer. plot.overlays.append(vertical_axis) plot.overlays.append(horizontal_axis) return plot