Esempio n. 1
0
def _create_plot_component():

    # Create a random scattering of XY pairs
    x = random.uniform(0.0, 10.0, 50)
    y = random.uniform(0.0, 5.0, 50)
    pd = ArrayPlotData(x = x, y = y)
    plot = Plot(pd, border_visible=True, overlay_border=True)

    scatter = plot.plot(("x", "y"), type="scatter", color="lightblue")[0]

    # Tweak some of the plot properties
    plot.set(title="Scatter Inspector Demo", padding=50)

    # Attach some tools to the plot
    plot.tools.append(PanTool(plot))
    plot.overlays.append(ZoomTool(plot))

    # Attach the inspector and its overlay
    inspector = ScatterInspector(scatter)
    scatter.tools.append(inspector)
    overlay = ScatterInspectorOverlay(scatter,
                    hover_color="red",
                    hover_marker_size=6,
                    selection_marker_size=6,
                    selection_color="yellow",
                    selection_outline_color="purple",
                    selection_line_width=3)
    scatter.overlays.append(overlay)

    # Optional: add a listener on inspector events:
    def echo(new):
        print("{} event on element {}".format(new.event_type, new.event_index))
    inspector.on_trait_change(echo, "inspector_event")

    return plot
Esempio n. 2
0
def _create_plot_component():

    # Create a random scattering of XY pairs
    x = random.uniform(0.0, 10.0, 50)
    y = random.uniform(0.0, 5.0, 50)
    pd = ArrayPlotData(x=x, y=y)
    plot = Plot(pd, border_visible=True, overlay_border=True)

    scatter = plot.plot(("x", "y"), type="scatter", color="lightblue")[0]

    # Tweak some of the plot properties
    plot.set(title="Scatter Inspector Demo", padding=50)

    # Attach some tools to the plot
    plot.tools.append(PanTool(plot))
    plot.overlays.append(ZoomTool(plot))

    # Attach the inspector and its overlay
    scatter.tools.append(ScatterInspector(scatter))
    overlay = ScatterInspectorOverlay(scatter,
                                      hover_color="red",
                                      hover_marker_size=6,
                                      selection_marker_size=6,
                                      selection_color="yellow",
                                      selection_outline_color="purple",
                                      selection_line_width=3)
    scatter.overlays.append(overlay)

    return plot
 def install(self):
     scatter = self.widget.scatter
     self._tool = ScatterInspector(scatter,
         selection_metadata_name = 'selection',
         selection_mode = 'toggle',
     )
     scatter.tools.append(self._tool)
Esempio n. 4
0
def _create_plot_component():

    # Create some data
    npts = 100
    x = sort(random(npts))
    y = random(npts)

    # Create a plot data obect and give it this data
    pd = ArrayPlotData()
    pd.set_data("index", x)
    pd.set_data("value", y)
    pd.set_data("value2", y * 2)

    # Create the plot
    plot = Plot(pd)
    plot.plot(("index", "value"),
              type="scatter",
              name="my_plot",
              marker="circle",
              index_sort="ascending",
              color="slategray",
              marker_size=6,
              bgcolor="white")
    plot.plot(("index", "value2"),
              type="scatter",
              name="my_plot",
              marker="circle",
              index_sort="ascending",
              color="red",
              marker_size=6,
              bgcolor="white")

    # Tweak some of the plot properties
    plot.title = "Scatter Plot With Selection"
    plot.line_width = 1
    plot.padding = 50

    # Right now, some of the tools are a little invasive, and we need the
    # actual ScatterPlot object to give to them
    my_plot = plot.plots["my_plot"][0]

    # Attach some tools to the plot
    my_plot.tools.append(
        ScatterInspector(my_plot,
                         selection_mode="toggle",
                         persistent_hover=False))
    my_plot.overlays.append(
        ScatterInspectorOverlay(my_plot,
                                hover_color="transparent",
                                hover_marker_size=10,
                                hover_outline_color="purple",
                                hover_line_width=2,
                                selection_marker_size=8,
                                selection_color="lawngreen"))

    my_plot.tools.append(PanTool(my_plot))
    my_plot.overlays.append(ZoomTool(my_plot, drag_button="right"))

    return plot
