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 _plot_default(self): x = linspace(-5, 10, 500) y = sin(x) y2 = 0.5 * cos(2 * x) view = DataView(border_visible=True) scatter = ScatterPlot( index=ArrayDataSource(x), value=ArrayDataSource(y), marker="square", color="red", outline_color="transparent", index_mapper=LinearMapper(range=view.index_range), value_mapper=LinearMapper(range=view.value_range)) line = LinePlot(index=scatter.index, value=ArrayDataSource(y2), color="blue", index_mapper=LinearMapper(range=view.index_range), value_mapper=LinearMapper(range=view.value_range)) # Add the plot's index and value datasources to the dataview's # ranges so that it can auto-scale and fit appropriately view.index_range.sources.append(scatter.index) view.value_range.sources.append(scatter.value) view.value_range.sources.append(line.value) # Add the renderers to the dataview. The z-order is determined # by the order in which renderers are added. view.add(scatter) view.add(line) view.tools.append(PanTool(view)) view.overlays.append(ZoomTool(view)) return view
def line_plot(self, x, y, new_plot=True): if self.plot is None or new_plot: if isinstance(x, (float, int)): x = [x] if isinstance(y, (float, int)): y = [y] self.plot = LinePlot( index=ArrayDataSource(x), value=ArrayDataSource(y), index_mapper=LinearMapper(range=self.index_range), value_mapper=LinearMapper(range=self.value_range) ) self.add(self.plot) else: datax = self.plot.index.get_data() datay = self.plot.value.get_data() nx = hstack((datax, [x])) ny = hstack((datay, [y])) self.plot.index.set_data(nx) self.plot.value.set_data(ny)
def _create_price_plots(self, times, prices, mini_height=75): """ Creates the two plots of prices and returns them. One of the plots can be zoomed and panned, and the other plot (smaller) always shows the full data. *dates* and *prices* are two data sources. """ # Create the price plot price_plot = FilledLinePlot( index=times, value=prices, index_mapper=LinearMapper(range=DataRange1D(times)), value_mapper=LinearMapper(range=DataRange1D(prices)), edge_color="blue", face_color="paleturquoise", bgcolor="white", border_visible=True) # Add pan and zoom price_plot.tools.append( PanTool( price_plot, constrain=True, constrain_direction="x")) price_plot.overlays.append( ZoomTool( price_plot, drag_button="right", always_on=True, tool_mode="range", axis="index", max_zoom_out_factor=1.0, )) # Create the miniplot miniplot = LinePlot( index=times, value=prices, index_mapper=LinearMapper(range=DataRange1D(times)), value_mapper=LinearMapper(range=DataRange1D(prices)), color="black", border_visible=True, bgcolor="white", height=mini_height, resizable="h") # Add a range overlay to the miniplot that is hooked up to the range # of the main price_plot range_tool = RangeSelection(miniplot) miniplot.tools.append(range_tool) range_overlay = RangeSelectionOverlay( miniplot, metadata_name="selections") miniplot.overlays.append(range_overlay) range_tool.on_trait_change(self._range_selection_handler, "selection") # Attach a handler that sets the tool when the plot's index range changes self.range_tool = range_tool price_plot.index_range.on_trait_change(self._plot_range_handler, "updated") return price_plot, miniplot
def _pulse_plot_default(self): plot = Plot(self.pulse_plot_data, padding=8, padding_left=64, padding_bottom=36) plot.plot(('x', 'y'), style='line', color='blue', name='data') edge_marker = LinePlot(index=ArrayDataSource(np.array((0, 0))), value=ArrayDataSource(np.array((0, 1e9))), color='red', index_mapper=LinearMapper(range=plot.index_range), value_mapper=LinearMapper(range=plot.value_range), name='marker') plot.add(edge_marker) plot.index_axis.title = 'time [ns]' plot.value_axis.title = 'intensity' return plot
def _refresh_container(self): ''' rebuild the container for the current data ''' broadcaster = BroadcasterTool() mfn_line = self.value # print self.panel.GetSize() adapter = self.adapter if adapter.var_x != '': # Get the x-label text from the object's trait var_x label_x = getattr(self.object, adapter.var_x) else: # Get the x-label from the adapter label_x = adapter.label_x if adapter.var_y != '': label_y = getattr(self.object, adapter.var_y) else: label_y = adapter.label_y index = ArrayDataSource(mfn_line.xdata) y = ArrayDataSource(mfn_line.ydata, sort_order="none") index_range = DataRange1D() index_range.add(index) index_mapper = LinearMapper(range=index_range) value_range = DataRange1D(low_setting=0.0) value_range.add(y) value_mapper = LinearMapper(range=value_range) styles_m = list(adapter.line_style.values()) line_style = styles_m[0] line_color = adapter.line_color[0] line_plot = self.lplot = LinePlot(index=index, value=y, index_mapper=index_mapper, value_mapper=value_mapper, color=line_color, width=25, edge_color='blue', linestyle=line_style, border_visible=False) add_default_grids(line_plot) add_default_axes(line_plot, vtitle="Y", htitle="X") self.plot_container.add(line_plot) line_plot.tools.append(PanTool(line_plot)) line_plot.overlays.append(ZoomTool(line_plot))
def _create_pulse_plot(self): pulse_data = ArrayPlotData(x=np.array((0., 100)), y=np.array((0, 1))) plot = Plot(pulse_data, padding=8, padding_left=64, padding_bottom=36) line = plot.plot(('x', 'y'), style='line', color='blue', name='data')[0] plot.index_axis.title = 'time bins' plot.value_axis.title = 'intensity' edge_marker = LinePlot( index=ArrayDataSource(np.array((0, 0))), value=ArrayDataSource(np.array((0, 1e9))), color='red', index_mapper=LinearMapper(range=plot.index_range), value_mapper=LinearMapper(range=plot.value_range), name='marker') plot.add(edge_marker) plot.tools.append(SaveTool(plot)) self.pulse_data = pulse_data self.pulse_plot = plot
def _add_avg_line(self): #Create averageplot # idx = self.plot_data.mat['index'] idx = range(len(self.index_labels)) span = idx[-1] - idx[0] index = ArrayDataSource([idx[0] - span, idx[-1] + span]) avg = self.average value = ArrayDataSource([avg, avg]) # value = self.mk_ads('average') plot_average = LinePlot( index=index, index_mapper=self.index_mapper, value=value, value_mapper=self.value_mapper, color='green', name='average', ) self.add(plot_average)
def _add_lines(self): # index = self.mk_ads('index') index = ArrayDataSource(range(len(self.index_labels))) value = self.mk_ads('values') # Create lineplot plot_line = LinePlot( index=index, index_mapper=self.index_mapper, value=value, value_mapper=self.value_mapper, name='line') # Add datapoint enhancement plot_scatter = ScatterPlot( index=index, index_mapper=self.index_mapper, value=value, value_mapper=self.value_mapper, color="blue", marker_size=5, name='scatter', ) self.add(plot_line, plot_scatter)
def _add_lines(self, flip=False): # When non fliped is index objects if flip: self.index_labels = self.plot_data.var_n pl_itr = self.plot_data.mat.iterrows() else: self.index_labels = self.plot_data.obj_n pl_itr = self.plot_data.mat.iteritems() idx = ArrayDataSource(range(len(self.index_labels))) self.index_range.add(idx) line_styles = ['solid', 'dot dash', 'dash', 'dot', 'long dash'] i = 0 for name, vec in pl_itr: i += 1 vals = ArrayDataSource(vec.values) self.value_range.add(vals) plot = LinePlot(index=idx, index_mapper=self.index_mapper, value=vals, value_mapper=self.value_mapper, color=COLOR_PALETTE[i % 4], line_style=line_styles[i % 4]) self.add(plot) self.plots[name] = plot
def _plot_default(self): container = DataView() xds = FunctionDataSource(func=self.xfunc) yds = FunctionDataSource(func=self.yfunc) xmapper = container.x_mapper ymapper = container.y_mapper xds.data_range = xmapper.range yds.data_range = xmapper.range xmapper.range.set_bounds(-5, 10) ymapper.range.set_bounds(-1, 1.2) plot = ScatterPlot(index=xds, value=yds, index_mapper=xmapper, value_mapper=ymapper, color="green", marker="circle", marker_size=3, line_width=0) plot2 = LinePlot(index=xds, value=yds, index_mapper=xmapper, value_mapper=ymapper, color="lightgray") container.add(plot2, plot) plot.tools.append( PanTool(plot, constrain_direction="x", constrain=True)) plot.tools.append(ZoomTool(plot, axis="index", tool_mode="range")) return container
def create_gridded_line_plot(x, y, orientation="h", color="red", width=1.0, dash="solid", 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 = LinePlot( index=index, value=value, index_mapper=index_mapper, value_mapper=value_mapper, orientation=orientation, color=color, line_width=width, line_style=dash, 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
def _get_plot_overlay(self): if self.data is None or len(self.data.shape) == 1: return container = OverlayPlotContainer(resizable="v", fill_padding=True, padding=30, bgcolor="transparent", use_backbuffer=True) numpoints = self.data.shape[1] if self.scale_type == 'Time': index = self._create_dates(numpoints, start=self.first_day) else: index = range(numpoints) time_ds = ArrayDataSource(index) xmapper = LinearMapper(range=DataRange1D(time_ds)) corr_mapper = None for (m, cDx) in enumerate(self.data): corr_ds = ArrayDataSource(cDx, sort_order="none") if corr_mapper is None: corr_mapper = LinearMapper(range=DataRange1D(corr_ds)) corr_plot = LinePlot( index=time_ds, value=corr_ds, index_mapper=xmapper, value_mapper=corr_mapper, color=tuple(COLOR_PALETTE[m % len(COLOR_PALETTE)]), edge_color="blue", face_color="paleturquoise", #bgcolor="white", border_visible=True, padding_left=25) corr_mapper.range.add(corr_plot.value) if m == 0: ###### Y axis ##################################################### left = PlotAxis( orientation='left', title=self.y_lbl, title_font="modern 12", #title_spacing=0, tick_label_font="modern 8", tick_visible=True, small_axis_style=True, axis_line_visible=False, ensure_labels_bounded=True, #tick_label_color="transparent", mapper=corr_mapper, component=corr_plot) corr_plot.overlays.append(left) ###### X axis ##################################################### if self.scale_type == 'Time': # Just the last axis shows tick_labels bottom_axis = PlotAxis(corr_plot, orientation="bottom", title=self.x_lbl, tick_generator=ScalesTickGenerator( scale=CalendarScaleSystem())) else: bottom_axis = PlotAxis(orientation='bottom', title=self.x_lbl, title_font="modern 12", tick_visible=True, small_axis_style=True, axis_line_visible=False, component=corr_plot) corr_plot.overlays.append(bottom_axis) ###### Grids ##################################################### _, vgrid = add_default_grids(corr_plot) vgrid.tick_generator = bottom_axis.tick_generator ###### Tools ##################################################### corr_plot.tools.append( PanTool(corr_plot, constrain=True, constrain_direction="x")) corr_plot.overlays.append( ZoomTool( corr_plot, drag_button="right", always_on=True, tool_mode="box", #axis="index", max_zoom_out_factor=10.0, )) container.add(corr_plot) ###### Title ##################################################### container.overlays.append( PlotLabel(self.p_title, component=container, overlay_position="outside top", font="modern 16")) container.padding_bottom = 50 return container
def _get_plot_vertical(self): if self.data is None or len(self.data.shape) == 1: return container = VPlotContainer(resizable="v", fill_padding=True, padding=30, stack_order="top_to_bottom", bgcolor="transparent", spacing=9) numpoints = self.data.shape[1] if self.scale_type == 'Time': index = self._create_dates(numpoints, start=self.first_day) else: index = range(numpoints) time_ds = ArrayDataSource(index) xmapper = LinearMapper(range=DataRange1D(time_ds)) corr_mapper = None for (m, cDx) in enumerate(self.data): corr_ds = ArrayDataSource(cDx, sort_order="none") corr_mapper = LinearMapper(range=DataRange1D(corr_ds)) if corr_mapper.range.low < self.y_low: self.y_low = corr_mapper.range.low if corr_mapper.range.high > self.y_high: self.y_high = corr_mapper.range.high corr_plot = LinePlot(index=time_ds, value=corr_ds, index_mapper=xmapper, value_mapper=corr_mapper, edge_color="blue", face_color="paleturquoise", bgcolor="white", border_visible=True, padding_left=25) ###### Y axis ##################################################### if self.y_lbl_type == 'Corr': vtitle = ("%d" % (m + 1)) + u"\u00B0" + " t_win" elif self.y_lbl_type == 'Single': vtitle = "" # One label for all the axis elif self.y_lbl_type == 'Custom' and \ len(self.y_labels) == self.data.shape[0] and \ self.y_labels[m] is not None: # a new value in the list defaults to None so raise an error before # the operator ends inputing it. vtitle = self.y_labels[m] else: vtitle = "" left = PlotAxis( orientation='left', title=vtitle, title_font="modern 12", #title_spacing=0, tick_label_font="modern 8", tick_visible=True, small_axis_style=True, axis_line_visible=False, ensure_labels_bounded=True, #tick_label_color="transparent", mapper=corr_mapper, component=corr_plot) corr_plot.overlays.append(left) ###### X axis ##################################################### if m != (self.data.shape[0] - 1): if self.scale_type == 'Time': # Set the plot's bottom axis to use the Scales ticking system bottom_axis = PlotAxis( corr_plot, orientation="bottom", tick_label_color="transparent", # mapper=xmapper, tick_generator=ScalesTickGenerator( scale=CalendarScaleSystem())) else: bottom_axis = PlotAxis(orientation='bottom', title="", tick_label_color="transparent", tick_visible=True, small_axis_style=True, axis_line_visible=False, component=corr_plot) else: if self.scale_type == 'Time': # Just the last axis shows tick_labels bottom_axis = PlotAxis(corr_plot, orientation="bottom", title=self.x_lbl, tick_generator=ScalesTickGenerator( scale=CalendarScaleSystem())) else: bottom_axis = PlotAxis(orientation='bottom', title=self.x_lbl, title_font="modern 12", tick_visible=True, small_axis_style=True, axis_line_visible=False, component=corr_plot) corr_plot.overlays.append(bottom_axis) _, vgrid = add_default_grids(corr_plot) vgrid.tick_generator = bottom_axis.tick_generator corr_plot.tools.append( PanTool(corr_plot, constrain=True, constrain_direction="x")) corr_plot.overlays.append( ZoomTool( corr_plot, drag_button="right", always_on=True, tool_mode="box", #axis="index", max_zoom_out_factor=10.0, )) container.add(corr_plot) for component in container.components: component.y_mapper.range.set_bounds(self.y_low, self.y_high) container.overlays.append( PlotLabel(self.p_title, component=container, overlay_position="outside top", font="modern 16")) if self.y_lbl_type == 'Single': container.overlays.append( PlotLabel(self.y_lbl, component=container, angle=90.0, overlay_position="outside left", font="modern 12")) container.padding_bottom = 50 return container
# Plot Generation #-------------------------------------------------------------------------- sin_index = ArrayDataSource([]) sin_value = ArrayDataSource([]) mod_index = ArrayDataSource([]) mod_value = ArrayDataSource([]) x_mapper = LinearMapper(range=DataRange1D(sin_index)) y_mapper = LinearMapper(range=DataRange1D(sin_value)) y_mapper.range.low_setting = -2.2 y_mapper.range.high_setting = 2.2 sin_line_plot = LinePlot( index=sin_index, value=sin_value, index_mapper=x_mapper, value_mapper=y_mapper, color='darkblue', ) mod_line_plot = LinePlot( index=mod_index, value=mod_value, index_mapper=x_mapper, value_mapper=y_mapper, color='darkred', ) container = OverlayPlotContainer( bgcolor='white', padding=50, fill_padding=False, border_visible=True, ) container.add(sin_line_plot) container.add(mod_line_plot) left_axis = PlotAxis( mapper=y_mapper, component=container, orientation='left',