Ejemplo n.º 1
0
def make_plot(xname, yname, xax=False, yax=False, text=None):
    plot = Plot(
        x_range=xdr, y_range=ydr, data_sources=[source], background_fill="#ffeedd",
        width=250, height=250, border_fill='white', title="", border_symmetry="", min_border=2)
    if xax:
        xaxis = LinearAxis(plot=plot, dimension=0, location="bottom")
    if yax:
        yaxis = LinearAxis(plot=plot, dimension=1, location="left")
    xgrid = Grid(plot=plot, dimension=0)
    ygrid = Grid(plot=plot, dimension=1)
    circle = Circle(x=xname, y=yname, fill_color="color", fill_alpha=0.2, size=4, line_color="color")
    circle_renderer = Glyph(
        data_source = source,
        xdata_range = xdr,
        ydata_range = ydr,
        glyph = circle,
    )
    plot.renderers.append(circle_renderer)
    plot.tools = [pan, zoom]
    if text:
        text = " ".join(text.split('_'))
        text = Text(
            x={'field':'xcenter', 'units':'screen'},
            y={'field':'ycenter', 'units':'screen'},
            text=text, angle=pi/4, text_font_style="bold", text_baseline="top",
            text_color="#ffaaaa", text_alpha=0.5, text_align="center", text_font_size="28pt")
        text_renderer = Glyph(
            data_source=text_source,
            xdata_range = xdr,
            ydata_range = ydr,
            glyph = text,
        )
        plot.data_sources.append(text_source)
        plot.renderers.append(text_renderer)
    return plot
Ejemplo n.º 2
0
def test_Text():
    glyph = Text()
    assert glyph.x == "x"
    assert glyph.y == "y"
    assert glyph.text == "text"
    assert glyph.angle == "angle"
    yield check_text, glyph
    yield check_props, glyph, ["x", "y", "text", "angle"], TEXT
Ejemplo n.º 3
0
def add_gauge(radius, max_value, length, direction, color, major_step, minor_step):
    major_angles, minor_angles = [], []
    major_labels, minor_labels = [], []

    total_angle = start_angle - end_angle

    major_angle_step = float(major_step)/max_value*total_angle
    minor_angle_step = float(minor_step)/max_value*total_angle

    major_angle = 0

    while major_angle <= total_angle:
        major_angles.append(start_angle - major_angle)
        major_angle += major_angle_step

    minor_angle = 0

    while minor_angle <= total_angle:
        minor_angles.append(start_angle - minor_angle)
        minor_angle += minor_angle_step

    major_labels = [ major_step*i for i, _ in enumerate(major_angles) ]
    minor_labels = [ minor_step*i for i, _ in enumerate(minor_angles) ]

    n = major_step/minor_step
    minor_angles = [ x for i, x in enumerate(minor_angles) if i % n != 0 ]
    minor_labels = [ x for i, x in enumerate(minor_labels) if i % n != 0 ]

    glyph = Arc(x=0, y=0, radius=radius, start_angle=start_angle, end_angle=end_angle, direction="clock", line_color=color, line_width=2)
    add_glyph(glyph, global_source)

    rotation = 0 if direction == 1 else -pi

    x, y = zip(*[ polar_to_cartesian(radius, angle) for angle in major_angles ])
    angles = [ angle + rotation for angle in major_angles ]
    source = ColumnDataSource(dict(x=x, y=y, angle=angles))
    plot.data_sources.append(source)

    glyph = Ray(x="x", y="y", length=length, angle="angle", line_color=color, line_width=2)
    add_glyph(glyph, source)

    x, y = zip(*[ polar_to_cartesian(radius, angle) for angle in minor_angles ])
    angles = [ angle + rotation for angle in minor_angles ]
    source = ColumnDataSource(dict(x=x, y=y, angle=angles))
    plot.data_sources.append(source)

    glyph = Ray(x="x", y="y", length=length/2, angle="angle", line_color=color, line_width=1)
    add_glyph(glyph, source)

    x, y = zip(*[ polar_to_cartesian(radius+2*length*direction, angle) for angle in major_angles ])
    text_angles = [ angle - pi/2 for angle in major_angles ]
    source = ColumnDataSource(dict(x=x, y=y, angle=text_angles, text=major_labels))
    plot.data_sources.append(source)

    glyph = Text(x="x", y="y", angle="angle", text="text", text_align="center", text_baseline="middle")
    add_glyph(glyph, source)
