示例#1
0
def draw_sell():
    g0 = Triangle(x='T',
                  y='H',
                  size=10,
                  fill_color='red',
                  angle=pi,
                  fill_alpha=0.8)

    return g0
示例#2
0
def draw_buy():
    g0 = Triangle(x='T',
                  y='L',
                  size=10,
                  fill_color='green',
                  angle=0.0,
                  fill_alpha=0.8)

    return g0
示例#3
0
文件: Triangle.py 项目: ogrisel/bokeh
xdr = DataRange1d(sources=[source.columns("x")])
ydr = DataRange1d(sources=[source.columns("y")])

plot = Plot(title=None,
            x_range=xdr,
            y_range=ydr,
            plot_width=300,
            plot_height=300,
            h_symmetry=False,
            v_symmetry=False,
            min_border=0,
            toolbar_location=None)

glyph = Triangle(x="x",
                 y="y",
                 size="sizes",
                 line_color="#99d594",
                 line_width=2,
                 fill_color=None)
plot.add_glyph(source, glyph)

xaxis = LinearAxis()
plot.add_layout(xaxis, 'below')

yaxis = LinearAxis()
plot.add_layout(yaxis, 'left')

plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker))
plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker))

doc = Document()
doc.add(plot)
示例#4
0
    def bokeh(self,
              glyphType="line",
              glyphSize=1,
              fillColor="red",
              lineColor="black",
              lineAlpha=1,
              fillAlpha=0.1,
              lineDash='solid'):

        #glyphs
        from bokeh.models.glyphs import Rect, Segment, Line, Patches, Arc
        from bokeh.models.renderers import GlyphRenderer
        from bokeh.models.markers import (Marker, Asterisk, Circle,
                                          CircleCross, CircleX, Cross, Diamond,
                                          DiamondCross, InvertedTriangle,
                                          Square, SquareCross, SquareX,
                                          Triangle, X)

        #data
        from bokeh.models import ColumnDataSource

        from math import sqrt

        #Parameters of the histogram
        l = self.low
        h = self.high
        num = self.num
        bin_width = (h - l) / num
        x = list()
        y = list()
        center = l
        for v in self.values:
            if not math.isnan(v.mean):
                y.append(v.mean)
                x.append(center + bin_width / 2)
                center += bin_width

        source = ColumnDataSource(data=dict(x=x, y=y))

        glyph = None
        if glyphType == "square":
            glyph = Square(x='x',
                           y='y',
                           line_color=lineColor,
                           fill_color=fillColor,
                           line_alpha=lineAlpha,
                           size=glyphSize,
                           line_dash=lineDash)
        elif glyphType == "diamond":
            glyph = Diamond(x='x',
                            y='y',
                            line_color=lineColor,
                            fill_color=fillColor,
                            line_alpha=lineAlpha,
                            size=glyphSize,
                            line_dash=lineDash)
        elif glyphType == "cross":
            glyph = Cross(x='x',
                          y='y',
                          line_color=lineColor,
                          fill_color=fillColor,
                          line_alpha=lineAlpha,
                          size=glyphSize,
                          line_dash=lineDash)
        elif glyphType == "triangle":
            glyph = Triangle(x='x',
                             y='y',
                             line_color=lineColor,
                             fill_color=fillColor,
                             line_alpha=lineAlpha,
                             size=glyphSize,
                             line_dash=lineDash)
        elif glyphType == "circle":
            glyph = Circle(x='x',
                           y='y',
                           line_color=lineColor,
                           fill_color=fillColor,
                           line_alpha=lineAlpha,
                           size=glyphSize,
                           line_dash=lineDash)
        elif glyphType == "errors":
            w = [bin_width for _ in x]
            h = [
                sqrt(v.variance / v.entries) if v.entries > 0 else 0.0
                for v in self.values
            ]
            source = ColumnDataSource(dict(x=x, y=y, w=w, h=h))
            glyph = Rect(x='x',
                         y='y',
                         width='w',
                         height='h',
                         fill_alpha=fillAlpha,
                         line_color=lineColor,
                         fill_color=fillColor)
        elif glyphType == "histogram":
            w = [bin_width for _ in x]
            h = y
            y = [yy / 2 for yy in y]
            source = ColumnDataSource(dict(x=x, y=y, w=w, h=h))
            glyph = Rect(x='x',
                         y='y',
                         width='w',
                         height='h',
                         fill_alpha=fillAlpha,
                         line_color=lineColor,
                         fill_color=fillColor)
        else:
            glyph = Line(x='x',
                         y='y',
                         line_color=lineColor,
                         line_alpha=lineAlpha,
                         line_width=glyphSize,
                         line_dash=lineDash)

        return GlyphRenderer(glyph=glyph, data_source=source)
