示例#1
0
 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
示例#2
0
 def _plot_default(self):
     self._set_arr(self.index_name, 'index')
     self._set_arr(self.value_name, 'value')
     plot = Plot(self.pd)
     plot.tools.append(TraitsTool(component=plot))
     plot.tools.append(ZoomTool(component=plot))
     plot.tools.append(PanTool(component=plot))
     plot.x_axis.title = self.index_name
     plot.y_axis.title = self.value_name
     plot.plot(('index', 'value'))
     return plot
示例#3
0
 def _plot_default (self):
     x = linspace(-14, 14, 100)
     y = sin(x) * x**3
     plotdata = ArrayPlotData(x = x, y = y)
     plot = Plot(plotdata)
     plot.tools.append(ZoomTool(plot))
     plot.tools.append(PanTool(plot))
     plot.tools.append(TraitsTool(plot))
     
     
     plot.plot(("x", "y"), type="line", color="blue")
     plot.title = "sin(x) * x^3"
     return plot
示例#4
0
 def _plot_default(self):
     self.pd.set_data('index', self.solver.t)
     # Set the first array as value array for plot.
     self.pd.set_data('value', self.solver.solution[:, 0])
     plot = Plot(self.pd)
     plot.plot(('index', 'value'))
     plot.tools.append(TraitsTool(component=plot))
     plot.tools.append(ZoomTool(component=plot))
     plot.tools.append(PanTool(component=plot))
     plot.x_axis.title = 'time'
     plot.y_axis.title = 'x'
     plot.title = self.ode.name
     return plot
示例#5
0
    def _setup_plot(self):
 
        self.plot_data = ArrayPlotData()            
        self.plot = MyPlotClass(self.plot_data,
            padding_left=120, fill_padding=True,
            bgcolor="white", use_backbuffer=True)
        

        self._setup_plot_tools(self.plot)

        # Recreate the legend so it sits on top of the other tools.
        self.plot.legend = Legend(component=self.plot,
                                  padding=10,
                                  error_icon='blank',
                                  visible=False,
                                  scrollable=True,
                                  plots=self.plots,clip_to_component=True)
        
        self.plot.legend.tools.append(LegendTool(self.plot.legend, drag_button="right"))



        self.plot.x_axis = MyPlotAxis(component=self.plot,
                                      orientation='bottom')
        self.plot.y_axis = MyPlotAxis(component=self.plot,
                                      orientation='left')
        self.plot.x_axis.title = ur'Angle (2\u0398)'
        tick_font = settings.tick_font
        self.plot.x_axis.title_font = settings.axis_title_font
        self.plot.y_axis.title_font = settings.axis_title_font
        self.plot.x_axis.tick_label_font = tick_font
        self.plot.y_axis.tick_label_font = tick_font
        #self.plot.x_axis.tick_out = 0
        #self.plot.y_axis.tick_out = 0
        self._set_scale('linear')

        # Add the traits inspector tool to the container
        self.plot.tools.append(TraitsTool(self.plot))
示例#6
0
    def _setup_plots(self):
        """Creates series of Bessel function plots"""
        plots = {}
        x = arange(self.low, self.high + 0.001,
                   (self.high - self.low) / self.numpoints)

        for i in range(self.num_funs):
            y = jn(i, x)
            if i % 2 == 1:
                plot = create_line_plot((x, y),
                                        color=tuple(COLOR_PALETTE[i]),
                                        width=2.0)
            else:
                plot = create_scatter_plot((x, y),
                                           color=tuple(COLOR_PALETTE[i]))

            if i == 0:
                value_mapper, index_mapper, legend = \
                    self._setup_plot_tools(plot)
            else:
                self._setup_mapper(plot, value_mapper, index_mapper)

            self.add(plot)
            plots["Bessel j_%d" % i] = plot

        # Set the list of plots on the legend
        legend.plots = plots

        # Add the title at the top
        self.overlays.append(
            PlotLabel("Bessel functions",
                      component=self,
                      font="swiss 16",
                      overlay_position="top"))

        # Add the traits inspector tool to the container
        self.tools.append(TraitsTool(self))