Ejemplo n.º 4
0
def make_plot(xname, yname, xax=False, yax=False, text=None):
    plot = Plot(x_range=xdr,
                y_range=ydr,
                background_fill="#efe8e2",
                border_fill='white',
                title="",
                min_border=2,
                border_symmetry=None,
                plot_width=250,
                plot_height=250)

    circle = Circle(x=xname,
                    y=yname,
                    fill_color="color",
                    fill_alpha=0.2,
                    size=4,
                    line_color="color")
    plot.add_glyph(source, circle)

    xticker = BasicTicker()
    if xax:
        xaxis = LinearAxis()
        plot.add_layout(xaxis, 'below')
        xticker = xaxis.ticker
    plot.add_layout(Grid(dimension=0, ticker=xticker))

    yticker = BasicTicker()
    if yax:
        yaxis = LinearAxis()
        plot.add_layout(yaxis, 'left')
        yticker = yaxis.ticker
    plot.add_layout(Grid(dimension=1, ticker=yticker))

    plot.add_tools(PanTool(), WheelZoomTool())

    if text:
        text = " ".join(text.split('_'))
        text = Text(x={
            'field': 'xcenter',
            'units': 'screen'
        },
                    y={
                        'field': 'ycenter',
                        'units': 'screen'
                    },
                    text=[text],
                    angle=pi / 4,
                    text_font_style="bold",
                    text_baseline="top",
                    text_color="#ffaaaa",
                    text_alpha=0.7,
                    text_align="center",
                    text_font_size="28pt")
        plot.add_glyph(text_source, text)

    return plot
Ejemplo n.º 5
0
def make_box_violin_plot(data, maxwidth=0.9):
    """ 
    data: dict[Str -> List[Number]]
    maxwidth: float
        Maximum width of tornado plot within each factor/facet

    Returns the plot object 
    """
    print("Plotting box violin graph")
    plot_width = 500
    plot_height = 350
    df = pd.DataFrame(columns=["group", "width", "height", "texts", "cats"])
    bar_height = 50
    bins = [0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e10]
    # Compute histograms, while keeping track of max Y values and max counts
    for i, (group, vals) in enumerate(data.iteritems()):
        hist, edges = np.histogram(vals, bins)
        df = df.append(pd.DataFrame(dict(
            group = group,
            width = np.log2(hist[1:]),
            height = np.ones(len(hist) - 1),
            texts = ["%d Nodes" % i for i in hist[1:-1]] + ["%d" % hist[-1]],
            cats = [">10^%d" % np.log10(bin) for bin in bins[1:-1]],
            )))
            
    df.replace(-np.inf, 0)

    # Normalize the widths
    df["width"] *= (maxwidth / df["width"].max())
    
    ds = ColumnDataSource(df)

    xdr = FactorRange(factors=sorted(df["group"].unique()))
    ydr = FactorRange(factors=list(df["cats"]))

    plot = Plot(data_sources=[ds], x_range=xdr, y_range=ydr,
                title="Degree Distribution (log scale)",
                plot_width=plot_width, plot_height=plot_height, 
                tools=[])
    yaxis = CategoricalAxis(plot=plot, location="left", axis_label="degree")
    plot.left.append(yaxis)
    
    glyph = Rect(x="group", y="cats", width="width", height="height",
                 fill_color="#3366ff")
    text_glyph = Text(x="group", y="cats", text="texts", text_baseline="middle",
                      text_align="center", angle=0)
    plot.renderers.append(Glyph(data_source=ds, xdata_range=xdr, ydata_range=ydr,
                                 glyph=glyph))
    plot.renderers.append(Glyph(data_source=ds, xdata_range=xdr, ydata_range=ydr,
                                glyph=text_glyph))
    return plot
Ejemplo n.º 6
0
end_angle = -pi / 4

max_kmh = 250
max_mph = max_kmh * 0.621371

major_step, minor_step = 25, 5

add_glyph(Circle(x=0, y=0, radius=1.00, fill_color="white",
                 line_color="black"))
add_glyph(Circle(x=0, y=0, radius=0.05, fill_color="gray", line_color="black"))

add_glyph(
    Text(x=0,
         y=+0.15,
         angle=0,
         text=["km/h"],
         text_color="red",
         text_align="center",
         text_baseline="bottom",
         text_font_style="bold"))
add_glyph(
    Text(x=0,
         y=-0.15,
         angle=0,
         text=["mph"],
         text_color="blue",
         text_align="center",
         text_baseline="top",
         text_font_style="bold"))


def speed_to_angle(speed, units):
Ejemplo n.º 7
0
line1 = Line(x="dates", y="sunrises", line_color="yellow", line_width=2)
line1_glyph = Glyph(data_source=source,
                    xdata_range=xdr,
                    ydata_range=ydr,
                    glyph=line1)