示例#5
0
    def bokeh(self,
              glyphType="line",
              glyphSize=1,
              fillColor="red",
              lineColor="black",
              lineAlpha=1,
              fillAlpha=0.1,
              lineDash='solid'):

        #glyphs
        from bokeh.models.glyphs import Rect, Segment, Line, Patches, Arc
        from bokeh.models.renderers import GlyphRenderer
        from bokeh.models.markers import (Marker, Asterisk, Circle,
                                          CircleCross, CircleX, Cross, Diamond,
                                          DiamondCross, InvertedTriangle,
                                          Square, SquareCross, SquareX,
                                          Triangle, X)

        #data
        from bokeh.models import ColumnDataSource

        #Parameters of the histogram
        l = self.low
        h = self.high
        num = self.num
        bin_width = (h - l) / num
        x = list()
        center = l
        for _ in range(num):
            x.append(center + bin_width / 2)
            center += bin_width
        y = self.numericalValues
        ci = [2. * v for v in self.confidenceIntervalValues()]

        source = ColumnDataSource(data=dict(x=x, y=y, ci=ci))

        glyph = None
        if glyphType == "square":
            glyph = Square(x='x',
                           y='y',
                           line_color=lineColor,
                           fill_color=fillColor,
                           line_alpha=lineAlpha,
                           size=glyphSize,
                           line_dash=lineDash)
        elif glyphType == "diamond":
            glyph = Diamond(x='x',
                            y='y',
                            line_color=lineColor,
                            fill_color=fillColor,
                            line_alpha=lineAlpha,
                            size=glyphSize,
                            line_dash=lineDash)
        elif glyphType == "cross":
            glyph = Cross(x='x',
                          y='y',
                          line_color=lineColor,
                          fill_color=fillColor,
                          line_alpha=lineAlpha,
                          size=glyphSize,
                          line_dash=lineDash)
        elif glyphType == "triangle":
            glyph = Triangle(x='x',
                             y='y',
                             line_color=lineColor,
                             fill_color=fillColor,
                             line_alpha=lineAlpha,
                             size=glyphSize,
                             line_dash=lineDash)
        elif glyphType == "circle":
            glyph = Circle(x='x',
                           y='y',
                           line_color=lineColor,
                           fill_color=fillColor,
                           line_alpha=lineAlpha,
                           size=glyphSize,
                           line_dash=lineDash)
        elif glyphType == "rect":
            glyph = Rect(x='x',
                         y='y',
                         width=bin_width,
                         height=0.1,
                         fill_alpha=fillAlpha,
                         line_color=lineColor,
                         fill_color=fillColor)
        elif glyphType == "errors":
            glyph = Rect(x='x',
                         y='y',
                         width=bin_width,
                         height='ci',
                         fill_alpha=fillAlpha,
                         line_color=lineColor,
                         fill_color=fillColor)
        elif glyphType == "histogram":
            h = y
            y = [yy / 2 for yy in y]
            source = ColumnDataSource(dict(x=x, y=y, h=h))
            glyph = Rect(x='x',
                         y='y',
                         width=bin_width,
                         height='h',
                         fill_alpha=fillAlpha,
                         line_color=lineColor,
                         fill_color=fillColor)
        else:
            glyph = Line(x='x',
                         y='y',
                         line_color=lineColor,
                         line_alpha=lineAlpha,
                         line_width=glyphSize,
                         line_dash=lineDash)

        return GlyphRenderer(glyph=glyph, data_source=source)