Esempio n. 5
0
    def _create_plot_component(self):
        """ Creates the plot component of the to be used in the FeatureScatter
            instance.
        """
        x = np.zeros(len(self.data))
        y = np.zeros(len(self.data))
        c = np.zeros(len(self.data))

        for i, (coord, count) in enumerate(self.data.items()):
            x[i], y[i] = coord
            c[i] = count

        c = np.log2(c)

        pd = ArrayPlotData()
        pd.set_data("x", x)
        pd.set_data("y", y)
        pd.set_data("color", c)

        cm = Map(DataRange1D(low=-c.max() / 2, high=c.max()))

        plot = Plot(pd)
        plot.plot(("x", "y", "color"),
                  type="cmap_scatter",
                  name="my_plot",
                  marker="dot",
                  index_sort="ascending",
                  color_mapper=cm,
                  marker_size=2,
                  bgcolor=0xF7F7F7,
                  )

        plot.title = "Scatter Plot With Lasso Selection"
        plot.line_width = 1
        plot.padding = 50

        my_plot = plot.plots["my_plot"][0]
        my_plot.data = self.data
        my_plot.out_file = self.out_file
        my_plot.label = self.label

        lasso_selection = FeatureLasso(component=my_plot,
                                       selection_datasource=my_plot.index,
                                       drag_button="left")

        my_plot.tools.append(lasso_selection)
        my_plot.tools.append(BetterZoom(my_plot, zoom_factor=1.2))
        my_plot.tools.append(PanTool(my_plot, drag_button="right"))
        my_plot.tools.append(ScatterInspector(my_plot))

        lasso_overlay = LassoOverlay(lasso_selection=lasso_selection,
                                     component=my_plot,
                                     selection_fill_color=0xEF8A62)

        my_plot.overlays.append(lasso_overlay)
        my_plot.overlays.append(ScatterInspectorOverlay(my_plot,
                                hover_marker_size=4))

        return plot
Esempio n. 6
0
 def _plot_default(self):
     import numpy as np
     data = np.random.normal(size=(10, 2))
     apl = ArrayPlotData(x=data[:, 0], y=data[:, 1])
     plot = Plot(apl)
     scatter = plot.plot(('x', 'y'), type='scatter')[0]
     self.inspector = ScatterInspector(scatter)
     scatter.tools.append(self.inspector)
     return plot
    def setUp(self):
        values = numpy.arange(10)
        self.plot = create_scatter_plot((values, values))
        self.plot.bounds = [100, 100]
        self.plot._window = self.create_mock_window()
        self.tool = ScatterInspector(component=self.plot)
        self.plot.active_tool = self.tool
        self.plot.do_layout()

        self.insp_event = None
Esempio n. 8
0
def _create_plot_component():
    # Create some data
    npts = 200
    x = sort(random(npts))
    y = random(npts)

    # Create a plot data obect and give it this data
    pd = ArrayPlotData()
    pd.set_data("index", x)
    pd.set_data("value", y)

    # Create the plot
    plot = Plot(pd)
    plot.plot(("index", "value"),
              type="scatter",
              name="my_plot",
              marker="circle",
              index_sort="ascending",
              color="red",
              marker_size=4,
              bgcolor="white")

    # Tweak some of the plot properties
    plot.title = "Scatter Plot With Rectangular Selection"
    plot.line_width = 1
    plot.padding = 50

    # Right now, some of the tools are a little invasive, and we need the
    # actual ScatterPlot object to give to them
    my_plot = plot.plots["my_plot"][0]

    # Attach some tools to the plot
    rect_selection = RectangularSelection(
        component=my_plot,
        selection_datasource=my_plot.index,
        drag_button="left",
        metadata_name='selections',
    )
    my_plot.tools.append(rect_selection)
    my_plot.tools.append(ScatterInspector(my_plot, selection_mode='toggle'))
    my_plot.active_tool = rect_selection

    lasso_overlay = LassoOverlay(lasso_selection=rect_selection,
                                 component=my_plot)
    my_plot.overlays.append(lasso_overlay)

    scatter_overlay = ScatterInspectorOverlay(
        component=my_plot,
        selection_color='cornflowerblue',
        selection_marker_size=int(my_plot.marker_size) + 3,
        selection_marker='circle')
    my_plot.overlays.append(scatter_overlay)

    return plot
Esempio n. 9
0
    def overlay_selection(self, plot):
        lasso_selection = LassoSelection(component=plot,
                                         selection_datasource=plot.index,
                                         drag_button="left")
        plot.active_tool = lasso_selection
        plot.tools.append(ScatterInspector(plot))
        lasso_overlay = LassoOverlay(lasso_selection=lasso_selection,
                                     component=plot)
        plot.overlays.append(lasso_overlay)

        # Uncomment this if you would like to see incremental updates:
        # lasso_selection.incremental_select = True
        self.index_datasource = plot.index
