def get_errorbar_plot(): x = np.linspace(1., 5., 10) y = 3.2 * x**2 + 4.0 y_with_noise = (y[None, :] + scipy.stats.norm(loc=0, scale=2.8).rvs( (10, 1))) means = y_with_noise.mean(0) stds = y_with_noise.std(0) x, y = get_data_sources(x=x, y=means) low = ArrayDataSource(means - stds) high = ArrayDataSource(means + stds) x_range = DataRange1D(low=0, high=6) y_range = DataRange1D(tight_bounds=False) y_range.add(y, low, high) errorbar_plot = ErrorBarPlot(index=x, value=y, index_mapper=LinearMapper(range=x_range), value_mapper=LinearMapper(range=y_range), value_low=low, value_high=high, endcap_size=11., **PLOT_DEFAULTS) add_axes(errorbar_plot, x_label='Test values', y_label='Measured') return errorbar_plot
def get_candle_plot(): means = np.array([0.2, 0.8, 0.5]) stds = np.array([1.0, 0.3, 0.5]) data = scipy.stats.norm(loc=means, scale=stds).rvs((100, 3)) x = ArrayDataSource(np.arange(3)) center = ArrayDataSource(data.mean(0)) bar_min = ArrayDataSource(data.mean(0) - data.std(0)) bar_max = ArrayDataSource(data.mean(0) + data.std(0)) stem_min = ArrayDataSource(data.min(0)) stem_max = ArrayDataSource(data.max(0)) x_range = DataRange1D(low=-1, high=3) y_range = DataRange1D(tight_bounds=False) y_range.add(center, bar_min, bar_max, stem_min, stem_max) candle_plot = CandlePlot(index=x, index_mapper=LinearMapper(range=x_range), value_mapper=LinearMapper(range=y_range), center_values=center, bar_min=bar_min, bar_max=bar_max, min_values=stem_min, max_values=stem_max, center_color='yellow', bar_color='orange', **PLOT_DEFAULTS) add_axes(candle_plot, x_label='Items', y_label='Values') return candle_plot
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 __init__(self, xs, ys, index_bounds=None, value_bounds=None, *args, **kw): index = ArrayDataSource(xs) value = ArrayDataSource(ys) if index_bounds is not None: index_range = DataRange1D(low=index_bounds[0], high=index_bounds[1]) else: index_range = DataRange1D() index_range.add(index) index_mapper = LinearMapper(range=index_range) index_range.tight_bounds = False if value_bounds is not None: value_range = DataRange1D(low=value_bounds[0], high=value_bounds[1]) else: value_range = DataRange1D() value_range.add(value) value_mapper = LinearMapper(range=value_range) value_range.tight_bounds = False self.index = index self.value = value self.index_mapper = index_mapper self.value_mapper = value_mapper # self.color = "red" # self.line_width = 1.0 # self.line_style = "solid" super(BaseInset, self).__init__(*args, **kw) tick_label_font = 'Helvetica 8' left = PlotAxis(orientation='left', mapper=value_mapper, bgcolor=self.bgcolor, tick_label_font=tick_label_font) bottom = PlotAxis(orientation='bottom', mapper=index_mapper, bgcolor=self.bgcolor, tick_label_font=tick_label_font) self.underlays.append(left) self.underlays.append(bottom)
def __init__(self): # Create some data x = np.random.random(N_POINTS) y = np.random.random(N_POINTS) color = np.exp(-(x**2 + y**2)) # Create a plot data object and give it this data data = ArrayPlotData(index=x, value=y, color=color) # Create the plot plot = Plot(data) plot.plot(("index", "value", "color"), type="cmap_scatter", color_mapper=jet) # Create the colorbar, handing in the appropriate range and colormap colormap = plot.color_mapper colorbar = ColorBar(index_mapper=LinearMapper(range=colormap.range), color_mapper=colormap, orientation='v', resizable='v', width=30, padding=20) colorbar.padding_top = plot.padding_top colorbar.padding_bottom = plot.padding_bottom # Create a container to position the plot and the colorbar side-by-side container = HPlotContainer(plot, colorbar) self.plot = container
def get_multiline_plot(): prng = np.random.RandomState(seed=1234) x = np.linspace(0, 10, 50) y_data = np.column_stack([ x**2, 50 * np.sin(x), 50 * np.cos(x), 0.5 * x**2 + 2 * prng.randn(50), 0.7 * x**2 + prng.randn(50), ]) # data sources for the two axes xs = ArrayDataSource(np.arange(50)) ys = ArrayDataSource(np.arange(y_data.shape[1])) y_range = DataRange1D(low=-0.5, high=y_data.shape[1] - 0.5) y_mapper = LinearMapper(range=y_range) # data source for the multiple lines lines_source = MultiArrayDataSource(data=y_data.T) colors = ['blue', 'green', 'yellow', 'orange', 'red'] def color_generator(color_idx): return color_table[colors[color_idx]] multiline_plot = MultiLinePlot( index=xs, yindex=ys, index_mapper=LinearMapper(range=DataRange1D(xs)), value_mapper=y_mapper, value=lines_source, normalized_amplitude=1.0, use_global_bounds=False, color_func=color_generator, **PLOT_DEFAULTS) add_axes(multiline_plot, x_label='Days', y_label='Stock price changes') y_grid = PlotGrid(mapper=y_mapper, orientation="horizontal", line_style="dot", component=multiline_plot) multiline_plot.overlays.append(y_grid) return multiline_plot
def _matrix_plot_container_default(self): matrix = np.nan_to_num(self.matrix) width = matrix.shape[0] # create a plot data object and give it this data plot_data = ArrayPlotData() plot_data.set_data("values", matrix) # create the plot plot = Plot(plot_data, origin=self.origin) img_plot = plot.img_plot("values", interpolation='nearest', xbounds=(0, width), ybounds=(0, width), colormap=self._create_colormap())[0] #### fix axes self._remove_grid_and_axes(plot) axis = self._create_increment_one_axis(plot, 0.5, width, 'bottom') self._add_value_axis(plot, axis) axis = self._create_increment_one_axis( plot, 0.5, width, 'left', ticks=[str(i) for i in range(width - 1, -1, -1)]) self._add_index_axis(plot, axis) #### tweak plot attributes self._set_title(plot) plot.aspect_ratio = 1. # padding [left, right, up, down] plot.padding = [0, 0, 25, 25] # create the colorbar, handing in the appropriate range and colormap colormap = img_plot.color_mapper colorbar = ColorBar(index_mapper=LinearMapper(range=colormap.range), color_mapper=colormap, plot=img_plot, orientation='v', resizable='v', width=20, padding=[0, 20, 0, 0]) colorbar.padding_top = plot.padding_top colorbar.padding_bottom = plot.padding_bottom # create a container to position the plot and the colorbar side-by-side container = HPlotContainer(use_backbuffer=True) container.add(plot) container.add(colorbar) container.bgcolor = 0xFFFFFF self.decorate_plot(container, self.matrix) return container
def get_multiline_plot(): prices = datasets.fetch_mldata('regression-datasets stock') T, N_LINES = 70, 5 prices_data = prices['data'][:T, :N_LINES] prices_data -= prices_data[0, :] # data sources for the two axes xs = ArrayDataSource(np.arange(T)) ys = ArrayDataSource(np.arange(N_LINES)) y_range = DataRange1D(low=-0.5, high=N_LINES - 0.5) y_mapper = LinearMapper(range=y_range) # data source for the multiple lines lines_source = MultiArrayDataSource(data=prices_data.T) colors = ['blue', 'green', 'yellow', 'orange', 'red'] def color_generator(color_idx): return color_table[colors[color_idx]] multiline_plot = MultiLinePlot( index=xs, yindex=ys, index_mapper=LinearMapper(range=DataRange1D(xs)), value_mapper=y_mapper, value=lines_source, normalized_amplitude=1.0, use_global_bounds=False, color_func=color_generator, **PLOT_DEFAULTS) add_axes(multiline_plot, x_label='Days', y_label='Stock price changes') y_grid = PlotGrid(mapper=y_mapper, orientation="horizontal", line_style="dot", component=multiline_plot) multiline_plot.overlays.append(y_grid) return multiline_plot
def _1D_mapper(source): data_range = DataRange1D() data_range.add(source) return LinearMapper(range=data_range)
def _plot_container_default(self): data = self.posterior nannotations, nclasses = data.shape # create a plot data object plot_data = ArrayPlotData() plot_data.set_data("values", data) # create the plot plot = Plot(plot_data, origin=self.origin) img_plot = plot.img_plot("values", interpolation='nearest', xbounds=(0, nclasses), ybounds=(0, nannotations), colormap=self._create_colormap())[0] ndisp = 55 img_plot.y_mapper.range.high = ndisp img_plot.y_mapper.domain_limits = ((0, nannotations)) self._set_title(plot) plot.padding_top = 80 # create x axis for labels label_axis = self._create_increment_one_axis(plot, 0.5, nclasses, 'top') label_axis.title = 'classes' self._add_index_axis(plot, label_axis) plot.y_axis.title = 'items' # tweak plot aspect goal_aspect_ratio = 2.0 plot_width = (goal_aspect_ratio * self.plot_height * nclasses / ndisp) self.plot_width = min(max(plot_width, 200), 400) plot.aspect_ratio = self.plot_width / self.plot_height # add colorbar colormap = img_plot.color_mapper colorbar = ColorBar(index_mapper=LinearMapper(range=colormap.range), color_mapper=colormap, plot=img_plot, orientation='v', resizable='', width=15, height=250) colorbar.padding_top = plot.padding_top colorbar.padding_bottom = int(self.plot_height - colorbar.height - plot.padding_top) colorbar.padding_left = 0 colorbar.padding_right = 30 # create a container to position the plot and the colorbar side-by-side container = HPlotContainer(use_backbuffer=True) container.add(plot) container.add(colorbar) container.bgcolor = 0xFFFFFF # light gray: 0xEEEEEE # add pan tools img_plot.tools.append( PanTool(img_plot, constrain=True, constrain_direction="y", speed=7.)) self.decorate_plot(container, self.posterior) self.plot_posterior = plot return container