plot.renderers.append(line1_glyph)

line2 = Line(x="dates", y="sunsets", line_color="red", line_width=2)
line2_glyph = Glyph(data_source=source,
                    xdata_range=xdr,
                    ydata_range=ydr,
                    glyph=line2)
plot.renderers.append(line2_glyph)

text = Text(x="dates", y="times", text="texts", angle=0, text_align="center")
text_glyph = Glyph(data_source=text_source,
                   xdata_range=xdr,
                   ydata_range=ydr,
                   glyph=text)
plot.renderers.append(text_glyph)

xformatter = DatetimeTickFormatter(formats=dict(months=["%b %Y"]))
xaxis = DatetimeAxis(plot=plot, dimension=0, formatter=xformatter)
yaxis = DatetimeAxis(plot=plot, dimension=1)
xgrid = Grid(plot=plot, dimension=0, axis=xaxis)
ygrid = Grid(plot=plot, dimension=1, axis=yaxis)

legend = Legend(plot=plot,
                legends={
                    "sunrise": [line1_glyph],
Ejemplo n.º 8
0
 def setUp(self):
     from bokeh.glyphs import Text
     self.test_text = Text()
Ejemplo n.º 9
0
        for angle in text_angle
    ]

    if first and text:
        text.insert(0, '(version)')
        offset = pi / 48
        text_angle.insert(0, text_angle[0] - offset)
        start.insert(0, start[0] - offset)
        end.insert(0, end[0] - offset)
        x, y = polar_to_cartesian(1.25, start, end)
        first = False

    text_source = ColumnDataSource(dict(text=text, x=x, y=y, angle=text_angle))
    glyph = Text(x="x",
                 y="y",
                 text="text",
                 angle="angle",
                 text_align="center",
                 text_baseline="middle")
    plot.add_glyph(text_source, glyph)


def to_base64(png):
    return "data:image/png;base64," + base64.b64encode(png).decode("utf-8")


urls = [to_base64(icons.get(browser, b"")) for browser in browsers]
x, y = polar_to_cartesian(1.7, start_angles, end_angles)

icons_source = ColumnDataSource(dict(urls=urls, x=x, y=y))
glyph = ImageURL(url="urls", x="x", y="y", angle=0.0, anchor="center")
plot.add_glyph(icons_source, glyph)
Ejemplo n.º 10
0
def make_box_violin_plot(data, num_bins, maxwidth=0.9):
    """ 
    data: dict[Str -> List[Number]]
    maxwidth: float
        Maximum width of tornado plot within each factor/facet

    Returns the plot object 
    """

    df = pd.DataFrame(columns=["group", "centers", "width", "height", "texts"])
    bar_height = 50
    bins = [0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6]
    # Compute histograms, while keeping track of max Y values and max counts
    for i, (group, vals) in enumerate(data.iteritems()):
        hist, edges = np.histogram(vals, bins)
        df = df.append(
            pd.DataFrame(
                dict(
                    group=group,
                    centers=np.arange(len(hist)) * bar_height,
                    width=np.log10(hist),
                    height=np.ones(hist.shape) * bar_height,
                    texts=map(str, hist),
                )))

    df.replace(-np.inf, 0)

    # Normalize the widths
    df["width"] *= (maxwidth / df["width"].max())

    ds = ColumnDataSource(df)

    xdr = FactorRange(factors=sorted(df["group"].unique()))
    ydr = DataRange1d(sources=[ds.columns("centers")])

    plot = Plot(data_sources=[ds],
                x_range=xdr,
                y_range=ydr,
                title="Degree Distribution",
                plot_width=750,
                plot_height=600,
                tools=[])
    xaxis = CategoricalAxis(plot=plot,
                            location="bottom",
                            axis_label="number of nodes")
    #yaxis = LogAxis(plot=plot, location="left", axis_label="degree")
    plot.below.append(xaxis)
    #plot.above.append(yaxis)

    #xgrid = Grid(plot=plot, dimension=0, axis=xaxis)
    #ygrid = Grid(plot=plot, dimension=1, axis=yaxis)

    glyph = Rect(x="group", y="centers", width="width", height="height")
    text_glyph = Text(x="group",
                      y="centers",
                      text="texts",
                      text_baseline="middle",
                      text_align="center")
    plot.renderers.append(
        Glyph(data_source=ds, xdata_range=xdr, ydata_range=ydr, glyph=glyph))
    plot.renderers.append(
        Glyph(data_source=ds,
              xdata_range=xdr,
              ydata_range=ydr,
              glyph=text_glyph))

    return plot