Esempio n. 10
0
def _create_plot_component():

    # Create some data
    npts = 2000
    x = sort(random(npts))
    y = random(npts)

    # Create a plot data obect and give it this data
    pd = ArrayPlotData()
    pd.set_data("index", x)
    pd.set_data("value", y)

    # Create the plot
    plot = Plot(pd)
    plot.plot(("index", "value"),
              type="scatter",
              name="my_plot",
              marker="circle",
              index_sort="ascending",
              color="red",
              marker_size=4,
              bgcolor="white")

    # Tweak some of the plot properties
    plot.title = "Scatter Plot With Lasso Selection"
    plot.line_width = 1
    plot.padding = 50

    # Right now, some of the tools are a little invasive, and we need the
    # actual ScatterPlot object to give to them
    my_plot = plot.plots["my_plot"][0]

    # Attach some tools to the plot
    lasso_selection = LassoSelection(component=my_plot,
                                     selection_datasource=my_plot.index,
                                     drag_button="left")
    #drag_button="right")
    my_plot.active_tool = lasso_selection
    my_plot.tools.append(ScatterInspector(my_plot))
    lasso_overlay = LassoOverlay(lasso_selection=lasso_selection,
                                 component=my_plot)
    my_plot.overlays.append(lasso_overlay)

    # Uncomment this if you would like to see incremental updates:
    #lasso_selection.incremental_select = True

    return plot
Esempio n. 11
0
    def _plot_default(self):
        container = DisableTrackingPlot(
            self.model.plot_data,
            live_plot=self,
            )
        container.padding_left = 100
        container.plot(('x', 'y'), type='line')

        scatter = container.plot(
            ('x', 'y'),
            type='scatter',
            marker_size=1,
            show_selection=False,
            color='lightskyblue',
            )
        self.scatter = scatter[0]
        inspector = ScatterInspector(
            self.scatter,
            selection_mode='single',
            )
        self.scatter.tools.append(inspector)
        overlay = ScatterInspectorOverlay(self.scatter,
                                          hover_color="lightskyblue",
                                          hover_marker_size=2,
                                          selection_marker_size=3,
                                          selection_color="lightskyblue",
                                          selection_outline_color="black",
                                          selection_line_width=1)
        self.scatter.overlays.append(overlay)

        self.scatter.index.on_trait_change(
            self._metadata_changed, "metadata_changed")

        self.zoom_tool = ZoomTool(
            container,
            )
        container.underlays.append(self.zoom_tool)
        container.tools.append(self.zoom_tool)
        self.pan_tool = PanTool(
            container,
            )
        container.tools.append(self.pan_tool)

        return container
Esempio n. 12
0
    def _add_scatter_inspector_overlay(self, scatter_plot):

        inspector = ScatterInspector(
            scatter_plot,
            threshold=10,
            multiselect_modifier=KeySpec(None, "shift"),
            selection_mode="multi",
        )

        overlay = ScatterInspectorOverlay(
            scatter_plot,
            hover_color=(0, 0, 1, 1),
            hover_marker_size=6,
            selection_marker_size=20,
            selection_color=(0, 0, 1, 0.5),
            selection_outline_color=(0, 0, 0, 0.8),
            selection_line_width=3,
        )

        scatter_plot.tools.append(inspector)
        scatter_plot.overlays.append(overlay)
Esempio n. 13
0
    def _create_plot_component(self):
        """ Creates the plot component of the to be used in the FeatureScatter
            instance.
        """
        feature_types = self.features.keys()

        print(feature_types)

        x = np.array(self.features["BORDER"]["x"] + self.features["ADDITIONAL_BORDER"]["x"])
        y = np.array(self.features["BORDER"]["y"] + self.features["ADDITIONAL_BORDER"]["y"])

        pd = ArrayPlotData()
        pd.set_data("x", x)
        pd.set_data("y", y)

        plot = Plot(pd)
        plot.plot(("x", "y"),
                  type="scatter",
                  name="my_plot",
                  color="red",
                  index_sort="ascending",
                  marker_size=2.0,
                  bgcolor="black",
                  line_width=0,
                  )

        my_plot = plot.plots["my_plot"][0]

        my_plot.tools.append(BetterZoom(my_plot, zoom_factor=1.2))
        my_plot.tools.append(PanTool(my_plot, drag_button="right"))
        my_plot.tools.append(ScatterInspector(my_plot))

        my_plot.overlays.append(ScatterInspectorOverlay(my_plot,
                                hover_marker_size=4))

        return plot
