def _theta_plot_default(self): theta = self.theta nclasses = theta.shape[0] # create a plot data object and give it this data plot_data = ArrayPlotData() plot_data.set_data('classes', range(nclasses)) # create the plot plot = Plot(plot_data) # --- plot theta samples if self.theta_samples is not None: self._plot_samples(plot, plot_data) # --- plot values of theta plots = self._plot_theta_values(plot, plot_data) # --- adjust plot appearance plot.aspect_ratio = 1.6 if is_display_small() else 1.7 # adjust axis bounds y_high = theta.max() if self.theta_samples is not None: y_high = max(y_high, self.theta_samples.max()) plot.range2d = DataRange2D( low = (-0.2, 0.0), high = (nclasses-1+0.2, y_high*1.1) ) # create new horizontal axis label_axis = self._create_increment_one_axis( plot, 0., nclasses, 'bottom') label_axis.title = 'True classes' self._add_index_axis(plot, label_axis) # label vertical axis plot.value_axis.title = 'Probability' # add legend legend = Legend(component=plot, plots=plots, align="ur", border_padding=10) legend.tools.append(LegendTool(legend, drag_button="left")) legend.padding_right = -100 plot.overlays.append(legend) container = VPlotContainer(width=plot.width + 100, halign='left') plot.padding_bottom = 50 plot.padding_top = 10 plot.padding_left = 0 container.add(plot) container.bgcolor = 0xFFFFFF self.decorate_plot(container, theta) return container
def test_selection_mask(self): plot_data = ArrayPlotData() plot = Plot(plot_data) arr = np.array([-2, -1, 1, 2]) plot_data.set_data("x", arr) plot_data.set_data("y", arr) splot = plot.plot(('x', 'y'), type='scatter')[0] tool = RectangularSelection( component=splot, selection_datasource=splot.index, metadata_name='selections', ) splot.tools.append(tool) # Set the cursor start and stop positions to be such # that the middle two points of the four possible are selected. cursor_start = splot.map_screen([-1.5, -1.5])[0] cursor_stop = splot.map_screen([1.5, 1.5])[0] self.mouse_down(interactor=tool, x=cursor_start[0], y=cursor_start[1]) self.mouse_move(interactor=tool, x=cursor_stop[0], y=cursor_stop[1]) self.mouse_up(interactor=tool, x=cursor_stop[0], y=cursor_stop[1]) expected_mask = [False, True, True, False] selection_mask = list(splot.index.metadata['selections']) self.assertEqual(expected_mask, selection_mask)
def _theta_plot_default(self): theta = self.theta nclasses = theta.shape[0] # create a plot data object and give it this data plot_data = ArrayPlotData() plot_data.set_data('classes', list(range(nclasses))) # create the plot plot = Plot(plot_data) # --- plot theta samples if self.theta_samples is not None: self._plot_samples(plot, plot_data) # --- plot values of theta plots = self._plot_theta_values(plot, plot_data) # --- adjust plot appearance plot.aspect_ratio = 1.6 if is_display_small() else 1.7 # adjust axis bounds y_high = theta.max() if self.theta_samples is not None: y_high = max(y_high, self.theta_samples.max()) plot.range2d = DataRange2D(low=(-0.2, 0.0), high=(nclasses - 1 + 0.2, y_high * 1.1)) # create new horizontal axis label_axis = self._create_increment_one_axis(plot, 0., nclasses, 'bottom') label_axis.title = 'True classes' self._add_index_axis(plot, label_axis) # label vertical axis plot.value_axis.title = 'Probability' # add legend legend = Legend(component=plot, plots=plots, align="ur", border_padding=10) legend.tools.append(LegendTool(legend, drag_button="left")) legend.padding_right = -100 plot.overlays.append(legend) container = VPlotContainer(width=plot.width + 100, halign='left') plot.padding_bottom = 50 plot.padding_top = 10 plot.padding_left = 0 container.add(plot) container.bgcolor = 0xFFFFFF self.decorate_plot(container, theta) return container
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 load(self): data = ArrayPlotData() p = Plot(data=data, padding=100) X = linspace(0, 2 * pi) data.set_data('x0', X) data.set_data('y0', cos(X)) p.plot(('x0', 'y0')) self.container.add(p)
def load(self): data = ArrayPlotData() p = Plot(data=data, padding=100) X = linspace(0, 2 * pi) data.set_data('x0', X) data.set_data('y0', cos(X)) p.plot(('x0', 'y0')) self.container.add(p)
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 cmap_plot(self, z): from chaco.array_plot_data import ArrayPlotData from chaco.plot import Plot from chaco.default_colormaps import color_map_name_dict pd = ArrayPlotData() pd.set_data("cmapdata", z) p = Plot(pd, padding=0) p.img_plot("cmapdata", xbounds=(-25, 25), ybounds=(-25, 25), colormap=color_map_name_dict["hot"]) self.add(p) return pd
def cmap_plot(self, z): from chaco.array_plot_data import ArrayPlotData from chaco.plot import Plot from chaco.default_colormaps import color_map_name_dict pd = ArrayPlotData() pd.set_data('cmapdata', z) p = Plot(pd, padding=0) p.img_plot('cmapdata', xbounds=(-25, 25), ybounds=(-25, 25), colormap=color_map_name_dict['hot']) self.add(p) return pd
def test_selection_no_warning(self): plot_data = ArrayPlotData() arr = np.arange(4) plot_data.set_data("x", arr) plot_data.set_data("y", arr) plot = Plot(plot_data) renderer = plot.plot(('x', 'y'))[0] tool = RangeSelection(renderer) with warnings.catch_warnings(record=True) as w: tool.selection = np.array([2.0, 3.0]) self.assertEqual(w, []) # Accept tuples and lists and None tool.selection = (1.5, 3.5) tool.selection = [1.0, 2.0] tool.selection = None
def test_selecting_mouse_leave_clipping(self): # Regression test for #216. plot_data = ArrayPlotData() arr = np.arange(4.0) plot_data.set_data("x", arr) plot_data.set_data("y", arr) for origin in ('bottom left', 'top left', 'bottom right', 'top right'): for orientation in ('h', 'v'): for axis in ('index', 'value'): plot = Plot(plot_data, orientation=orientation, origin='top right') renderer = plot.plot(('x', 'y'))[0] renderer.bounds = [10, 20] tool = RangeSelection( renderer, left_button_selects=True, axis=axis, ) renderer.tools.append(tool) low_x, low_y = plot.position high_x = low_x + renderer.bounds[0] - 1 high_y = low_y + renderer.bounds[1] - 1 cx = 5 cy = 5 bounds = ( (low_x - 1, low_y), (high_x + 1, low_y), (low_x, low_y - 1), (low_x, high_y + 1), ) for x, y in bounds: self.mouse_down(tool, x=cx, y=cy) self.mouse_leave(tool, x=x, y=y) selection = tool.selection self.assertTrue(selection[0] <= selection[1]) self.mouse_up(tool, x=x, y=y)
def test_selection_no_warning(self): plot_data = ArrayPlotData() arr = np.arange(4) plot_data.set_data("x", arr) plot_data.set_data("y", arr) plot = Plot(plot_data) renderer = plot.plot(('x', 'y'))[0] tool = RangeSelection(renderer) with warnings.catch_warnings(record=True) as w: # Ignore warnings coming from Traits warnings.filterwarnings("ignore", 'elementwise == comparison failed') tool.selection = np.array([2.0, 3.0]) self.assertEqual(w, []) # Accept tuples and lists and None tool.selection = (1.5, 3.5) tool.selection = [1.0, 2.0] tool.selection = None
def test_selecting_mouse_leave_clipping(self): # Regression test for #216. plot_data = ArrayPlotData() arr = np.arange(4.0) plot_data.set_data("x", arr) plot_data.set_data("y", arr) for origin in ('bottom left', 'top left', 'bottom right', 'top right'): for orientation in ('h', 'v'): for axis in ('index', 'value'): plot = Plot( plot_data, orientation=orientation, origin='top right' ) renderer = plot.plot(('x', 'y'))[0] renderer.bounds = [10, 20] tool = RangeSelection( renderer, left_button_selects=True, axis=axis, ) renderer.tools.append(tool) low_x, low_y = plot.position high_x = low_x + renderer.bounds[0] - 1 high_y = low_y + renderer.bounds[1] - 1 cx = 5 cy = 5 bounds = ( (low_x - 1, low_y), (high_x + 1, low_y), (low_x, low_y - 1), (low_x, high_y + 1), ) for x, y in bounds: self.mouse_down(tool, x=cx, y=cy) self.mouse_leave(tool, x=x, y=y) selection = tool.selection self.assertTrue(selection[0] <= selection[1]) self.mouse_up(tool, x=x, y=y)
def _load_image(self, path): self.container = self._container_factory() im = Image.open(path) # oim = array(im) im = im.convert('L') odim = ndim = array(im) pd = ArrayPlotData() pd.set_data('img', odim) plot = Plot(data=pd, padding=[30, 5, 5, 30], default_origin='top left') img_plot = plot.img_plot('img', colormap=color_map_name_dict[self.colormap_name_1] )[0] self.add_inspector(img_plot) self.add_tools(img_plot) self.oplot = plot self.container.add(self.oplot) # self.container.add(self.plot) self.container.request_redraw()
def test_selection_no_warning(self): plot_data = ArrayPlotData() arr = np.arange(4) plot_data.set_data("x", arr) plot_data.set_data("y", arr) plot = Plot(plot_data) renderer = plot.plot(('x', 'y'))[0] tool = RangeSelection(renderer) with warnings.catch_warnings(record=True) as w: # Ignore warnings coming from any package other than Chaco warnings.filterwarnings( "ignore", module="(?!chaco)", ) tool.selection = np.array([2.0, 3.0]) self.assertEqual(w, []) # Accept tuples and lists and None tool.selection = (1.5, 3.5) tool.selection = [1.0, 2.0] tool.selection = None
def test_restrict_to_data_with_empty_source(self): # Regression test for #214. plot_data = ArrayPlotData() plot = Plot(plot_data) arr = np.arange(4.0) plot_data.set_data("x", arr) plot_data.set_data("y", arr) plot_data.set_data("z", np.array([], np.float64)) plot.plot(('x', 'y')) plot.plot(('z', 'z')) tool = PanTool(plot, restrict_to_data=True) plot.tools.append(tool) x_range = plot.x_mapper.range y_range = plot.y_mapper.range x_bounds = (x_range.low, x_range.high) y_bounds = (y_range.low, y_range.high) self.mouse_down(tool, 0.0, 0.0) self.mouse_move(interactor=tool, x=1.0, y=1.0) self.mouse_up(interactor=tool, x=1.0, y=1.0) self.assertEqual((x_range.low, x_range.high), x_bounds) self.assertEqual((y_range.low, y_range.high), y_bounds)
def test_restrict_to_data_with_empty_source(self): # Regression test for #214. plot_data = ArrayPlotData() plot = Plot(plot_data) arr = np.arange(4.0) plot_data.set_data("x", arr) plot_data.set_data("y", arr) plot_data.set_data("z", np.array([], np.float64)) plot.plot(('x', 'y')) plot.plot(('z', 'z')) tool = PanTool(plot, restrict_to_data=True) plot.tools.append(tool) x_range = plot.x_mapper.range y_range = plot.y_mapper.range x_bounds = (x_range.low, x_range.high) y_bounds = (y_range.low, y_range.high) self.mouse_down(tool, 0.0, 0.0) self.mouse_move(interactor=tool, x=1.0, y=1.0) self.mouse_up(interactor=tool, x=1.0, y=1.0) self.assertEqual((x_range.low, x_range.high), x_bounds) self.assertEqual((y_range.low, y_range.high), y_bounds)
def load(self, path): parser = ElementTree(file=open(path, 'r')) circles = parser.find('circles') outline = parser.find('outline') bb = outline.find('bounding_box') bs = bb.find('width'), bb.find('height') w, h = [float(b.text) for b in bs] use_label = parser.find('use_label') if use_label is not None: use_label = to_bool(use_label.text.strip()) else: use_label = True data = ArrayPlotData() p = Plot(data=data, padding=100) p.x_grid.visible = False p.y_grid.visible = False p.x_axis.visible = False p.y_axis.visible = False p.x_axis.title = 'X cm' p.y_axis.title = 'Y cm' # font = 'modern 22' # p.x_axis.title_font = font # p.x_axis.tick_label_font = font # p.y_axis.title_font = font # p.y_axis.tick_label_font = font # p.x_axis_visible = False # p.y_axis_visible = False p.index_range.low_setting = -w / 2 p.index_range.high_setting = w / 2 p.value_range.low_setting = -h / 2 p.value_range.high_setting = h / 2 thetas = linspace(0, 2 * pi) radius = circles.find('radius').text radius = float(radius) face_color = circles.find('face_color') if face_color is not None: face_color = face_color.text else: face_color = 'white' labels = [] for i, pp in enumerate(circles.findall('point')): x, y, l = pp.find('x').text, pp.find('y').text, pp.find('label').text # print i, pp, x, y # load hole specific attrs r = pp.find('radius') if r is None: r = radius else: r = float(r.text) fc = pp.find('face_color') if fc is None: fc = face_color else: fc = fc.text x, y = list(map(float, (x, y))) xs = x + r * sin(thetas) ys = y + r * cos(thetas) xn, yn = 'px{:03d}'.format(i), 'py{:03d}'.format(i) data.set_data(xn, xs) data.set_data(yn, ys) plot = p.plot((xn, yn), face_color=fc, type='polygon')[0] labels.append((x, y, l)) # if use_label: # label = myDataLabel(component=plot, # data_point=(x, y), # label_text=l, # bgcolor='transparent') # plot.overlays.append(label) if use_label: p.overlays.append(LabelsOverlay(component=plot, labels=labels)) self.container.add(p) self.container.invalidate_and_redraw()
def _theta_plot_default(self): theta = self.theta nannotators = theta.shape[0] samples = self.theta_samples # plot data object plot_data = ArrayPlotData() # create the plot plot = Plot(plot_data) # --- plot theta as vertical dashed lines # add vertical lines extremes plot_data.set_data('line_extr', [0., 1.]) for k in range(nannotators): name = self._theta_name(k) plot_data.set_data(name, [theta[k], theta[k]]) plots = {} for k in range(nannotators): name = self._theta_name(k) line_plot = plot.plot( (name, 'line_extr'), line_width = 2., color = get_annotator_color(k), line_style = 'dash', name = name ) plots[name] = line_plot # --- plot samples as distributions if samples is not None: bins = np.linspace(0., 1., 100) max_hist = 0. for k in range(nannotators): name = self._theta_name(k) + '_distr_' hist, x = np.histogram(samples[:,k], bins=bins) hist = hist / float(hist.sum()) max_hist = max(max_hist, hist.max()) # make "bars" out of histogram values y = np.concatenate(([0], np.repeat(hist, 2), [0])) plot_data.set_data(name+'x', np.repeat(x, 2)) plot_data.set_data(name+'y', y) for k in range(nannotators): name = self._theta_name(k) + '_distr_' plot.plot((name+'x', name+'y'), line_width = 2., color = get_annotator_color(k) ) # --- adjust plot appearance plot.aspect_ratio = 1.6 if is_display_small() else 1.7 plot.padding = [20,0,10,40] # adjust axis bounds x_low, x_high = theta.min(), theta.max() y_low, y_high = 0., 1. if samples is not None: x_high = max(x_high, samples.max()) x_low = min(x_low, samples.min()) y_high = max_hist plot.range2d = DataRange2D( low = (max(x_low-0.05, 0.), y_low), high = (min(x_high*1.1, 1.), min(y_high*1.1, 1.)) ) # label axes plot.value_axis.title = 'Probability' plot.index_axis.title = 'Theta' # add legend legend = Legend(component=plot, plots=plots, align="ul", padding=5) legend.tools.append(LegendTool(legend, drag_button="left")) plot.overlays.append(legend) container = VPlotContainer() container.add(plot) container.bgcolor = 0xFFFFFF self.decorate_plot(container, theta) self._set_title(plot) return container
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
def load(self, path): parser = ElementTree(file=open(path, 'r')) circles = parser.find('circles') outline = parser.find('outline') bb = outline.find('bounding_box') bs = bb.find('width'), bb.find('height') w, h = map(lambda b:float(b.text), bs) data = ArrayPlotData() p = Plot(data=data) p.x_grid.visible = False p.y_grid.visible = False p.index_range.low_setting = -w / 2 p.index_range.high_setting = w / 2 p.value_range.low_setting = -h / 2 p.value_range.high_setting = h / 2 thetas = linspace(0, 2 * pi) radius = circles.find('radius').text radius = float(radius) face_color = circles.find('face_color') if face_color is not None: face_color = face_color.text else: face_color = 'white' for i, pp in enumerate(circles.findall('point')): x, y, l = pp.find('x').text, pp.find('y').text, pp.find('label').text # load hole specific attrs r = pp.find('radius') if r is None: r = radius else: r = float(r.text) fc = pp.find('face_color') if fc is None: fc = face_color else: fc = fc.text x, y = map(float, (x, y)) xs = x + r * sin(thetas) ys = y + r * cos(thetas) xn, yn = 'px{:03n}'.format(i), 'py{:03n}'.format(i) data.set_data(xn, xs) data.set_data(yn, ys) plot = p.plot((xn, yn), face_color=fc, type='polygon', )[0] label = myDataLabel(component=plot, data_point=(x, y), label_text=l, bgcolor=fc ) plot.overlays.append(label) self.container.add(p)
def load(self, path): parser = ElementTree(file=open(path, 'r')) circles = parser.find('circles') outline = parser.find('outline') bb = outline.find('bounding_box') bs = bb.find('width'), bb.find('height') w, h = map(lambda b: float(b.text), bs) use_label = parser.find('use_label') if use_label is not None: use_label = to_bool(use_label.text.strip()) else: use_label = True data = ArrayPlotData() p = Plot(data=data, padding=100) p.x_grid.visible = False p.y_grid.visible = False p.x_axis.title = 'X cm' p.y_axis.title = 'Y cm' font = 'modern 22' p.x_axis.title_font = font p.x_axis.tick_label_font = font p.y_axis.title_font = font p.y_axis.tick_label_font = font # p.x_axis_visible = False # p.y_axis_visible = False p.index_range.low_setting = -w / 2 p.index_range.high_setting = w / 2 p.value_range.low_setting = -h / 2 p.value_range.high_setting = h / 2 thetas = linspace(0, 2 * pi) radius = circles.find('radius').text radius = float(radius) face_color = circles.find('face_color') if face_color is not None: face_color = face_color.text else: face_color = 'white' for i, pp in enumerate(circles.findall('point')): x, y, l = pp.find('x').text, pp.find('y').text, pp.find( 'label').text # print i, pp, x, y # load hole specific attrs r = pp.find('radius') if r is None: r = radius else: r = float(r.text) fc = pp.find('face_color') if fc is None: fc = face_color else: fc = fc.text x, y = map(float, (x, y)) xs = x + r * sin(thetas) ys = y + r * cos(thetas) xn, yn = 'px{:03n}'.format(i), 'py{:03n}'.format(i) data.set_data(xn, xs) data.set_data(yn, ys) plot = p.plot( (xn, yn), face_color=fc, type='polygon', )[0] if use_label: label = myDataLabel( component=plot, data_point=(x, y), label_text=l, bgcolor='transparent', ) plot.overlays.append(label) self.container.add(p)
def _theta_plot_default(self): theta = self.theta nannotators = theta.shape[0] samples = self.theta_samples # plot data object plot_data = ArrayPlotData() # create the plot plot = Plot(plot_data) # --- plot theta as vertical dashed lines # add vertical lines extremes plot_data.set_data('line_extr', [0., 1.]) for k in range(nannotators): name = self._theta_name(k) plot_data.set_data(name, [theta[k], theta[k]]) plots = {} for k in range(nannotators): name = self._theta_name(k) line_plot = plot.plot( (name, 'line_extr'), line_width = 2., color = get_annotator_color(k), line_style = 'dash', name = name ) plots[name] = line_plot # --- plot samples as distributions if samples is not None: bins = np.linspace(0., 1., 100) max_hist = 0. for k in range(nannotators): name = self._theta_name(k) + '_distr_' hist, x = np.histogram(samples[:,k], bins=bins) hist = hist / float(hist.sum()) max_hist = max(max_hist, hist.max()) # make "bars" out of histogram values y = np.concatenate(([0], np.repeat(hist, 2), [0])) plot_data.set_data(name+'x', np.repeat(x, 2)) plot_data.set_data(name+'y', y) for k in range(nannotators): name = self._theta_name(k) + '_distr_' plot.plot((name+'x', name+'y'), line_width = 2., color = get_annotator_color(k) ) # --- adjust plot appearance plot.aspect_ratio = 1.6 if is_display_small() else 1.7 plot.padding = [20,0,10,40] # adjust axis bounds x_low, x_high = theta.min(), theta.max() y_low, y_high = 0., 1. if samples is not None: x_high = max(x_high, samples.max()) x_low = min(x_low, samples.min()) y_high = max_hist plot.range2d = DataRange2D( low = (max(x_low-0.05, 0.), y_low), high = (min(x_high*1.1, 1.), min(y_high*1.1, 1.)) ) # label axes plot.value_axis.title = 'Probability' plot.index_axis.title = 'Theta' # add legend legend = Legend(component=plot, plots=plots, align="ul", padding=5) legend.tools.append(LegendTool(legend, drag_button="left")) plot.overlays.append(legend) container = VPlotContainer() container.add(plot) container.bgcolor = 0xFFFFFF self.decorate_plot(container, theta) self._set_title(plot) return container
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