示例#7
0
def _create_plot_component():

    container = OverlayPlotContainer(padding=50,
                                     fill_padding=True,
                                     bgcolor="lightgray",
                                     use_backbuffer=True)

    # Create the initial X-series of data
    numpoints = 100
    low = -5
    high = 15.0
    x = arange(low, high + 0.001, (high - low) / numpoints)

    # Plot some bessel functions
    plots = {}
    broadcaster = BroadcasterTool()
    for i in range(4):
        y = jn(i, x)
        plot = create_line_plot((x, y),
                                color=tuple(COLOR_PALETTE[i]),
                                width=2.0)
        plot.index.sort_order = "ascending"
        plot.bgcolor = "white"
        plot.border_visible = True
        if i == 0:
            add_default_grids(plot)
            add_default_axes(plot)

        # Create a pan tool and give it a reference to the plot it should
        # manipulate, but don't attach it to the plot.  Instead, attach it to
        # the broadcaster.
        pan = PanTool(plot)
        broadcaster.tools.append(pan)

        container.add(plot)
        plots["Bessel j_%d" % i] = plot

    # Add an axis on the right-hand side that corresponds to the second plot.
    # Note that it uses plot.value_mapper instead of plot0.value_mapper.
    plot1 = plots["Bessel j_1"]
    axis = PlotAxis(plot1, orientation="right")
    plot1.underlays.append(axis)

    # Add the broadcast tool to the container, instead of to an
    # individual plot
    container.tools.append(broadcaster)

    legend = Legend(component=container, padding=10, align="ur")
    legend.tools.append(LegendTool(legend, drag_button="right"))
    container.overlays.append(legend)

    # Set the list of plots on the legend
    legend.plots = plots

    # Add the title at the top
    container.overlays.append(
        PlotLabel("Bessel functions",
                  component=container,
                  font="swiss 16",
                  overlay_position="top"))

    # Add the traits inspector tool to the container
    container.tools.append(TraitsTool(container))

    return container
示例#8
0
def _create_plot_component():
    container = OverlayPlotContainer(
        padding=50,
        fill_padding=True,
        bgcolor="lightgray",
        use_backbuffer=True)

    # Create the initial X-series of data
    numpoints = 100
    low = -5
    high = 15.0
    x = linspace(low, high, numpoints)

    now = time()
    timex = linspace(now, now + 7 * 24 * 3600, numpoints)

    # Plot some bessel functions
    value_mapper = None
    index_mapper = None
    plots = {}
    for i in range(10):
        y = jn(i, x)
        if i % 2 == 1:
            plot = create_line_plot(
                (timex, y), color=tuple(COLOR_PALETTE[i]), width=2.0)
            plot.index.sort_order = "ascending"
        else:
            plot = create_scatter_plot(
                (timex, y), color=tuple(COLOR_PALETTE[i]))
        plot.bgcolor = "white"
        plot.border_visible = True
        if i == 0:
            value_mapper = plot.value_mapper
            index_mapper = plot.index_mapper
            left, bottom = add_default_axes(plot)
            left.tick_generator = ScalesTickGenerator()
            bottom.tick_generator = ScalesTickGenerator(
                scale=CalendarScaleSystem())
            add_default_grids(plot, tick_gen=bottom.tick_generator)
        else:
            plot.value_mapper = value_mapper
            value_mapper.range.add(plot.value)
            plot.index_mapper = index_mapper
            index_mapper.range.add(plot.index)

        if i == 0:
            plot.tools.append(PanTool(plot))
            zoom = ZoomTool(plot, tool_mode="box", always_on=False)
            plot.overlays.append(zoom)
            # Add a legend in the upper right corner, and make it relocatable
            legend = Legend(component=plot, padding=10, align="ur")
            legend.tools.append(LegendTool(legend, drag_button="right"))
            plot.overlays.append(legend)

        container.add(plot)
        plots["Bessel j_%d" % i] = plot

    # Set the list of plots on the legend
    legend.plots = plots

    # Add the title at the top
    container.overlays.append(
        PlotLabel(
            "Bessel functions",
            component=container,
            font="swiss 16",
            overlay_position="top"))

    # Add the traits inspector tool to the container
    container.tools.append(TraitsTool(container))

    return container
