Ejemplo n.º 1
0
def plot_bokeh_events(src, plot=None, pitch_colour='green', event_hover=True):

    from bokeh.models import Arrow, OpenHead, LabelSet, HoverTool, Segment

    import Football_Pitch_Bokeh as fpbokeh

    line_colour = 'white'
    if pitch_colour == 'white':
        line_colour = 'black'

    if plot is None:
        p = fpbokeh.draw_pitch(hspan=[-53, 53],
                               vspan=[-34, 34],
                               fill_color=pitch_colour,
                               line_color=line_colour)
    else:
        p = plot

    players = p.circle(source=src,
                       x='Start X',
                       y='Start Y',
                       color='red',
                       size=10,
                       alpha=0.7)

    glyph = Segment(x0="Start X",
                    y0="Start Y",
                    x1="End X",
                    y1="End Y",
                    line_color="black",
                    line_width=1)
    p.add_glyph(src, glyph)

    p.add_layout(
        LabelSet(x='Start X',
                 y='Start Y',
                 x_offset=3,
                 y_offset=3,
                 text='Shirt Number',
                 text_font_size='10pt',
                 text_color='red',
                 source=src,
                 level='glyph'))
    if event_hover == True:
        hover = HoverTool()
        hover.mode = 'mouse'
        hover.tooltips = [("Event", "@play_frame"),
                          ("Player", "@{Shirt Number}"), ("Type", "@Type"),
                          ("SubType", "@SubType"),
                          ("(Start X,Start Y)", "($x, $y)")]
        hover.renderers = [players]
        p.add_tools(hover)

    return p
Ejemplo n.º 2
0
         y="y",
         length=45,
         angle=-0.7,
         line_color="#FB8072",
         line_width=2)),
    ("rect",
     Rect(x="x",
          y="y",
          width=screen(10),
          height=screen(20),
          angle=-0.7,
          fill_color="#CAB2D6")),
    ("segment",
     Segment(x0="x",
             y0="y",
             x1="xm01",
             y1="ym01",
             line_color="#F4A582",
             line_width=3)),
    ("text", Text(x="x", y="y", text=["hello"])),
    ("wedge",
     Wedge(x="x",
           y="y",
           radius=screen(15),
           start_angle=0.6,
           end_angle=4.1,
           fill_color="#B3DE69")),
]

markers = [
    ("circle", Circle(x="x", y="y", radius=0.1, fill_color="#3288BD")),
    ("circle_x",
Ejemplo n.º 3
0
    dict(
        x=x,
        y=y,
        xm01=x - x**3 / 10 + 0.3,
        ym01=y - x**2 / 10 + 0.5,
    ))

plot = Plot(title=None,
            plot_width=300,
            plot_height=300,
            min_border=0,
            toolbar_location=None)

glyph = Segment(x0="x",
                y0="y",
                x1="xm01",
                y1="ym01",
                line_color="#f4a582",
                line_width=3)
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))

curdoc().add_root(plot)
Ejemplo n.º 4
0
    _source_point = ColumnDataSource(d_point)
    _source_endog = ColumnDataSource(d_endog)
    _point_filter = IndexFilter(indices=[])
    _view_point = CDSView(source=_source_point, filters=[_point_filter])
    _view_endog = CDSView(source=_source_endog, filters=[endog_filter])
    point_perc_filters.append(_point_filter)
    point_percentile_source.append(_source_point)
    endog_percentile_source.append(_source_endog)
    point_perc_view.append(_view_point)
    endog_perc_view.append(_view_endog)

    point_band = Segment(x0='position',
                         x1='position',
                         y0='low',
                         y1='high',
                         line_color='color',
                         line_width=10,
                         line_alpha=_alphas[i])
    endog_band = Segment(x0=31,
                         x1=31,
                         y0='low',
                         y1='high',
                         line_color='slategrey',
                         line_width=10,
                         line_alpha=_alphas[i])

    loop_freq_ax.add_glyph(_source_point, point_band, view=_view_point)
    loop_freq_ax.add_glyph(_source_endog, endog_band, view=_view_endog)