Esempio n. 14
0
    def _create_plot_component(self, recalc=False):

        container = VPlotContainer()

        ### Assemble the scatter plot of the Efficient Frontier
        x, y = self.get_stock_data()
        if not hasattr(self, "efx") or recalc:
            efx, efy, allocations = self.get_ef_data()
        else:
            efx = self.efx
            efy = self.efy

        p = self.portfolio

        symbs = p.symbols

        pd = ArrayPlotData(x=x, y=y, efx=efx, efy=efy, mp_x=[self.model_portfolio_x], mp_y=[self.model_portfolio_y])

        # Create some plots of the data
        plot = Plot(pd, title="Efficient Frontier")

        # Create a scatter plot (and keep a handle on it)
        stockplt = plot.plot(("x", "y"), color="transparent",
                                         type="scatter",
                                         marker="dot",
                                         marker_line_color="transparent",
                                         marker_color="transparent",
                                         marker_size=1)[0]

        efplt = plot.plot(("efx", "efy"), color=(0.0,0.5,0.0,0.25),
                                          type="scatter",
                                          marker="circle",
                                          marker_size=6)[0]
        efpltline = plot.plot(("efx", "efy"), color=(0.1,0.4,0.1,0.7),
                                          type="line")[0]


        # Create another one-point scatter for a model portfolio
        mp_plot = plot.plot(("mp_x", "mp_y"), color=(1.0, 0.5, 0.5, 0.25),
            type="scatter",
            market="triangle",
            market_size=7)[0]

        for i in range(len(p.stocks)):
            label = DataPointLabel(component=plot, data_point=(x[i], y[i]),
                              label_position="bottom right",
                              padding=4,
                              bgcolor="transparent",
                              border_visible=False,
                              text=self.symbols[i],
                              marker="circle",
                              marker_color=(0.0,0.0,0.5,0.25),
                              marker_line_color="lightgray",
                              marker_size=6,
                              arrow_size=8.0,
                              arrow_min_length=7.0,
                              font_size=14)

            plot.overlays.append(label)

            tool = DataLabelTool(label, drag_button="left", auto_arrow_root=True)
            label.tools.append(tool)

        stockplt.tools.append(ScatterInspector(stockplt, selection_mode="toggle",
                                          persistent_hover=False))

        scatinsp = ScatterInspectorOverlay(stockplt,
                hover_color = "red",
                hover_marker_size = 8,
                hover_outline_color = (0.7, 0.7, 0.7, 0.5),
                hover_line_width = 1)

        stockplt.overlays.append(scatinsp)

        # Tweak some of the plot properties
        plot.padding = 50
        stockplt.value_range.low=0.0
        stockplt.value_range.high=0.1
        stockplt.index_range.low=0.0
        stockplt.index_range.high=0.1
        # Attach some tools to the plot
        plot.tools.append(PanTool(plot, drag_button="right"))
        plot.overlays.append(ZoomTool(plot))

        #### Assemble the "stacked area" plot
        if not hasattr(self, "efx") or recalc:
            a = self.get_ef_data()[2]
        else:
            a = self.allocations

        rts = a.keys()
        rts.sort()
        rts = np.array(rts)

        symbs = a[rts[0]].keys()
        symbs.sort()

        # "Transpose" symbols' weights to get vectors of weights for each symbol
        symb_data = np.array([[a[rt][symb] for rt in rts] for symb in symbs])

        self.symbols2 = [Symbol(symbol=symbs[i], color=COLORS[i]) for i in range(len(symbs))]

        # Create a plot data object and give it this data
        bpd = ArrayPlotData()
        bpd.set_data("index", rts)
        bpd.set_data("allocations", symb_data)

        # Create a contour polygon plot of the data
        bplot = Plot(bpd, title="Allocations")
        bplot.stacked_bar_plot(("index", "allocations"),
                        color = COLORS,
                        outline_color = "gray")

        bplot.padding = 50
        #bplot.legend.visible = True

        # Add a plot of the stocks
        stock_obj_list = [p.stocks[symb] for symb in symbs]
        
        #for itm in stock_obj_list:
            #itm.print_traits()
            #print "Has Cache?:", itm.stock_data_cache is not None

        splot = StockPlot(stocks=[p.stocks[symb] for symb in symbs], colors=COLORS).plot

        container.add(bplot)
        container.add(plot)
        container.add(splot)
        
        return container