示例#6
0
    def plotbokeh(self, glyphType="line", glyphSize=1, fillColor="red",
                  lineColor="black", lineAlpha=1, fillAlpha=0.1, lineDash='solid'):

        # glyphs
        from bokeh.models.glyphs import Rect, Line
        from bokeh.models.renderers import GlyphRenderer
        from bokeh.models.markers import (Circle, Cross,
                                          Diamond, Square,
                                          Triangle)

        # data
        from bokeh.models import ColumnDataSource

        # Parameters of the histogram
        lo = self.low
        hi = self.high
        num = self.num
        bin_width = (hi-lo)/num
        x = list()
        y = list()
        center = lo
        for v in self.values:
            if not math.isnan(v.mean):
                y.append(v.mean)
                x.append(center+bin_width/2)
                center += bin_width

        source = ColumnDataSource(data=dict(x=x, y=y))

        glyph = None
        if glyphType == "square":
            glyph = Square(
                x='x',
                y='y',
                line_color=lineColor,
                fill_color=fillColor,
                line_alpha=lineAlpha,
                size=glyphSize,
                line_dash=lineDash)
        elif glyphType == "diamond":
            glyph = Diamond(
                x='x',
                y='y',
                line_color=lineColor,
                fill_color=fillColor,
                line_alpha=lineAlpha,
                size=glyphSize,
                line_dash=lineDash)
        elif glyphType == "cross":
            glyph = Cross(
                x='x',
                y='y',
                line_color=lineColor,
                fill_color=fillColor,
                line_alpha=lineAlpha,
                size=glyphSize,
                line_dash=lineDash)
        elif glyphType == "triangle":
            glyph = Triangle(
                x='x',
                y='y',
                line_color=lineColor,
                fill_color=fillColor,
                line_alpha=lineAlpha,
                size=glyphSize,
                line_dash=lineDash)
        elif glyphType == "circle":
            glyph = Circle(
                x='x',
                y='y',
                line_color=lineColor,
                fill_color=fillColor,
                line_alpha=lineAlpha,
                size=glyphSize,
                line_dash=lineDash)
        elif glyphType == "histogram":
            w = [bin_width for _ in x]
            h = y
            y = [yy/2 for yy in y]
            source = ColumnDataSource(dict(x=x, y=y, w=w, h=h))
            glyph = Rect(
                x='x',
                y='y',
                width='w',
                height='h',
                fill_alpha=fillAlpha,
                line_color=lineColor,
                fill_color=fillColor)
        else:
            glyph = Line(
                x='x',
                y='y',
                line_color=lineColor,
                line_alpha=lineAlpha,
                line_width=glyphSize,
                line_dash=lineDash)

        return GlyphRenderer(glyph=glyph, data_source=source)