示例#9
0
    def _plot(self, x, y, z, scale):
        pd = ArrayPlotData()
        pd.set_data("imagedata", z)
        plot = Plot(pd, padding_left=60, fill_padding=True)
        plot.bgcolor = 'white'
        cmap = fix(jet, (0, z.max()))
        origin = 'bottom left' # origin = 'top left' # to flip y-axis
        plot.img_plot("imagedata", name="surface2d",
                      xbounds=(np.min(x), np.max(x)),
                      ybounds=(1.0, y[-1,-1]),
                      colormap=cmap, hide_grids=True, interpolation='nearest',
                      origin=origin,
                      )
        plot.default_origin = origin
        plot.x_axis.title = u'Angle (2\u0398)'

        tick_font = settings.tick_font
        plot.x_axis.title_font = settings.axis_title_font
        plot.y_axis.title_font = settings.axis_title_font
        plot.x_axis.tick_label_font = tick_font
        plot.y_axis.tick_label_font = tick_font
        plot.y_axis.title = "Dataset"
        plot.y_axis.tick_interval = 1.0
        actual_plot = plot.plots["surface2d"][0]

        self.plot_zoom_tool = ClickUndoZoomTool(
            plot, tool_mode="box", always_on=True, pointer="cross",
            drag_button=settings.zoom_button,
            undo_button=settings.undo_button,
            x_min_zoom_factor=-np.inf, y_min_zoom_factor=-np.inf,
        )
        plot.overlays.append(self.plot_zoom_tool)
        plot.tools.append(TraitsTool(plot))

        # Add a color bar
        colormap = actual_plot.color_mapper
        colorbar = ColorBar(index_mapper=LinearMapper(range=colormap.range),
                        color_mapper=colormap,
                        plot=actual_plot,
                        orientation='v',
                        resizable='v',
                        width=30,
                        padding=40,
                        padding_top=50,
                        fill_padding=True)

        colorbar._axis.title_font = settings.axis_title_font
        colorbar._axis.tick_label_font = settings.tick_font
        # Add pan and zoom tools to the colorbar
        self.colorbar_zoom_tool = ClickUndoZoomTool(colorbar,
                                                    axis="index",
                                                    tool_mode="range",
                                                    always_on=True,
                                                    drag_button=settings.zoom_button,
                                                    undo_button=settings.undo_button)
        pan_tool = PanToolWithHistory(colorbar,
                                      history_tool=self.colorbar_zoom_tool,
                                      constrain_direction="y", constrain=True,
                                      drag_button=settings.pan_button)
        colorbar.tools.append(pan_tool)
        colorbar.overlays.append(self.colorbar_zoom_tool)

        # Add a label to the top of the color bar
        colorbar_label = PlotLabel(
            u'Intensity\n{:^9}'.format('(' + get_value_scale_label(scale) + ')'),
            component=colorbar,
            font=settings.axis_title_font,
        )
        colorbar.overlays.append(colorbar_label)
        colorbar.tools.append(TraitsTool(colorbar))

        # Add the plot and colorbar side-by-side
        container = HPlotContainer(use_backbuffer=True)
        container.add(plot)
        container.add(colorbar)
        return container
示例#10
0
def _create_plot_component():

    container = OverlayPlotContainer(padding=60,
                                     fill_padding=True,
                                     use_backbuffer=True,
                                     border_visible=True)

    # Create the initial X-series of data
    numpoints = 100
    low = -5
    high = 15.0
    x = arange(low, high + 0.001, (high - low) / numpoints)

    # Plot some bessel functions
    plots = {}
    broadcaster = BroadcasterTool()
    for i in range(4):
        y = jn(i, x)
        plot = create_line_plot((x, y),
                                color=tuple(COLOR_PALETTE[i]),
                                width=2.0)
        if i == 0:
            add_default_grids(plot)
            left_axis, _ = add_default_axes(plot)
            left_axis.title = "Bessel j0, j2, j3"
        elif i != 1:
            # Map correctly j2 and j3 on the first plot's axis:
            plot0 = plots["Bessel j_0"]
            plot.index_mapper = plot0.index_mapper
            plot.value_mapper = plot0.value_mapper
            plot0.value_mapper.range.add(plot.value)

        # Create a pan/zoom tool and give it a reference to the plot it should
        # manipulate, but don't attach it to the plot. Instead, attach it to
        # the broadcaster. Do it only for each independent set of axis_mappers:
        if i in [0, 1]:
            pan = PanTool(component=plot)
            broadcaster.tools.append(pan)

            zoom = ZoomTool(component=plot)
            broadcaster.tools.append(zoom)

        container.add(plot)
        plots["Bessel j_%d" % i] = plot

    # Add an axis on the right-hand side that corresponds to the second plot.
    # Note that it uses plot.value_mapper instead of plot0.value_mapper.
    plot1 = plots["Bessel j_1"]
    axis = PlotAxis(plot1, orientation="right")
    plot1.underlays.append(axis)
    axis.title = "Bessel j1"

    # Add the broadcast tool to one of the renderers: adding it to the
    # container instead breaks the box mode of the ZoomTool:
    plot0 = plots["Bessel j_0"]
    plot0.tools.append(broadcaster)

    # Create a legend, with tools to move it around and highlight renderers:
    legend = Legend(component=container, padding=10, align="ur")
    legend.tools.append(LegendTool(legend, drag_button="right"))
    legend.tools.append(LegendHighlighter(legend))
    container.overlays.append(legend)
    # Set the list of plots on the legend
    legend.plots = plots

    # Add the title at the top
    container.overlays.append(
        PlotLabel("Bessel functions",
                  component=container,
                  font="swiss 16",
                  overlay_position="top"))

    # Add the traits inspector tool to the container
    container.tools.append(TraitsTool(container))

    return container
