Beispiel #1
0
def _create_plot_component():

    # Create some data
    index, vals = _create_data(20)

    # Create a plot data object and give it this data
    pd = ArrayPlotData(index = index,
                       values = vals)

    # Create the plot
    plot = Plot(pd)
    plot.stacked_bar_plot(("index", "values"),
                     color = ["red", "yellow", "green", "blue"],
                     outline_color = "lightgray",)

    # Tweak some of the plot properties
    plot.title = "Stacked Bar Plot"
    plot.line_width = 0.5
    plot.padding = 50

    # Attach some tools to the plot
    plot.tools.append(PanTool(plot, constrain_key="shift"))
    zoom = ZoomTool(component=plot, tool_mode="box", always_on=False)
    plot.overlays.append(zoom)

    return plot
Beispiel #2
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