示例#7
0
    def plotbokeh(self, glyphType="line", glyphSize=1, fillColor="red",
                  lineColor="black", lineAlpha=1, fillAlpha=0.1, lineDash='solid'):

        # glyphs
        from bokeh.models.glyphs import Rect, Line
        from bokeh.models.renderers import GlyphRenderer
        from bokeh.models.markers import (Circle, Cross,
                                          Diamond, Square,
                                          Triangle)

        # data
        from bokeh.models import ColumnDataSource

        # Parameters of the histogram
        lo = self.low
        hi = self.high
        num = self.numFilled
        bin_width = (hi-lo)/num
        x = list()
        center = lo
        for _ in range(num):
            x.append(center+bin_width/2)
            center += bin_width
        y = [v.entries for _, v in sorted(self.bins.items())]

        source = ColumnDataSource(data=dict(x=x, y=y))

        glyph = None
        if glyphType == "square":
            glyph = Square(
                x='x',
                y='y',
                line_color=lineColor,
                fill_color=fillColor,
                line_alpha=lineAlpha,
                size=glyphSize,
                line_dash=lineDash)
        elif glyphType == "diamond":
            glyph = Diamond(
                x='x',
                y='y',
                line_color=lineColor,
                fill_color=fillColor,
                line_alpha=lineAlpha,
                size=glyphSize,
                line_dash=lineDash)
        elif glyphType == "cross":
            glyph = Cross(
                x='x',
                y='y',
                line_color=lineColor,
                fill_color=fillColor,
                line_alpha=lineAlpha,
                size=glyphSize,
                line_dash=lineDash)
        elif glyphType == "triangle":
            glyph = Triangle(
                x='x',
                y='y',
                line_color=lineColor,
                fill_color=fillColor,
                line_alpha=lineAlpha,
                size=glyphSize,
                line_dash=lineDash)
        elif glyphType == "circle":
            glyph = Circle(
                x='x',
                y='y',
                line_color=lineColor,
                fill_color=fillColor,
                line_alpha=lineAlpha,
                size=glyphSize,
                line_dash=lineDash)
        elif glyphType == "rect":
            glyph = Rect(
                x='x',
                y='y',
                width=bin_width,
                height=0.1,
                fill_alpha=fillAlpha,
                line_color=lineColor,
                fill_color=fillColor)
        elif glyphType == "errors":
            ci = [2.*v for v in self.confidenceIntervalValues()]
            source = ColumnDataSource(data=dict(x=x, y=y, ci=ci))
            glyph = Rect(
                x='x',
                y='y',
                width=bin_width,
                height='ci',
                fill_alpha=fillAlpha,
                line_color=lineColor,
                fill_color=fillColor)
        elif glyphType == "histogram":
            h = y
            y = [yy/2 for yy in y]
            source = ColumnDataSource(dict(x=x, y=y, h=h))
            glyph = Rect(
                x='x',
                y='y',
                width=bin_width,
                height='h',
                fill_alpha=fillAlpha,
                line_color=lineColor,
                fill_color=fillColor)
        else:
            glyph = Line(
                x='x',
                y='y',
                line_color=lineColor,
                line_alpha=lineAlpha,
                line_width=glyphSize,
                line_dash=lineDash)

        return GlyphRenderer(glyph=glyph, data_source=source)