示例#11
0
class AggregationEvaluator(HasTraits):
    step_size = Int(8)
    reduction_function = Function
    # Function to reduce the tracks to the form they were clustered in
    clust_editor = Instance(ClusterEditor)
    parameters = List
    param1_name = Str
    param2_name = Str("None", )
    param3_name = Str("None")
    n_params = Int(1)
    search_params = List
    a_calc = Button()
    plot = Instance(OverlappingPlotContainer)

    def __init__(self, **traits):
        super(AggregationEvaluator, self).__init__(**traits)
        self.clust_editor
        self.param1_name
        #self._setup_plots()

    def _plot_default(self):
        return OverlappingPlotContainer(padding=50,
                                        fill_padding=True,
                                        bgcolor="lightgray",
                                        use_backbuffer=True)

    def _parameters_default(self):
        return ["None"] + self.clust_editor.parameters

    def _param1_name_default(self):
        return self.clust_editor.parameters[0]

    @on_trait_change("param+")
    def param_selected(self):
        self.search_params = [self.param1_name]
        if not self.param2_name == "None":
            self.search_params.append(self.param2_name)
        if not self.param3_name == "None":
            self.search_params.append(self.param3_name)

    def silhouette_coefficient(self, tds):
        """ Computes the silhouette coefficient for the current set
        of clusters.
        """
        labels = np.zeros(len(tds.tracks))
        for clust in tds.clusters:
            labels[clust.indices] = clust.id_number
        if np.sum(labels > 0) < 2: return -2
        return metrics.silhouette_score(self.reduction_function(
            tds.tracks)[labels > 0],
                                        labels[labels > 0],
                                        metric='euclidean')

    def setup_plots(self, *a):
        print "calculate..."
        #if len(self.search_params) == 1:
        self._single_param_plot()

    def _a_calc_fired(self):
        self.setup_plots()

    def _single_param_plot(self):
        """Creates series of Bessel function plots"""
        for component in self.plot.components:
            self.plot.remove(component)
        self.plot.request_redraw()
        plots = {}
        self.clust_editor.interactive = False
        eval_points = np.floor(np.linspace(2, 11, self.step_size))
        original_parameter = getattr(self.clust_editor, self.param1_name)
        for tnum, tds in enumerate(self.clust_editor.track_sets):
            tds.interactive = False
            # Compute the sihlouette coefficient for each parameter value
            eval_results = []
            for eval_param in eval_points:
                setattr(self.clust_editor, self.param1_name, int(eval_param))
                try:
                    eval_results.append(self.silhouette_coefficient(tds))
                except Exception, e:
                    print e
                    eval_results.append(-2)
            _plot = create_line_plot((eval_points, np.array(eval_results)),
                                     color=tuple(COLOR_PALETTE[tnum]),
                                     width=2.0)
            if tnum == 0:
                value_mapper, index_mapper, legend = \
                    self._setup_plot_tools(_plot)
            else:
                self._setup_mapper(_plot, value_mapper, index_mapper)

            self.plot.add(_plot)
            plots[tds.name] = _plot

        # Add lines to the legend
        legend.plots = plots

        # Add the title at the top
        self.plot.overlays.append(
            PlotLabel("Sihlouette Coefficient",
                      component=self.plot,
                      font="swiss 16",
                      overlay_position="top"))
        # Traits inspector tool
        self.plot.tools.append(TraitsTool(self.plot))

        # Set the cluster editor back to its original glory
        self.clust_editor.interactive = True
        setattr(self.clust_editor, self.param1_name, original_parameter)