seq_hover_cb = """
Ejemplo n.º 5
0
source = ColumnDataSource(data=dict(
    pt_lat=pt_lat,
    pt_lon=pt_lon,
    identifier=identifier,
    x0=x0,
    x1=x1,
    y0=y0,
    y1=y1,
    title=title,
    author=author,
    year=year,
    color=color,
))
segment = Segment(x0="x0",
                  y0="y0",
                  x1="x1",
                  y1="y1",
                  line_color="color",
                  line_width=3)
fig.add_glyph(source, segment)

circle = Circle(x="pt_lon", y="pt_lat", size=6, fill_color="color")
nonselection_circle = Circle(x="pt_lon",
                             y="pt_lat",
                             size=6,
                             fill_color="color",
                             fill_alpha=0.8)
fig.add_glyph(source,
              circle,
              selection_glyph=circle,
              nonselection_glyph=nonselection_circle)
Ejemplo n.º 6
0
                                        fill_color="green",
                                        line_color='green',
                                        line_width=3)
cluster_points.nonselection_glyph = Square(fill_alpha=0.1,
                                           fill_color="grey",
                                           line_color=None)
cluster_errors = plot2.segment('mass',
                               'vmax_up_error',
                               'mass',
                               'vmax_down_error',
                               source=shutter_positions,
                               line_width=2,
                               line_color='grey',
                               line_alpha=0.1)
cluster_errors.selection_glyph = Segment(line_alpha=0.5,
                                         line_color='green',
                                         line_width=3)
cluster_errors.nonselection_glyph = Segment(line_alpha=0.1,
                                            line_color='grey',
                                            line_width=1)

# Set up control widgets
aperture = Slider(title="Aperture (meters)",
                  value=12.,
                  start=4.,
                  end=20.0,
                  step=1.0,
                  width=400)
exposure = Slider(title="Exposure Time (hr)",
                  value=1,
                  start=1.0,
Ejemplo n.º 7
0
    def _process_series(self, series):
        self._init_cyclers()
        # clear figure. need to clear both the renderers as well as the
        # colorbars which are added to the right side.
        self._fig.renderers = []
        self._fig.right = []

        for i, s in enumerate(series):
            if s.is_2Dline:
                if s.is_parametric and self._use_cm:
                    x, y, param = s.get_data()
                    colormap = (next(self._cyccm) if self._use_cyclic_cm(
                        param, s.is_complex) else next(self._cm))
                    ds, line, cb = self._create_gradient_line(
                        x, y, param, colormap, s.label)
                    self._fig.add_glyph(ds, line)
                    if self.legend:
                        self._handles[i] = cb
                        self._fig.add_layout(cb, "right")
                else:
                    if s.is_parametric:
                        x, y, param = s.get_data()
                        source = {"xs": x, "ys": y, "us": param}
                    else:
                        x, y = s.get_data()
                        x, y, modified = self._detect_poles(x, y)
                        if modified:
                            self._fig.y_range.start = self.ylim[0]
                            self._fig.y_range.end = self.ylim[1]
                        source = {"xs": x, "ys": y}

                    lkw = dict(line_width=2,
                               legend_label=s.label,
                               color=next(self._cl))
                    line_kw = self._kwargs.get("line_kw", dict())
                    if not s.is_point:
                        self._fig.line("xs",
                                       "ys",
                                       source=source,
                                       **merge({}, lkw, line_kw))
                    else:
                        self._fig.dot("xs",
                                      "ys",
                                      source=source,
                                      **merge({"size": 20}, lkw, line_kw))

            elif s.is_contour and (not s.is_complex):
                x, y, z = s.get_data()
                x, y, zz = [t.flatten() for t in [x, y, z]]
                minx, miny, minz = min(x), min(y), min(zz)
                maxx, maxy, maxz = max(x), max(y), max(zz)

                contour_kw = self._kwargs.get("contour_kw", dict()).copy()
                cm = contour_kw.pop("palette", next(self._cm))

                self._fig.image(
                    image=[z],
                    x=minx,
                    y=miny,
                    dw=abs(maxx - minx),
                    dh=abs(maxy - miny),
                    palette=cm,
                )

                colormapper = LinearColorMapper(palette=cm,
                                                low=minz,
                                                high=maxz)
                cbkw = dict(width=8, title=s.label)
                colorbar = ColorBar(color_mapper=colormapper, **cbkw)
                self._fig.add_layout(colorbar, "right")
                self._handles[i] = colorbar

            elif s.is_implicit:
                points = s.get_data()
                # TODO: add color to the legend
                if len(points) == 2:
                    # interval math plotting
                    x, y, pixels = self._get_pixels(s, points[0])
                    x, y = x.flatten(), y.flatten()
                    cm = ["#00000000", next(self._cl)]
                    self._fig.image(
                        image=[pixels],
                        x=min(x),
                        y=min(y),
                        dw=abs(max(x) - min(x)),
                        dh=abs(max(y) - min(y)),
                        palette=cm,
                        legend_label=s.label,
                    )
                else:
                    x, y, z, ones, plot_type = points
                    if plot_type == "contour":
                        source = get_contour_data(x, y, z)
                        lkw = dict(
                            line_color=next(self._cl),
                            line_width=2,
                            source=source,
                            legend_label=s.label,
                        )
                        line_kw = self._kwargs.get("line_kw", dict())
                        self._fig.multi_line("xs", "ys",
                                             **merge({}, lkw, line_kw))
                    else:
                        cm = ["#00000000", next(self._cl)]
                        self._fig.image(
                            image=[ones],
                            x=min(x),
                            y=min(y),
                            dw=abs(max(x) - min(x)),
                            dh=abs(max(y) - min(y)),
                            palette=cm,
                            legend_label=s.label,
                        )

            elif s.is_2Dvector:
                streamlines = self._kwargs.get("streamlines", False)
                if streamlines:
                    x, y, u, v = s.get_data()
                    sqk = dict(color=next(self._cl),
                               line_width=2,
                               line_alpha=0.8)
                    stream_kw = self._kwargs.get("stream_kw", dict())
                    density = stream_kw.pop("density", 2)
                    xs, ys = compute_streamlines(x[0, :],
                                                 y[:, 0],
                                                 u,
                                                 v,
                                                 density=density)
                    self._fig.multi_line(xs, ys, **merge({}, sqk, stream_kw))
                else:
                    x, y, u, v = s.get_data()
                    quiver_kw = self._kwargs.get("quiver_kw", dict())
                    data, quiver_kw = self._get_quivers_data(
                        x, y, u, v, **quiver_kw)
                    mag = data["magnitude"]

                    color_mapper = LinearColorMapper(palette=next(self._cm),
                                                     low=min(mag),
                                                     high=max(mag))
                    # don't use color map if a scalar field is visible or if
                    # use_cm=False
                    solid = (True if ("scalar" not in self._kwargs.keys()) else
                             (False if
                              ((not self._kwargs["scalar"]) or
                               (self._kwargs["scalar"] is None)) else True))
                    line_color = ({
                        "field": "magnitude",
                        "transform": color_mapper
                    } if ((not solid) and self._use_cm) else next(self._cl))
                    source = ColumnDataSource(data=data)
                    # default quivers options
                    qkw = dict(line_color=line_color,
                               line_width=1,
                               name=s.label)
                    glyph = Segment(x0="x0",
                                    y0="y0",
                                    x1="x1",
                                    y1="y1",
                                    **merge({}, qkw, quiver_kw))
                    self._fig.add_glyph(source, glyph)
                    if isinstance(line_color, dict):
                        colorbar = ColorBar(color_mapper=color_mapper,
                                            width=8,
                                            title=s.label)
                        self._fig.add_layout(colorbar, "right")
                        self._handles[i] = colorbar

            elif s.is_complex and s.is_domain_coloring:
                x, y, magn_angle, img, colors = s.get_data()
                img = self._get_img(img)

                source = ColumnDataSource({
                    "image": [img],
                    "abs": [magn_angle[:, :, 0]],
                    "arg": [magn_angle[:, :, 1]],
                })

                self._fig.image_rgba(
                    source=source,
                    x=x.min(),
                    y=y.min(),
                    dw=x.max() - x.min(),
                    dh=y.max() - y.min(),
                )

                if colors is not None:
                    # chroma/phase-colorbar
                    cm1 = LinearColorMapper(palette=[tuple(c) for c in colors],
                                            low=-self.pi,
                                            high=self.pi)
                    ticks = [-self.pi, -self.pi / 2, 0, self.pi / 2, self.pi]
                    labels = ["-π", "-π / 2", "0", "π / 2", "π"]
                    colorbar1 = ColorBar(
                        color_mapper=cm1,
                        title="Argument",
                        ticker=FixedTicker(ticks=ticks),
                        major_label_overrides={
                            k: v
                            for k, v in zip(ticks, labels)
                        },
                    )
                    self._fig.add_layout(colorbar1, "right")

                if s.coloring == "f":
                    # lightness/magnitude-colorbar
                    cm2 = LinearColorMapper(palette=bp.gray(100),
                                            low=0,
                                            high=1)
                    colorbar2 = ColorBar(
                        color_mapper=cm2,
                        title="Magnitude",
                        ticker=FixedTicker(ticks=[0, 1]),
                        major_label_overrides={
                            0: "0",
                            1: "∞"
                        },
                    )
                    self._fig.add_layout(colorbar2, "right")

            elif s.is_geometry:
                x, y = s.get_data()
                color = next(self._cl)
                pkw = dict(alpha=0.5,
                           line_width=2,
                           line_color=color,
                           fill_color=color)
                patch_kw = self._kwargs.get("patch_kw", dict())
                self._fig.patch(x, y, **merge({}, pkw, patch_kw))

            else:
                raise NotImplementedError("{} is not supported by {}\n".format(
                    type(s),
                    type(self).__name__) + "Bokeh only supports 2D plots.")

        if len(self._fig.legend) > 0:
            self._fig.legend.visible = self.legend
            # interactive legend
            self._fig.legend.click_policy = "hide"
            self._fig.add_layout(self._fig.legend[0], "right")