示例#8
0
    def visualize(self):
        """
        Generates a plot using bokeh, which displays the initial trajectory and
        the optimized trajectory of the cutting tool.
        """

        # Tools that will be displayed on the plots
        tools = "pan,wheel_zoom,reset,save"

        # Plot displaying the optimized path
        result_plot = figure(plot_width=1000,
                             plot_height=500,
                             tools=tools,
                             active_scroll='wheel_zoom')
        result_plot.title.text = "Optimized Path"

        # Plot displaying the non optimized path
        initial_plot = figure(plot_width=1000,
                              plot_height=500,
                              tools=tools,
                              active_scroll='wheel_zoom')
        initial_plot.title.text = "Initial Path"

        # Add the data to the result plot
        result_plot = self.populate_plot(result_plot, self.result)
        result_plot.legend.location = "bottom_right"

        # Add the data to the initial plot
        initial_plot = self.populate_plot(initial_plot, self.initial)
        initial_plot.legend.location = "bottom_right"

        # Add cutting tool to plots
        # Generate the points on which the triangle should move on
        result_lines_x, result_lines_y = self.generate_tool_path(
            self.result, 1)
        initial_lines_x, initial_lines_y = self.generate_tool_path(
            self.initial, 1)

        # Add cutting tool triangle to optimized path
        result_triangle_position = ColumnDataSource(
            data=dict(x=[result_lines_x[0]], y=[result_lines_y[0]]))
        result_triangle = Triangle(x='x',
                                   y='y',
                                   line_color=Category10_4[3],
                                   line_width=3,
                                   size=20,
                                   fill_alpha=0)
        result_plot.add_glyph(result_triangle_position, result_triangle)

        # Add cutting tool triangle to initial path
        initial_triangle_position = ColumnDataSource(
            data=dict(x=[initial_lines_x[0]], y=[initial_lines_y[0]]))
        initial_triangle = Triangle(x='x',
                                    y='y',
                                    line_color=Category10_4[3],
                                    line_width=3,
                                    size=20,
                                    fill_alpha=0)
        initial_plot.add_glyph(initial_triangle_position, initial_triangle)

        # Add button to start moving the triangle
        button = Button(label='Start')
        result_num_steps = result_lines_x.shape[0]
        initial_num_steps = initial_lines_x.shape[0]
        num_steps = max(result_num_steps, initial_num_steps)

        # JavaScript callback which will be called once the button is pressed
        callback = CustomJS(args=dict(
            result_triangle_position=result_triangle_position,
            result_lines_x=result_lines_x,
            result_lines_y=result_lines_y,
            result_num_steps=result_num_steps,
            initial_triangle_position=initial_triangle_position,
            initial_lines_x=initial_lines_x,
            initial_lines_y=initial_lines_y,
            initial_num_steps=initial_num_steps,
            num_steps=num_steps),
                            code="""
            // Animate optimal path plot
            for(let i = 0; i < num_steps; i += 50) {
                setTimeout(function() {
                    if (i < result_num_steps) {
                        result_triangle_position.data['x'][0] = result_lines_x[i]
                        result_triangle_position.data['y'][0] = result_lines_y[i]
                    }

                    if (i < initial_num_steps) {
                        initial_triangle_position.data['x'][0] = initial_lines_x[i]
                        initial_triangle_position.data['y'][0] = initial_lines_y[i]
                    }

                    result_triangle_position.change.emit()
                    initial_triangle_position.change.emit()

                }, i)
            }
        """)
        # Add callback function to button, which starts the whole animation
        button.js_on_click(callback)

        # Save the plot
        result_plot = row([result_plot, button])
        plot = column([result_plot, initial_plot])
        output_file("visualization.html", title="CNC Path Optimization")
        save(plot)
示例#9
0
    x = [1,2,3,4,5]
    sizes = [10]*len(x)
    circles = [1]*len(x)
    crosses = [2]*len(x)
    triangles = [3]*len(x)
    exes = [4]*len(x)
    asterisks = [5]*len(x)
    diamonds = [6]*len(x)
    squares = [7]*len(x)

    scatter_data = ColumnDataSource(dict(x=x, circles=circles, crosses=crosses, triangles=triangles, exes=exes, asterisks=asterisks, diamonds=diamonds, squares=squares, sizes=sizes))

    glyphs = []
    glyphs.append(Circle(x="x", y="circles", size="sizes", fill_color="red", name="the_circles"))
    glyphs.append(Cross(x="x", y="crosses", size="sizes", fill_color="blue", name="the_crosses"))
    glyphs.append(Triangle(x="x", y="triangles", size="sizes", fill_color="green", name="the_triangles"))
    glyphs.append(X(x="x", y="exes", size="sizes", fill_color="purple", name="the_xs"))
    glyphs.append(Asterisk(x="x", y="asterisks", size="sizes", fill_color="orange", name="the_asterisks"))
    glyphs.append(Diamond(x="x", y="diamonds", size="sizes", fill_color="yellow", name="the_diamonds"))
    glyphs.append(Square(x="x", y="squares", size="sizes", fill_color="gray", name="the_squares"))

    legend_items = []
    for glyph in glyphs:
        renderer = p.add_glyph(scatter_data, glyph)
        renderer.name = glyph.name
        legend_items.append((renderer.name, [renderer]))
    
    legend = Legend(
        items=legend_items,
        name="the_legend",
        label_text_font_size='5pt',