示例#1
0
def testPlotKline(ts_code, days=1000):
    """
    绘制K线,pe走势图
    :param ts_code: string, 股票代码, 600619
    :param days: int, 走势图显示的总天数
    :return:
    """
    df = readKlineDf(ts_code, days)
    source = ColumnDataSource(df)
    TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
    width = 1000
    klineHeight = int(width / 16 * 6)
    peHeight = int(width / 16 * 3)
    selectHeight = int(width / 16 * 1)

    # 绘制K线图
    dataLen = df.shape[0]
    tooltips = [('date', '@date'), ('close', '@close')]
    pkline = figure(x_axis_type="datetime", tools=TOOLS,
                    plot_height=klineHeight,
                    plot_width=width,
                    x_axis_location="above",
                    title="kline: %s" % ts_code,
                    tooltips=tooltips,
                    x_range=(dataLen - 200, dataLen - 1))
    pkline.xaxis.major_label_overrides = df['date'].to_dict()
    plotCandlestick(pkline, df)
    print(type(pkline.y_range))
    print(pkline.y_range)

    tooltips = [('pe', '@pe')]
    ppe = figure(x_axis_type="datetime", tools=TOOLS,
                 plot_height=peHeight, plot_width=width,
                 tooltips=tooltips,
                 # x_axis_location=None,
                 # x_axis_location="bottom",
                 x_range=pkline.x_range)
    ppe.xaxis.major_label_overrides = df['date'].to_dict()
    plotPE(ppe, source)

    select = figure(
                    # title="Drag the middle and edges of the selection box to change the range above",
                    plot_height=selectHeight,
                    plot_width=width,
                    # y_range=ppe.y_range,
                    # x_axis_type="datetime",
                    y_axis_type=None,
                    tools="", toolbar_location=None, background_fill_color="#efefef")
    select.xaxis.major_label_overrides = df['date'].to_dict()
    plotPE(select, source)

    range_tool = RangeTool(x_range=pkline.x_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool

    column_layout = column([pkline, ppe, select])
    output_file("../kline.html", title="kline plot test")
    show(column_layout)  # open a browser
def make_plot():
    # prepare the data
    dates = np.array(AAPL['date'], dtype=np.datetime64)
    source = ColumnDataSource(data=dict(date=dates, close=AAPL['adj_close']))

    # prepare the chart
    p = figure(plot_height=300, plot_width=800, tools='xpan', toolbar_location=None,
               x_axis_type='datetime', x_axis_location='above',
               background_fill_color="#efefef", x_range=(dates[1500], dates[2500]))

    p.line('date', 'close', source=source)
    p.yaxis.axis_label = 'Price'

    # get the selection/dragger
    select = figure(title="Drag the middle and edges of the selection box to change the range above",
                    plot_height=130, plot_width=800, y_range=p.y_range,
                    x_axis_type="datetime", y_axis_type=None,
                    tools="", toolbar_location=None, background_fill_color="#efefef")

    range_tool = RangeTool(x_range=p.x_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2

    select.line('date', 'close', source=source)
    select.ygrid.grid_line_color = None
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool

    return column(p, select)
示例#3
0
    def init_componets(self):
        ''' 
            init of components, must be done in a seperate function.

            On load of page, a standard document is offerd to the renderer function,
            the components created in this function are
        '''

        dates = [datetime(2017, 1, 1), datetime(2018, 1, 1)]

        self.main_vw = figure(name='main_view',
                              sizing_mode='scale_width',
                              plot_height=100,
                              plot_width=500,
                              x_axis_type="datetime",
                              x_range=(dates[0], dates[1]))

        self.select_vw = figure(name='select_view',
                                sizing_mode='scale_width',
                                plot_height=100,
                                plot_width=500,
                                x_axis_type="datetime",
                                y_axis_type=None,
                                tools="",
                                toolbar_location=None)

        range_rool = RangeTool(x_range=self.main_vw.x_range)
        range_rool.overlay.fill_color = "navy"
        range_rool.overlay.fill_alpha = 0.2

        self.select_vw.ygrid.grid_line_color = None
        self.select_vw.add_tools(range_rool)
        self.select_vw.toolbar.active_multi = range_rool
示例#4
0
def make_plot(name, wave_list, spec_list, color_list, label_list, image,
              sx, sy):
    plot = figure(plot_height=300, plot_width=800,
               toolbar_location=None, x_axis_location="above",
               background_fill_color="#efefef", x_range=(3470., 5540.),
               y_axis_type="linear")

    imageplot = figure(plot_height=540, plot_width=430,
                       tools="crosshair, pan, reset, save, wheel_zoom",
                       y_axis_location="right",
                       tooltips=[("x", "$x"), ("y", "$y"),
                                 ("value", "@image")],
                       x_range=(image[1].min(), image[1].max()),
                       y_range=(image[2].min(), image[2].max()))

    select = figure(title=("Drag the selection "
                           "box to change the range above"),
                    plot_height=240, plot_width=800, y_range=plot.y_range,
                    y_axis_type="linear",
                    tools="", toolbar_location=None,
                    background_fill_color="#efefef")
    for wavelength, spectrum, color, label in zip(wave_list, spec_list, 
                                                  color_list, label_list):               
        source = ColumnDataSource(data=dict(wavelength=wavelength, 
                                            spectrum=spectrum))
        plot.line('wavelength', 'spectrum', source=source, line_width=3,
                  line_alpha=0.6, line_color=color,
                  legend=label)
        select.line('wavelength', 'spectrum', source=source,
                    line_color=color)
    plot.yaxis.axis_label = '1e-17 ergs/s/cm^2/A'
    for p in [plot, select]:
        p.xaxis.major_label_text_font_size = "14pt"
        p.yaxis.major_label_text_font_size = "14pt"
        p.xaxis.axis_label = 'Wavelength'
        p.xaxis.axis_label_text_font_size = "16pt"
        p.yaxis.axis_label_text_font_size = "16pt"
        p.xaxis.major_tick_line_color = "firebrick"
        p.xaxis.major_tick_line_width = 3
        p.xaxis.minor_tick_line_color = "orange"
        p.yaxis.major_tick_line_color = "firebrick"
        p.yaxis.major_tick_line_width = 3
        p.yaxis.minor_tick_line_color = "orange"
        p.yaxis[0].formatter = PrintfTickFormatter(format="%3.1f")
    select.ygrid.grid_line_color = None
    range_tool = RangeTool(x_range=plot.x_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool
    imageplot.image(image=[image[0]], x=image[1].min(), y=image[2].min(),
                    dw=image[1].max()-image[1].min(),
                    dh=image[2].max()-image[2].min())
    imageplot.scatter(sx, sy, marker='circle_x', size=15,
              line_color="orange", fill_color="red", alpha=0.75)
    output_file(name+".html", title=name)
    save(row(column(plot, select), imageplot))
示例#5
0
def plotIndexPE():
    """
    待删除
    :return:
    """
    sql = 'select date, pe from pehistory where name="all"'
    df = pd.read_sql(sql, engine)
    print(df)
    source = ColumnDataSource(df)
    TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
    width = 1000
    peHeight = int(width / 16 * 3)
    selectHeight = int(width / 16 * 1)

    # # 绘制K线图
    # tooltips = [('date', '@date'), ('close', '@close')]
    # pkline = figure(x_axis_type="datetime", tools=TOOLS,
    #                 plot_height=klineHeight,
    #                 plot_width=width,
    #                 x_axis_location="above",
    #                 title="kline: %s" % ts_code,
    #                 tooltips=tooltips,
    # pkline.xaxis.major_label_overrides = df['date'].to_dict()
    # plotCandlestick(pkline, df)
    # print(type(pkline.y_range))
    # print(pkline.y_range)

    dataLen = df.shape[0]
    tooltips = [('pe', '@pe')]
    ppe = figure(x_axis_type="datetime", tools=TOOLS,
                 plot_height=peHeight, plot_width=width,
                 # x_axis_location=None,
                 # x_axis_location="bottom",
                 x_range=(dataLen - 200, dataLen - 1),
                 tooltips=tooltips)
    # ppe.xaxis.major_label_overrides = df['date'].to_dict()
    plotPE(ppe, source)

    select = figure(plot_height=selectHeight,
                    plot_width=width,
                    # y_range=ppe.y_range,
                    # x_axis_type="datetime",
                    y_axis_type=None,
                    tools="", toolbar_location=None, background_fill_color="#efefef")
    # select.xaxis.major_label_overrides = df['date'].to_dict()
    plotPE(select, source)

    range_tool = RangeTool(x_range=ppe.x_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool

    column_layout = column([ppe, select])
    output_file("pe.html", title="index pe plot test")
    show(column_layout)  # open a browser
示例#6
0
def uploaded_file(filename):
    filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)

    def search_string_in_file(file_name, string_to_search):
        line_number = 0
        list_of_results = []
        with open(file_name, 'r') as read_obj:
            for line in read_obj:
                line_number += 1
                if string_to_search in line:
                    list_of_results.append((line_number, line.rstrip()))
        return list_of_results

    matched_lines = search_string_in_file(filepath, '[Data]')
    elem = ()
    for elem in matched_lines:
        break

    df = pd.read_csv(filepath, skipinitialspace=True, skiprows=elem[0])
    df.dropna(axis=1, inplace=True)
    source = ColumnDataSource(df)

    p = figure(plot_height=700, plot_width=1000, toolbar_location="right", tools="save,xwheel_zoom,"
                                                                                 ",reset,crosshair",
               x_axis_type="linear", x_axis_location="below",
               background_fill_color=None, x_range=(df['Angle'].iloc[0], df['Angle'].iloc[-1]))

    p.add_tools(BoxSelectTool(dimensions='width'))
    p.add_tools(CustomAction(icon="/home/cleber/Documentos/GitHub/fitdrx/site2/static/icon.png",
                             callback=CustomJS(code='alert("foo")')))
    legend = filename[0:-4]

    p.scatter('Angle', 'Det1Disc1', source=source, legend_label=legend, color='red', size=0.2, alpha=0)
    p.line('Angle', 'Det1Disc1', source=source, legend_label=legend, line_color='#0020C2')
    p.yaxis.axis_label = 'I (U.A.)'
    p.xaxis.axis_label = '2-theta (degree)'
    # button = Button(label='FIT', aspect_ratio=10, background='red',
    #                 align='center', button_type='primary',
    #                 width_policy='max')

    select = figure(title='Selecionar pico',
                    plot_height=110, plot_width=1030, y_range=p.y_range,
                    x_axis_type="linear", y_axis_type=None,
                    tools="", toolbar_location=None, background_fill_color="#efefef")
    range_tool = RangeTool(x_range=p.x_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2
    select.line('Angle', 'Det1Disc1', source=df)
    select.ygrid.grid_line_color = None
    select.xgrid.grid_line_color = None
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool

    script, divs = components((p, select))

    return render_template('page2.html', script=script, div0=divs[0], div1=divs[1])
示例#7
0
def get_select_RangeTool(p,
                         x_col,
                         y_cols,
                         source,
                         palette=viridis(1),
                         output_backend='canvas',
                         x_axis_type='datetime'):
    """Return a new figure that uses the RangeTool to control the figure p.

    Args:
        p (figure): Bokeh figure to control with RangeTool.

        x_col (str): Name of column for x axis.

        y_cols (list): Names of columns for y axis.

        source (ColumnDataSource): Bokeh's data.

        palette (list, optional): Color palette. Defaults to viridis(1).

        output_backend (str, optional): Bokeh's rendering backend. Defaults
        to 'canvas'.

        x_axis_type (str, optional): Type of x axis, 'datetime' or 'linear'.

    Returns:
        select (TYPE): DESCRIPTION.

    """
    select = figure(
        plot_height=45,
        y_range=p.y_range,
        tools="",
        x_axis_type=x_axis_type,
        y_axis_type=None,
        toolbar_location=None,
        background_fill_color="#efefef",
        output_backend=output_backend,
        height_policy="fixed",
        width_policy="fit",
    )
    for y_col in y_cols:  # Show all lines of primary figure in "select", too
        select.line(x_col, y_col, source=source)

    # Create a RangeTool, that will be applied to the "select" figure
    range_tool = RangeTool(x_range=p.x_range)  # Link figure and RangeTool
    range_tool.overlay.fill_color = palette[0]
    range_tool.overlay.fill_alpha = 0.25

    select.ygrid.grid_line_color = None
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool
    return select
示例#8
0
def _make_plot():
    source = ColumnDataSource(dict(x=[1, 2], y=[1, 1]))
    r = Range1d(start=0.4, end=0.6)
    plot = Plot(height=400, width=1100, x_range=Range1d(0, 1), y_range=Range1d(0, 1), min_border=0)
    plot.add_glyph(source, Rect(x='x', y='y', width=0.9, height=0.9))
    tool = RangeTool(x_range=r)
    plot.add_tools(tool)
    plot.min_border_right = 100
    code = RECORD("start", "t.x_range.start", final=False) + RECORD("end", "t.x_range.end")
    plot.tags.append(CustomJS(name="custom-action", args=dict(t=tool), code=code))
    plot.toolbar_sticky = False
    return plot
示例#9
0
def _create_range_tool(
    data,
    min_time,
    max_time,
    plot_range,
    width,
    height,
    time_column: str = None,
    y: str = "y_index",
):
    """Create plot bar to act as as range selector."""
    ext_min = min_time - ((max_time - min_time) * 0.15)
    ext_max = max_time + ((max_time - min_time) * 0.15)
    plot_height = max(120, int(height * 0.20))
    rng_select = figure(
        x_range=(ext_min, ext_max),
        title="Range Selector",
        plot_height=plot_height,
        plot_width=width,
        x_axis_type="datetime",
        y_axis_type=None,
        tools="",
        toolbar_location=None,
    )
    help_str = (
        "Drag the middle or edges of the selection box to change "
        + "the range in the main chart"
    )
    rng_select.add_layout(
        Title(text=help_str, align="right", text_font_size="10px"), "below"
    )
    rng_select.xaxis[0].formatter = _get_tick_formatter()
    if isinstance(data, dict):
        for _, series_def in data.items():
            rng_select.circle(
                x=series_def["time_column"],
                y=y,
                color=series_def["color"],
                source=series_def["source"],
            )
    elif isinstance(data, pd.DataFrame):
        rng_select.circle(
            x=time_column, y=y, color="blue", source=ColumnDataSource(data)
        )

    range_tool = RangeTool(x_range=plot_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2
    rng_select.ygrid.grid_line_color = None
    rng_select.add_tools(range_tool)
    rng_select.toolbar.active_multi = range_tool
    return rng_select
def build_normalized_price_fig(data):
    # ========== themes & appearance ============= #
    STK_1_LINE_COLOR = "#053061"
    STK_2_LINE_COLOR = "#67001f"
    STK_1_LINE_WIDTH = 1.5
    STK_2_LINE_WIDTH = 1.5
    WINDOW_SIZE = 10
    TITLE = "PRICE OF X vs Y" 
    HEIGHT = 250
    SLIDER_HEIGHT = 150
    WIDTH = 600

    # ========== data ============= #
    # use sample data from ib-data folder
    dates = np.array(data['date'], dtype=np.datetime64)
    STK_1_source = ColumnDataSource(data=dict(date=dates, close=data['data0']))
    STK_2_source = ColumnDataSource(data=dict(date=dates, close=data['data1']))

    # ========== plot data points ============= #
    # x_range is the zoom in slider setup. Pls ensure both STK_1 and STK_2 have same length, else some issue
    normp = figure(plot_height=HEIGHT, 
                   plot_width=WIDTH, 
                   x_range=(dates[-WINDOW_SIZE], dates[-1]), 
                   title=TITLE, 
                   toolbar_location=None)

    normp.line('date', 'close', source=STK_1_source, line_color = STK_1_LINE_COLOR, line_width = STK_1_LINE_WIDTH)
    normp.line('date', 'close', source=STK_2_source, line_color = STK_2_LINE_COLOR, line_width = STK_2_LINE_WIDTH)
    normp.yaxis.axis_label = 'Price'

    normp.xaxis[0].formatter = DatetimeTickFormatter()


    # ========== RANGE SELECT TOOL ============= #

    select = figure(title="Drag the middle and edges of the selection box to change the range above",
                    plot_height=SLIDER_HEIGHT, plot_width=WIDTH, y_range=normp.y_range,
                    x_axis_type="datetime", y_axis_type=None,
                    tools="", toolbar_location=None, background_fill_color="#efefef")

    range_tool = RangeTool(x_range=normp.x_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2

    select.line('date', 'close', source=STK_1_source, line_color = STK_1_LINE_COLOR, line_width = STK_1_LINE_WIDTH)
    select.line('date', 'close', source=STK_2_source, line_color = STK_2_LINE_COLOR, line_width = STK_2_LINE_WIDTH)
    select.ygrid.grid_line_color = None
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool

    return column(normp, select)
示例#11
0
def plot1():
    sim_charges_df = pd.read_csv(
        "/Users/brendanpolidori/Desktop/project_repos/inter_webapp.nosync/e3f2s/e3f2s/webapp/apis/api_test_bokeh/sim_booking_requests_test_bre.csv"
    )

    dates = np.array(sim_charges_df["start_time"], dtype=np.datetime64)
    source = ColumnDataSource(data=dict(
        date=np.array(sim_charges_df["start_time"], dtype=np.datetime64),
        close=list(sim_charges_df["n_vehicles_available"])))

    p = figure(plot_height=300,
               plot_width=800,
               tools="xpan",
               toolbar_location=None,
               x_axis_type="datetime",
               x_axis_location="above",
               background_fill_color="#efefef",
               x_range=(dates[1500], dates[2500]))

    p.line('date', 'close', source=source)
    p.yaxis.axis_label = 'Number Vehicles Available'

    select = figure(
        title=
        "Drag the middle and edges of the selection box to change the range above",
        plot_height=130,
        plot_width=800,
        y_range=p.y_range,
        x_axis_type="datetime",
        y_axis_type=None,
        tools="",
        toolbar_location=None,
        background_fill_color="#efefef")

    range_tool = RangeTool(x_range=p.x_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2

    select.line('date', 'close', source=source)
    select.ygrid.grid_line_color = None
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool

    show(column(p, select))

    return json.dumps(json_item(p, "myplot"))
示例#12
0
def plot(source):
    fig = setup_figure()

    # plot data
    rect = Rect(x='dates',
                y='y',
                width='w',
                height='h',
                line_width=0,
                fill_color='colors',
                fill_alpha=1)
    fig.add_glyph(source, rect)

    line = Line(x='dates', y='avg_to_bed_times')
    fig.add_glyph(source, line)
    # circ = Circle(x='dates', y='avg_to_bed_times', size=3)
    # fig.add_glyph(source, circ)
    line = Line(x='dates', y='avg_get_up_times')
    fig.add_glyph(source, line)
    # circ = Circle(x='dates', y='avg_get_up_times', size=3)
    # fig.add_glyph(source, circ)

    # plot select
    select = figure(
        # background_fill_color='#333333'
        title="",
        plot_width=PLOT_WIDTH,
        plot_height=80,
        toolbar_location=None,  # tools="xwheel_pan",
        x_axis_type="datetime",  # y_axis_type=None,
        x_axis_location=None,
        y_axis_location=None,
        y_range=fig.y_range,
    )
    range_tool = RangeTool(x_range=fig.x_range)
    select.add_tools(range_tool)

    select.line(x=[dt(2013, 5, 1), dt.now()], y=[0, 0], color=None)
    select.add_glyph(source, rect)
    select.ygrid.grid_line_color = None
    select.toolbar.active_multi = range_tool
    select.yaxis.ticker = []

    return fig, select
def bohek_slider(df, col, title=None):

    titledwd = 'Official Outdoor Observations Braunschweig (City Center) Germany'

    dates = df.index.values
    source = ColumnDataSource(data=dict(date=dates, close=df[col]))

    p = figure(title=col + ":" + title,
               plot_height=300,
               plot_width=800,
               tools="",
               toolbar_location=None,
               x_axis_type="datetime",
               x_axis_location="above",
               background_fill_color="#efefef",
               x_range=(dates[0], dates[df[col].shape[0] - 1]))

    p.line('date', 'close', source=source)
    p.yaxis.axis_label = 'Celcius'

    select = figure(
        title=
        "Drag the middle and edges of the selection box to change the range above",
        plot_height=130,
        plot_width=800,
        y_range=p.y_range,
        x_axis_type="datetime",
        y_axis_type=None,
        tools="",
        toolbar_location=None,
        background_fill_color="#efefef")

    range_tool = RangeTool(x_range=p.x_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2

    select.line('date', 'close', source=source)
    select.ygrid.grid_line_color = None
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool

    return column(p, select)
示例#14
0
def _create_vert_range_tool(data,
                            min_y,
                            max_y,
                            plot_range,
                            width,
                            height,
                            x_col,
                            y_col,
                            fill_map="navy"):
    """Return vertical range too for plot."""
    rng_select = figure(
        plot_width=width,
        plot_height=height,
        y_range=(min_y - 1, max_y + 1),
        toolbar_location=None,
    )

    x_dodge = dodge(x_col, -0.5)
    rng_select.rect(
        x=x_dodge,
        y=y_col,
        width=1.2,
        height=0.8,
        source=data,
        fill_alpha=0.6,
        fill_color=fill_map,
    )

    rng_select.xaxis.visible = False
    rng_select.yaxis.visible = False

    range_tool = RangeTool(y_range=plot_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2
    rng_select.ygrid.grid_line_color = None
    rng_select.xgrid.grid_line_color = None
    rng_select.add_tools(range_tool)
    rng_select.toolbar.active_multi = range_tool
    return rng_select
示例#15
0
def build_range_tool_graph(stock_data):
    source = ColumnDataSource(data=dict(date=stock_data.time, close=stock_data.close))

    plot = figure(
        plot_height=300,
        plot_width=950,
        sizing_mode='scale_width',
        tools="xpan",
        toolbar_location=None,
        x_axis_type="datetime",
        x_axis_location="above",
        background_fill_color="white",                                                                    # color here
        x_range=(stock_data.time[280], stock_data.time[355])
    )
    plot.sizing_mode = "scale_both"
    plot.line('date', 'close', source=source)
    plot.yaxis.axis_label = 'Price'
    select = figure(
        # title="Drag the middle and edges of the selection box to change the range above",
        plot_height=100,
        # plot_width=1000,
        sizing_mode='scale_width',
        y_range=plot.y_range,
        x_axis_type="datetime",
        y_axis_type=None,
        tools="",
        toolbar_location=None,
        background_fill_color="white"                                                                     # color here
    )
    select.sizing_mode = "scale_both"
    range_tool = RangeTool(x_range=plot.x_range)
    range_tool.overlay.fill_color = "white"                                                             # color here
    range_tool.overlay.fill_alpha = 0.1  # transparent

    select.line('date', 'close', source=source)
    select.ygrid.grid_line_color = None
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool
    return components(column(plot, select))
示例#16
0
def bokeh_plot(df):
    dates = df['Date']
    source = ColumnDataSource(
        data={
            'date': dates,
            'close': df['Adj_Close'],
            'high': df['High'],
            'low': df['Low'],
            'volume': df['Adj_Volume']
        })

    p = figure(title="Drag to change range",
               plot_height=300,
               plot_width=700,
               tools="xpan",
               toolbar_location=None,
               x_axis_type="datetime",
               x_axis_location="below",
               sizing_mode="scale_width",
               background_fill_color="#f5f5f5",
               x_range=(dates[len(dates) - 300], dates[len(dates) - 1]))

    hover_tool = HoverTool(
        tooltips=[
            ('Date', '@date{%F}'),  # use @{ } for field names with spaces
            ('Close', '$@close{%0.2f}'),
            ('High', '$@high{%0.2f}'),
            ('Low', '$@low{%0.2f}'),
            ('Volume', '@volume{0.00 a}'),
        ],
        formatters={
            'date': 'datetime',  # use 'datetime' formatter for 'date' field
            'close': 'printf',  # use 'printf' formatter for 'adj close' field
            'high': 'printf',
            'low': 'printf'
            # use default 'numeral' formatter for other fields
        },

        # display a tooltip whenever the cursor is vertically in line with a glyph
        mode='vline')
    # cross_tool = CrosshairTool()

    p.add_tools(hover_tool)

    p.line('date', 'close', source=source)
    p.yaxis.axis_label = 'Price'

    select = figure(title="Drag to change the range",
                    plot_height=130,
                    plot_width=930,
                    y_range=p.y_range,
                    x_axis_type="datetime",
                    y_axis_type=None,
                    sizing_mode="scale_width",
                    tools="",
                    toolbar_location=None,
                    background_fill_color="#f5f5f5")

    range_tool = RangeTool(x_range=p.x_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2

    select.line('date', 'close', source=source)
    select.ygrid.grid_line_color = "white"
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool

    c = column(p, select)
    script, div = components(p)

    return script, div
示例#17
0
    def __init__(self, days=1000):
        sql = 'select date, pe from pehistory where name="all"'
        self.df = pd.read_sql(sql, engine)
        self.source = ColumnDataSource(self.df)

        TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
        width = 1000
        peHeight = int(width / 16 * 3)
        selectHeight = int(width / 16 * 1)

        # 绘制K线图
        dataLen = self.df.shape[0]
        # tooltips = [('index', '@index'), ('date', '@date'), ('close', '@close')]
        # ymin = self.df.low[-200:].min()
        # ymax = self.df.high[-200:].max()
        # start = ymin - (ymax - ymin) * 0.05
        # end = ymax + (ymax - ymin) * 0.05
        # self.pkline = figure(x_axis_type="datetime", tools=TOOLS,
        #                      plot_height=klineHeight,
        #                      plot_width=width,
        #                      x_axis_location="above",
        #                      x_range=(dataLen - 200, dataLen - 1),
        #                      y_range=(start, end),
        #                      tooltips=tooltips)
        # self.pkline.xaxis.major_label_overrides = self.df['date'].to_dict()
        # self.plotCandlestick()

        tooltips = [('pe', '@pe')]
        ymin = self.df.pe[-200:].min()
        ymax = self.df.pe[-200:].max()
        start = ymin - (ymax - ymin) * 0.05
        end = ymax + (ymax - ymin) * 0.05
        self.ppe = figure(x_axis_type="datetime", tools=TOOLS,
                          plot_height=peHeight, plot_width=width,
                          tooltips=tooltips,
                          x_range=(dataLen - 200, dataLen - 1),
                          # x_range=self.pkline.x_range,
                          y_range=(start, end))
        self.ppe.xaxis.major_label_overrides = self.df['date'].to_dict()
        self.plotPE(self.ppe)

        self.select = figure(plot_height=selectHeight,
                             plot_width=width,
                             x_range=(0, days - 1),
                             y_axis_type=None,
                             tools="",
                             toolbar_location=None,
                             background_fill_color="#efefef")
        self.select.xaxis.major_label_overrides = self.df['date'].to_dict()
        self.plotPE(self.select)

        range_tool = RangeTool(x_range=self.ppe.x_range)
        range_tool.overlay.fill_color = "navy"
        range_tool.overlay.fill_alpha = 0.2
        self.select.add_tools(range_tool)
        self.select.toolbar.active_multi = range_tool

        # kline和pe显示范围变动时自动更新y轴范围
        code = """
                var xstart = parseInt(ppe.x_range.start);
                if(xstart<0){xstart=0;}
                var xend = parseInt(ppe.x_range.end);
                if(xend>maxdays - 1){xend=maxdays - 1;}
                console.log('xstart: ', xstart);
                console.log('xend: ', xend);
                console.log('maxdays: ', maxdays);
                
                var data = source.data;
                var highdata = data['high'];
                var lowdata = data['low'];
                var pedata = data['pe'];
                var peymax = pedata[xstart];
                var peymin = pedata[xstart];
                
                for (var i = xstart + 1; i < xend; i++) {
                    peymax =  Math.max(peymax, pedata[i]);
                    peymin =  Math.min(peymin, pedata[i]);
                    // console.log('pedata[i]: ', pedata[i]);
                    // console.log('i:', i);
                }
                
                ppe.y_range.start = peymin - (peymax - peymin) * 0.05;
                ppe.y_range.end = peymax + (peymax - peymin) * 0.05;
                console.log('peymax: ', peymax);
                console.log('peymin: ', peymin);
                """
        # code = """
        #         ppe.y_range.end = 30
        #        """
        callback = CustomJS(args=dict(ppe=self.ppe,
                                      source=self.source,
                                      maxdays=days),
                            code=code)
        self.ppe.x_range.js_on_change('start', callback)
        self.ppe.x_range.js_on_change('end', callback)

        self.column_layout = column([self.ppe, self.select])
示例#18
0
    def __init__(self, ID, df):
        df['date'] = [i.strftime('%Y%m%d') for i in df.date]
        self.df = df
        days = df.shape[0]
        self.source = ColumnDataSource(self.df)

        TOOLS = 'pan,wheel_zoom,box_zoom,reset,save,crosshair'
        width = 1000
        klineHeight = int(width / 16 * 6)
        peHeight = int(width / 16 * 3)
        selectHeight = int(width / 16 * 1)

        # 绘制K线图
        dataLen = self.df.shape[0]
        tooltips = [('date', '@date'), ('close', '@close')]
        ymin = self.df.low[-200:].min()
        ymax = self.df.high[-200:].max()
        start = ymin - (ymax - ymin) * 0.05
        end = ymax + (ymax - ymin) * 0.05
        self.pkline = figure(x_axis_type="datetime", tools=TOOLS,
                             plot_height=klineHeight,
                             plot_width=width,
                             x_axis_location="above",
                             x_range=(dataLen - 200, dataLen),
                             y_range=(start, end),
                             tooltips=tooltips)
        self.pkline.xaxis.major_label_overrides = self.df['date'].to_dict()
        self.plotCandlestick()

        tooltips = [('date', '@date'), ('pe', '@pe')]
        ymin = self.df.pe[-200:].min()
        ymax = self.df.pe[-200:].max()
        start = ymin - (ymax - ymin) * 0.05
        end = ymax + (ymax - ymin) * 0.05
        self.ppe = figure(x_axis_type="datetime", tools=TOOLS,
                          plot_height=peHeight, plot_width=width,
                          tooltips=tooltips,
                          x_range=self.pkline.x_range,
                          y_range=(start, end))
        self.ppe.xaxis.major_label_overrides = self.df['date'].to_dict()
        self.plotPE(self.ppe)

        self.select = figure(plot_height=selectHeight,
                             plot_width=width,
                             x_range=(-2, days + 2),
                             y_axis_type=None,
                             tools="",
                             toolbar_location=None,
                             background_fill_color="#efefef")
        self.select.xaxis.major_label_overrides = self.df['date'].to_dict()
        self.plotPE(self.select)

        range_tool = RangeTool(x_range=self.pkline.x_range)
        range_tool.overlay.fill_color = "navy"
        range_tool.overlay.fill_alpha = 0.2
        self.select.add_tools(range_tool)
        self.select.toolbar.active_multi = range_tool

        # kline和pe显示范围变动时自动更新y轴范围
        code = """
                var xstart = parseInt(ppe.x_range.start);
                if(xstart<0){xstart=0;}
                var xend = parseInt(ppe.x_range.end);
                if(xend>maxdays){xend=maxdays;}
                console.log('xstart: ', xstart);
                console.log('xend: ', xend);
                console.log('maxdays: ', maxdays);
                
                var data = source.data;
                var highdata = data['high'];
                var lowdata = data['low'];
                var klineymax = highdata[xstart];
                var klineymin = lowdata[xstart];
                var pedata = data['pe'];
                var peymax = pedata[xstart];
                var peymin = pedata[xstart];
                
                for (var i = xstart + 1; i <= xend; i++) {
                    klineymax =  Math.max(klineymax, highdata[i]);
                    klineymin =  Math.min(klineymin, lowdata[i]);
                    peymax =  Math.max(peymax, pedata[i]);
                    peymin =  Math.min(peymin, pedata[i]);
                    // console.log('pedata[i]: ', pedata[i]);
                    // console.log('i:', i);
                }
                
                pkline.y_range.start = klineymin - (klineymax - klineymin) * 0.05;
                pkline.y_range.end = klineymax + (klineymax - klineymin) * 0.05;
                ppe.y_range.start = peymin - (peymax - peymin) * 0.05;
                ppe.y_range.end = peymax + (peymax - peymin) * 0.05;
                console.log('klineymax: ', klineymax);
                console.log('klineymin: ', klineymin);
                console.log('peymax: ', peymax);
                console.log('peymin: ', peymin);
                """
        # code = """
        #         ppe.y_range.end = 30
        #        """
        callback = CustomJS(args=dict(ppe=self.ppe,
                                      pkline=self.pkline,
                                      source=self.source,
                                      maxdays=days - 1),
                            code=code)
        self.ppe.x_range.js_on_change('start', callback)
        self.ppe.x_range.js_on_change('end', callback)

        self.column_layout = column([self.pkline, self.ppe, self.select])
示例#19
0
# plot the globally averaged AOD
# ------------------------------------
p2 = figure(plot_height=300,
            plot_width=900,
            x_axis_type='datetime',
            y_axis_type='log',
            y_range=(1e-4, 1e-1),
            x_range=p.x_range)
a0 = p2.line(x='time', y='aod', source=aod_df, color='gray')
a1 = p2.line(x='time', y='aod_diff', source=aod_diff_df, line_width=1)
p2.yaxis.axis_label = 'Global AOD [1020nm]'

# ------------------------------------
# Add the range tool for selection
# ------------------------------------
range_tool = RangeTool(
    x_range=Range1d(np.datetime64('1991-05-01'), np.datetime64('1996-06-01')))
range_tool.overlay.fill_color = "gray"
range_tool.overlay.fill_alpha = 0.1

range_tool.x_range.on_change('start', vci_callback)
range_tool.x_range.on_change('end', vci_callback)

p2.ygrid.grid_line_color = None
p2.add_tools(range_tool)
p.add_tools(range_tool)
p2.toolbar.active_multi = range_tool

aod_band_diff2 = calc_aod_in_period()
glyph = Patch(x="time",
              y="aod_fill",
              fill_color="#f46842",
示例#20
0
def make_plot(alarm, warning_msg, label_Tx=None):

    output_file("static/bokeh.html", title="Seizure Alarm")

    # define what to plot
    alarm_x = np.arange(alarm.shape[0])
    source = ColumnDataSource(data=dict(alarm_x=alarm_x, alarm=alarm))

    if label_Tx is not None:
        shift = label_Tx.shape[0] - alarm.shape[0]
        alarm = np.pad(alarm, (shift, 0), mode="constant", constant_values=0)
        label_Tx_x = np.arange(label_Tx.shape[0])
        source = ColumnDataSource(data=dict(
            alarm_x=label_Tx_x,
            alarm=alarm,
            label_Tx_x=label_Tx_x,
            label_Tx=label_Tx,
        ))

    # make an upper figure in the column object
    start = 1000
    end = 1060
    if alarm_x.shape[0] < 1060:
        start = 0
        end = 60
    p = figure(
        title=warning_msg,
        plot_height=400,
        plot_width=800,
        tools="xpan",
        toolbar_location=None,
        x_axis_location="above",
        background_fill_color="#efefef",
        x_range=(alarm_x[start], alarm_x[end]),
    )

    # draw alarm in upper figure
    p.line(
        "alarm_x",
        "alarm",
        legend_label="Alarm",
        line_width=3,
        color="red",
        source=source,
    )

    # make a lower figure in the column object
    select = figure(
        plot_height=200,
        plot_width=800,
        y_range=p.y_range,
        y_axis_type=None,
        tools="",
        toolbar_location=None,
        background_fill_color="#efefef",
    )

    # plot alarm in lower figure
    select.line("alarm_x", "alarm", color="red", source=source)

    if label_Tx is not None:
        # draw label in the upper figure
        p.line(
            "label_Tx_x",
            "label_Tx",
            legend_label="clinician label",
            line_width=3,
            color="orange",
            line_dash="4 4",
            source=source,
        )
        # draw label in the lower figure
        select.line("label_Tx_x",
                    "label_Tx",
                    color="orange",
                    line_dash="4 4",
                    source=source)

    # cosmetic changes
    for panel in [p, select]:
        panel.yaxis.ticker = [0, 1]
        panel.yaxis.major_label_overrides = {0: "OFF", 1: "ON"}
    p.yaxis.axis_label = "Seizure"
    p.xaxis.axis_label = "Time (seconds)"
    p.title.text_font_size = "20pt"
    select.ygrid.grid_line_color = None

    # create a range tool
    range_tool = RangeTool(x_range=p.x_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2

    # add range tool to lower figure
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool

    # save column object to html defined by output_file
    save(column(p, select))
示例#21
0
##########################################
select = figure(
    title=
    "Drag the middle and edges of the selection box to change the range of plots",
    plot_height=130,
    plot_width=920,
    y_range=temp_plot.y_range,
    x_axis_type="datetime",
    y_axis_type=None,
    tools="",
    toolbar_location=None,
    background_fill_color="#efefef",
    height_policy="fixed",
    min_width=920)

range_tool = RangeTool(x_range=temp_plot.x_range)
range_tool.overlay.fill_color = "navy"
range_tool.overlay.fill_alpha = 0.2

select.line('date', 'kitchen_temp', source=source_static)
select.line('date', 'outside_temp', source=source_static, line_color="tomato")
select.line('date', 'bathroom_temp', source=source_static, line_color="green")
select.ygrid.grid_line_color = None
select.add_tools(range_tool)
select.toolbar.active_multi = range_tool

select.sizing_mode = 'stretch_width'

####################################################################################################

battery_plot = figure(plot_width=200,
示例#22
0
              formatters={'date': 'datetime'}))
add_plot_lines(plot_queue, source, plot_sources[3:], Spectral3)
plot_queue.yaxis.axis_label = 'r'
plot_queue.background_fill_color = "#f5f5f5"
plot_queue.grid.grid_line_color = "white"

# plot seletor
select = figure(plot_height=320,
                y_range=plot_queue.y_range,
                sizing_mode="scale_width",
                x_axis_type="datetime",
                y_axis_type=None,
                toolbar_location="right",
                tools=tools)
select
range_rool = RangeTool(x_range=plot_sys.x_range)
range_rool.overlay.fill_color = "navy"
range_rool.overlay.fill_alpha = 0.2

#select.line('date', 'sy_max', source=source)
add_plot_lines(select, source, plot_sources, Spectral6)
select.ygrid.grid_line_color = None
select.add_tools(range_rool)
select.toolbar.active_multi = range_rool
select.background_fill_color = "#f5f5f5"
select.grid.grid_line_color = "white"
select.x_range.range_padding = 0.01

# define a tabela
columns = [
    TableColumn(field="date", title="Data",
def generate_plot(plot_df):
    window_start = plot_df.query(f"{AGE_COL} >= {DEFAULT_WINDOW_SIZE}").index.max()

    # Creating Main Plot
    window_total_max = plot_df.loc[plot_df.index > window_start,
                                   [STILL_OPEN_COL]].max().max() * 1.1
    logging.debug(f"window_start={window_start}, window_total_max={window_total_max}")

    plot = figure(
        plot_height=None, plot_width=None,
        sizing_mode="scale_both",
        x_range=(-5, DEFAULT_WINDOW_SIZE),
        y_range=(0, window_total_max),
        toolbar_location=None,
        x_axis_label="Request Age (days)",
        y_axis_label="Open SR Count (#)",
        background_fill_color="#eaeaf2",
    )
    plot.extra_y_ranges = {"proportion": Range1d(start=0, end=plot_df[CUMULATIVE_PROPORTION_COL].max() * 1.1)}
    secondary_axis = LinearAxis(y_range_name="proportion", axis_label="Request Proportion (%)")
    plot.add_layout(secondary_axis, 'right')

    # Open and less than 180 days
    vbar_open = plot.vbar(
        top=STILL_OPEN_COL, x=AGE_COL, width=0.75, source=plot_df.query(f"{AGE_COL} < @MARKER_LOCATION"),
        fill_color="#4c72b0", line_color="#4c72b0", alpha=0.8, line_alpha=0.8
    )
    # Open and more than 180 days
    vbar_still_open = plot.vbar(
        top=STILL_OPEN_COL, x=AGE_COL, width=0.75, source=plot_df.query(f"{AGE_COL} >= @MARKER_LOCATION"),
        fill_color="#c44e52", line_color="#c44e52", alpha=0.8, line_alpha=0.8
    )

    # 180 day Marker line
    marker_span = Span(
        location=MARKER_LOCATION,
        dimension='height', line_color="#dd8452",
        line_dash='dashed', line_width=4
    )
    plot.add_layout(marker_span)

    # Cumulative Proportion Line
    cum_prop_line = plot.line(
        y=CUMULATIVE_PROPORTION_COL, x=AGE_COL, width=2, source=plot_df, y_range_name="proportion",
        line_color="#55a868", alpha=0.8, line_alpha=0.8
    )

    # Plot grid and axis
    service_delivery_volume_code_metric_plot_widgets_to_minio.stlye_axes(plot)
    plot.yaxis.formatter = NumeralTickFormatter(format="0.[0] a")
    secondary_axis.formatter = NumeralTickFormatter(format="0.[00] %")

    # Plot legend
    legend_items = [("Open < 180 days", [vbar_open]),
                    ("Open > 180 days", [vbar_still_open]),
                    ("Open Requests", [cum_prop_line])]
    service_delivery_volume_code_metric_plot_widgets_to_minio.add_legend(plot, legend_items)

    # Plot tooltip
    hover_tool = HoverTool(tooltips=HOVER_COLS, mode="vline",
                           formatters={f'@{DATE_COL}': 'datetime'},
                           renderers=[cum_prop_line])
    plot.add_tools(hover_tool)

    # Adding select figure below main plot
    select = figure(plot_height=75, plot_width=None,
                    y_range=plot.y_range,
                    sizing_mode="scale_width",
                    y_axis_type=None,
                    tools="", toolbar_location=None, background_fill_color="#eaeaf2")

    range_tool = RangeTool(x_range=plot.x_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2

    select_open = select.line(
        y=STILL_OPEN_COL, x=AGE_COL, line_width=1, source=plot_df.query(f"{AGE_COL} < @MARKER_LOCATION"),
        line_color="#4c72b0", alpha=0.6, line_alpha=0.6
    )
    select_still_open = select.line(
        y=STILL_OPEN_COL, x=AGE_COL, line_width=1, source=plot_df.query(f"{AGE_COL} >= @MARKER_LOCATION"),
        line_color="#c44e52", alpha=0.6, line_alpha=0.6
    )

    select.xgrid.grid_line_color = "White"
    service_delivery_volume_code_metric_plot_widgets_to_minio.stlye_axes(select)

    select.ygrid.grid_line_color = None

    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool

    combined_plot = column(plot, select, height_policy="max", width_policy="max")

    plot_html = file_html(combined_plot, CDN, "Business Continuity Service Delivery Request Age Distribution")

    return plot_html
示例#24
0
    # Add an interactive plot tool to select a date range
    # First, build the plot
    select = figure(
        title=
        "Drag the middle and edges of the selection box to change the range above",
        plot_height=200,
        plot_width=1000,
        y_range=plot.y_range,
        y_axis_type=None,
        tools=["xpan", "xwheel_zoom", "pan", "reset"],
        background_fill_color="#efefef")
    select.toolbar.active_scroll = select.select_one(WheelZoomTool)
    select.toolbar.logo = None

    # Build the range tool we will overlay on the plot
    range_tool = RangeTool(x_range=plot.x_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2

    # Add all the data lines to the plot, and store them (for setting visible later)
    lines2 = []
    for index, source in enumerate(sources):
        lines2.append(
            select.line('Time', 'GM_AOD', source=source, color=colours[index]))

    # Overlay the range tool on the plot
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool

    # Build a checkbox group to select the visible datasets
    # First, write the javascript callback that will set lines visible or not
def generate_plot(plot_df):
    window_start = pandas.to_datetime(WINDOW_START, format=ISO601_DATE_FORMAT)
    window_end = plot_df.index.max() + pandas.Timedelta(days=1)

    y_range_end = plot_df.loc[WINDOW_START:, DURATION_DAYS_COL].max() * 1.1

    logging.debug(f"x axis range: {window_start} - {window_end}")

    plot = figure(
        plot_height=None,
        plot_width=None,
        sizing_mode="scale_both",
        x_range=(window_start, window_end),
        y_range=(0, y_range_end),
        x_axis_type='datetime',
        toolbar_location=None,
        y_axis_label="SR Duration (Days)",
        background_fill_color="#eaeaf2",
    )

    # Previous year lines
    for i in range(1, PREVIOUS_YEARS + 1):
        prefix = PREVIOUS_YEAR_TEMPLATE.format(i)
        for col in COLS_SET:
            previous_year_line = plot.line(x=DATE_COL,
                                           y=f"{prefix}_{col}",
                                           source=plot_df,
                                           color="Grey",
                                           line_width=2,
                                           alpha=0.5)

    line = plot.line(x=DATE_COL,
                     y=DURATION_DAYS_COL,
                     source=plot_df,
                     color="#c44e52",
                     line_width=4,
                     alpha=0.8,
                     line_alpha=0.8)
    circle = plot.circle(x=DATE_COL,
                         y=DURATION_DAYS_COL,
                         source=plot_df,
                         color="#c44e52",
                         size=6,
                         alpha=0.8,
                         line_alpha=0.8)

    # Marker lines
    marker_span = Span(location=pandas.to_datetime(REFERENCE_DATE),
                       dimension='height',
                       line_color="#4c72b0",
                       line_dash='dashed',
                       line_width=4)
    plot.add_layout(marker_span)
    second_marker_span = Span(
        location=pandas.to_datetime(SECOND_REFERENCE_DATE),
        dimension='height',
        line_color="#4c72b0",
        line_dash='dashed',
        line_width=4)
    plot.add_layout(second_marker_span)

    # Plot grid and axis
    plot.grid.grid_line_color = "white"
    plot.xaxis.major_label_orientation = math.pi / 4
    plot.xaxis.formatter = DatetimeTickFormatter(days=ISO601_DATE_FORMAT)
    plot.y_range.start = 0

    plot.axis.axis_label_text_font_size = "12pt"
    plot.axis.major_label_text_font_size = "12pt"

    legend_items = [("Duration (days)", [line, circle]),
                    ("Previous Years", [previous_year_line])]

    legend = Legend(items=legend_items,
                    location="center",
                    orientation="horizontal",
                    margin=2,
                    padding=2)
    plot.add_layout(legend, 'below')

    hover_tool = HoverTool(tooltips=TOOL_TIPS,
                           renderers=[line],
                           mode="vline",
                           formatters={f'@{DATE_COL}': 'datetime'})
    plot.add_tools(hover_tool)

    # Adding select figure below main plot
    select = figure(plot_height=75,
                    plot_width=None,
                    y_range=plot.y_range,
                    sizing_mode="scale_width",
                    x_axis_type="datetime",
                    y_axis_type=None,
                    tools="",
                    toolbar_location=None,
                    background_fill_color="#eaeaf2")

    range_tool = RangeTool(x_range=plot.x_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2

    select_line = select.line(y=DURATION_DAYS_COL,
                              x=DATE_COL,
                              line_width=1,
                              source=plot_df,
                              line_color="#c44e52",
                              alpha=0.6,
                              line_alpha=0.6)

    select_span = Span(location=pandas.to_datetime(REFERENCE_DATE),
                       dimension='height',
                       line_color="#4c72b0",
                       line_dash='dashed',
                       line_width=2)
    select.add_layout(select_span)
    second_select_span = Span(
        location=pandas.to_datetime(SECOND_REFERENCE_DATE),
        dimension='height',
        line_color="#4c72b0",
        line_dash='dashed',
        line_width=2)
    select.add_layout(second_select_span)

    select.xgrid.grid_line_color = "White"
    select.ygrid.grid_line_color = None
    select.xaxis.formatter = DatetimeTickFormatter(days=ISO601_DATE_FORMAT)
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool

    select.axis.axis_label_text_font_size = "12pt"
    select.axis.major_label_text_font_size = "12pt"

    combined_plot = column(plot,
                           select,
                           height_policy="max",
                           width_policy="max")

    plot_html = file_html(
        combined_plot, CDN,
        "Business Continuity Service Request Duration Time Series")

    return plot_html
示例#26
0
def sentiment_tab():
    # Load "sentiment from headlines", creadted by a separate code
    df_head = pd.read_csv('./dashboard/data/headsentiment.csv',
                          parse_dates=True)
    df_head.reset_index()

    df_body = pd.read_csv('./dashboard/data/bodysentiment.csv',
                          parse_dates=True)
    df_body.reset_index()

    # Join two dataframes into one dataframe using the key, named 'time'
    df = df_head.join(df_body, lsuffix='_head', rsuffix='_body')

    dates = np.array(df['time_head'], dtype=np.datetime64)

    # Get a column of pandas df, return a moving average of that column with window size <- window_size
    def set_moving_avg(df, window_size):
        window = np.ones(window_size) / float(window_size)
        return np.convolve(df.copy(), window, 'same')

    # Set initial window sizes to 10 to both of the head and body.
    mv_subj_head = set_moving_avg(df['subj_head'], 5)
    mv_subj_body = set_moving_avg(df['subj_body'], 5)

    # Create a cds object for headlines
    cds_head = ColumnDataSource(data=dict(
        date=dates, polar_head=df['polar_head'], subj_head=mv_subj_head))

    # Create a cds object for body
    cds_body = ColumnDataSource(data=dict(
        date=dates, polar_body=df['polar_body'], subj_body=mv_subj_body))

    p_polar_head = figure(plot_height=300,
                          plot_width=500,
                          tools="",
                          toolbar_location=None,
                          x_axis_type="datetime",
                          x_axis_location="above",
                          background_fill_color="#efefef",
                          x_range=(dates[-10000], dates[-1]),
                          y_range=(-1, 1))

    p_subj_head = figure(plot_height=300,
                         plot_width=500,
                         tools="",
                         toolbar_location=None,
                         x_axis_type="datetime",
                         x_axis_location="above",
                         background_fill_color="#efefef",
                         x_range=(dates[-10000], dates[-1]),
                         y_range=(0, 1))

    p_polar_body = figure(plot_height=300,
                          plot_width=500,
                          tools="",
                          toolbar_location=None,
                          x_axis_type="datetime",
                          x_axis_location="above",
                          background_fill_color="#efefef",
                          x_range=(dates[-10000], dates[-1]),
                          y_range=(-1, 1))

    p_subj_body = figure(plot_height=300,
                         plot_width=500,
                         tools="",
                         toolbar_location=None,
                         x_axis_type="datetime",
                         x_axis_location="above",
                         background_fill_color="#efefef",
                         x_range=(dates[-10000], dates[-1]),
                         y_range=(0, 1))

    # Setting:  Plot a line chart for polar - News headlines
    p_polar_head.line('date',
                      'polar_head',
                      source=cds_head,
                      line_color=Greys5[1])
    p_polar_head.title.text = 'Polar in news headlines'
    p_polar_head.yaxis.axis_label = 'Measurements'

    # Setting: Plot a line chart for subjectivity - News headlines
    p_subj_head.line('date',
                     'subj_head',
                     source=cds_head,
                     line_color=Blues5[0])
    p_subj_head.title.text = 'Subjectivity in news headlines'
    p_subj_head.yaxis.axis_label = 'Measurements'

    # Setting:  Plot a line chart for polar - News bodies
    p_polar_body.line('date',
                      'polar_body',
                      source=cds_body,
                      line_color=Greys5[1])
    p_polar_body.title.text = 'Polar in news bodies'
    p_polar_body.yaxis.axis_label = 'Measurements'

    # Setting: Plot a line chart for subjectivity - News bodies
    p_subj_body.line('date',
                     'subj_body',
                     source=cds_body,
                     line_color=Blues5[0])
    p_subj_body.title.text = 'Subjectivity in news bodies'
    p_subj_body.yaxis.axis_label = 'Measurements'

    select_polar_head = figure(
        title=
        "Drag the middle and edges of the box below to change the range above",
        plot_height=110,
        plot_width=500,
        y_range=(-1, 1),
        x_axis_type="datetime",
        y_axis_type=None,
        tools="",
        toolbar_location=None,
        background_fill_color="white")

    select_subj_head = figure(
        title=
        "Drag the middle and edges of the box below to change the range above",
        plot_height=110,
        plot_width=500,
        y_range=p_subj_head.y_range,
        x_axis_type="datetime",
        y_axis_type=None,
        tools="",
        toolbar_location=None,
        background_fill_color="white")

    select_polar_body = figure(title="",
                               plot_height=110,
                               plot_width=500,
                               y_range=(-1, 1),
                               x_axis_type="datetime",
                               y_axis_type=None,
                               tools="",
                               toolbar_location=None,
                               background_fill_color="white")

    select_subj_body = figure(title="",
                              plot_height=110,
                              plot_width=500,
                              y_range=p_subj_head.y_range,
                              x_axis_type="datetime",
                              y_axis_type=None,
                              tools="",
                              toolbar_location=None,
                              background_fill_color="white")

    # Setting: Plot a range tool for polar - News headlines
    range_tool_polar_head = RangeTool(x_range=p_polar_head.x_range)
    range_tool_polar_head.overlay.fill_color = Blues5[2]
    range_tool_polar_head.overlay.fill_alpha = 0.4

    # Setting: Plot a range tool for subjectivity - News headlines
    range_tool_subj_head = RangeTool(x_range=p_subj_head.x_range)
    range_tool_subj_head.overlay.fill_color = Blues5[2]
    range_tool_subj_head.overlay.fill_alpha = 0.4

    # Setting: Plot a range tool for polar - News headlines
    range_tool_polar_body = RangeTool(x_range=p_polar_body.x_range)
    range_tool_polar_body.overlay.fill_color = Blues5[2]
    range_tool_polar_body.overlay.fill_alpha = 0.4

    # Setting: Plot a range tool for subjectivity - News headlines
    range_tool_subj_body = RangeTool(x_range=p_subj_body.x_range)
    range_tool_subj_body.overlay.fill_color = Blues5[2]
    range_tool_subj_body.overlay.fill_alpha = 0.4

    # Plot a range tool for polar - News headlines
    select_polar_head.line('date',
                           'polar_head',
                           source=cds_head,
                           line_color=Greys5[3])
    select_polar_head.ygrid.grid_line_color = None
    select_polar_head.add_tools(range_tool_polar_head)
    select_polar_head.toolbar.active_multi = range_tool_polar_head

    # Plot a range tool for subjectivity - News headlines
    select_subj_head.line('date',
                          'subj_head',
                          source=cds_head,
                          line_color=Blues5[1])
    select_subj_head.ygrid.grid_line_color = None
    select_subj_head.add_tools(range_tool_subj_head)
    select_subj_head.toolbar.active_multi = range_tool_subj_head

    # Plot a range tool for polar - News bodies
    select_polar_body.line('date',
                           'polar_body',
                           source=cds_body,
                           line_color=Greys5[3])
    select_polar_body.ygrid.grid_line_color = None
    select_polar_body.add_tools(range_tool_polar_body)
    select_polar_body.toolbar.active_multi = range_tool_polar_body

    # Plot a range tool for subjectivity - News bodies
    select_subj_body.line('date',
                          'subj_body',
                          source=cds_body,
                          line_color=Blues5[1])
    select_subj_body.ygrid.grid_line_color = None
    select_subj_body.add_tools(range_tool_subj_body)
    select_subj_body.toolbar.active_multi = range_tool_subj_body

    # Plot sliders
    slider_mv = Slider(title="Smoothing by N days (moving average)",
                       value=5,
                       start=1,
                       end=20,
                       step=1,
                       width=300)

    #s_subj_body = Slider(title="", value=5, start=1, end=20, step=1)

    # Register event handlers for sliders to get a window size of moving avergaes
    def update_mv(attrname, old, new):
        cds_head.data['subj_head'] = set_moving_avg(df['subj_head'], new)
        cds_body.data['subj_body'] = set_moving_avg(df['subj_body'], new)

    slider_mv.on_change('value', update_mv)

    hover_polar_head = p_polar_head.select(dict(type=HoverTool))
    hover_polar_head.tooltips = [("value", "@polar_head"), ("date", "@date")]
    hover_polar_head.mode = 'mouse'

    l = gridplot(
        children=[[p_polar_head, p_polar_body],
                  [select_polar_head, select_polar_body],
                  [p_subj_head, p_subj_body],
                  [select_subj_head, select_subj_body], [None, slider_mv]])

    tab = Panel(child=l, title='Sentiment')

    return tab
示例#27
0
def plot_actual_predictions_series(
    data,
    results,
    model_errors,
    joe=None,
    par=False,
    model='arima',
    target='cumStatpoints',
    metric='mase',
    player_name='Leon Draisaitl',
    deepar_model_name='deepar_truncated_results_unit_s_ne300_lr1e-3_bs64_nl3_cl3.csv'
):
    """ plots the real and predicted time series along with confidence intervals for a player"""
    if results is None:
        model = 'deepar'
    if model == 'arima':
        player_df = data.loc[data.loc[:, 'name'] == player_name]
        player_errors = model_errors.loc[
            model_errors.loc[:, 'name.arima_results_m3'] == player_name]
        try:
            player_error = player_errors['arima_results_m3'].values.astype(
                float).item()
        except ValueError:
            st.write(
                'Unfortunately, Auto-ARIMA was unable to find a suitable model for this player.'
            )
            return
        series_dataframe = calculate_predictions(data,
                                                 results,
                                                 player_name=player_name,
                                                 target=target)
        intervals = return_intervals(results, player_name)
        dates = series_dataframe.index.values.astype(np.datetime64)
        joe = joe.loc[joe.loc[:, 'date'].isin(player_df.date)]
        joe = joe.set_index('date')
        compare_dataframe = joe.loc[:, 'cumStatpoints']
        compare_dataframe = compare_dataframe.sort_index()
    elif model == 'deepar':
        player_df = data.loc[data.loc[:, 'name'] == player_name]
        if model_errors is not None:
            player_errors = model_errors.loc[
                model_errors.loc[:, 'name.{}'.format(deepar_model_name)] ==
                player_name]
            try:
                player_error = player_errors[deepar_model_name].values.astype(
                    float).item()
            except ValueError:
                st.write('This player was not able to be evaluated.')
        else:
            player_df = player_df.sort_values('gameNumber', ascending=True)
            player_df = player_df.set_index('gameNumber')
            # player_df.dropna(axis=1, subset=['predictions'])
        # _, _, rmse = calculate_errors(calculate_residuals(player_df))
        if model_errors is not None:
            series_dataframe = player_df.loc[:, [target, 'predictions']]
        else:
            series_dataframe = player_df.loc[:, 'predictions']
        series_dataframe = series_dataframe.sort_index()
        intervals = player_df.loc[:, ['high', 'low']].dropna()
        intervals = intervals.sort_index()
        if model_errors is not None:
            dates = series_dataframe.index.values.astype(np.datetime64)
        else:
            gameNumbers = series_dataframe.index.values.astype(int)
        # st.write(dates)
        if model_errors is not None:
            joe = joe.loc[joe.loc[:, 'date'].isin(series_dataframe.index)]
            joe = joe.set_index('date')
            compare_dataframe = joe.loc[:, 'cumStatpoints']
            compare_dataframe = compare_dataframe.sort_index()
        if par:
            calculate_par(series_dataframe, compare_dataframe)
    elif data is None and results is None:
        player_name = 'Joe Schmo'
        joe = joe.set_index('date')
        series_dataframe = joe.loc[:, 'cumStatpoints']
        series_dataframe = series_dataframe.sort_index()
        dates = series_dataframe.index.values.astype(np.datetime64)
    start_date = dt.strptime('2018-10-03', '%Y-%m-%d')

    if model_errors is not None:
        # st.dataframe(model_errors)
        real_source = ColumnDataSource(
            data=dict(date=dates, points=series_dataframe[target]))
        compare_source = ColumnDataSource(
            data=dict(date=dates, points=compare_dataframe))
        pred_source = ColumnDataSource(
            data=dict(date=dates, points=series_dataframe['predictions']))
        interval_dates = dates[-intervals.shape[0]:].reshape(-1, 1)
        interval_dates = np.hstack((interval_dates, interval_dates))
    else:
        pred_source = ColumnDataSource(
            data=dict(gameNumber=gameNumbers, points=series_dataframe))
        interval_games = gameNumbers[-intervals.shape[0]:].reshape(-1, 1)
        interval_games = np.hstack((interval_games, interval_games))

    if model_errors is not None:
        y_max = max([
            series_dataframe.loc[:, target].max(), joe.loc[:, target].max()
        ]) + 50
    else:
        try:
            y_min = series_dataframe.values[0]
        except IndexError:
            st.write('This player was not able to be predicted.')
            return
        y_max = series_dataframe.max() + 50

    if model == 'arima':
        player_line = figure(title=('{0}({1},{2},{3})({4},{5},{6},{7}) ({8}: {9:.3f})') \
                                    .format(player_name,
                                            results.loc[player_name, 'p'],
                                            results.loc[player_name, 'd'],
                                            results.loc[player_name, 'q'],
                                            results.loc[player_name, 'P'],
                                            results.loc[player_name, 'D'],
                                            results.loc[player_name, 'Q'],
                                            3, # TODO: undo hardcoding
                                            metric.upper(),
                                            player_error), # TODO: change to MASE
                            plot_height=300,
                            plot_width=800,
                            tools="xpan",
                            toolbar_location='above',
                            x_axis_type="datetime",
                            x_axis_location="below",
                            x_range=(start_date, dates[-1]),
                            y_range=(0, y_max),
                            x_axis_label='Game Date',
                            y_axis_label='Cumulative Points',
                            background_fill_color="#efefef"
                            )
    else:
        if model_errors is not None:
            player_line = figure(
                title=('{0} ({1}: {2:.3f})'.format(
                    player_name, metric.upper(),
                    player_error)),  # TODO: add error
                plot_height=300,
                plot_width=800,
                tools="xpan",
                toolbar_location='above',
                x_axis_type="datetime",
                x_axis_location="below",
                x_range=(start_date, dates[-1]),
                y_range=(0, y_max),
                x_axis_label='Game Date',
                y_axis_label='Cumulative Points',
                background_fill_color="#efefef")
        else:
            player_line = figure(
                title=('{0}'.format(player_name)),  # TODO: add error
                plot_height=300,
                plot_width=800,
                tools="xpan",
                toolbar_location='above',
                x_axis_location="below",
                x_range=(0, 82),
                y_range=(y_min, y_max),
                x_axis_label='Game Date',
                y_axis_label='Cumulative Points',
                background_fill_color="#efefef")
    # elif data is None and results is None:
    #     player_line = figure(title=('{0}'.format(player_name)), # TODO: add error
    #                          plot_height=300,
    #                          plot_width=800,
    #                          tools="xpan",
    #                          toolbar_location='above',
    #                          x_axis_type="datetime",
    #                          x_axis_location="below",
    #                          x_range=(dates[0], dates[-1]),
    #                          background_fill_color="#efefef"
    #                          )

    # hover_tool = HoverTool(tooltips=[("date", "@date"),
    #                                  ("points", "@points")
    #                                 ],
    #                        mode='vline'
    #                        )

    if model_errors is not None:
        player_line.line('date',
                         'points',
                         source=real_source,
                         line_color='blue',
                         legend='actual')
        player_line.line('date',
                         'points',
                         source=compare_source,
                         line_color='green',
                         legend='average')
        player_line.circle('date',
                           'points',
                           source=pred_source,
                           line_color='red',
                           fill_color='red',
                           legend='predicted')
        player_line.varea(x=interval_dates[:, 0],
                          y1=intervals.loc[:, 'high'],
                          y2=intervals.loc[:, 'low'],
                          fill_alpha=0.4,
                          color='red',
                          legend='predicted')
    else:
        player_line.circle('gameNumber',
                           'points',
                           source=pred_source,
                           line_color='red',
                           fill_color='red',
                           legend='predicted')
        player_line.varea(x=interval_games[:, 0],
                          y1=intervals.loc[:, 'high'],
                          y2=intervals.loc[:, 'low'],
                          fill_alpha=0.4,
                          color='red',
                          legend='predicted')

    player_line.legend.location = 'bottom_right'
    player_line.legend.click_policy = 'hide'
    # player_line.add_tools(hover_tool)
    # player_line.toolbar.active_multi = hover_tool
    # player_line.yaxis.axis_label('Cumulative Points')

    test_start = Span(location=start_date,
                      dimension='height',
                      line_color='green',
                      line_dash='dashed',
                      line_width=3)
    player_line.add_layout(test_start)

    if model_errors is not None:
        select = figure(title=("Drag the middle and edges of the"
                               "selection box to change the range above"),
                        plot_height=130,
                        plot_width=800,
                        y_range=player_line.y_range,
                        x_axis_type="datetime",
                        y_axis_type=None,
                        x_axis_label='Game Date',
                        y_axis_label='Cumulative Points',
                        tools="",
                        toolbar_location=None,
                        background_fill_color="#efefef",
                        x_range=(dates[0], dates[-1]))
    else:
        select = figure(title=("Drag the middle and edges of the"
                               "selection box to change the range above"),
                        plot_height=130,
                        plot_width=800,
                        y_range=player_line.y_range,
                        x_axis_type="datetime",
                        y_axis_type=None,
                        x_axis_label='Game Date',
                        y_axis_label='Cumulative Points',
                        tools="",
                        toolbar_location=None,
                        background_fill_color="#efefef",
                        x_range=(0, 82))

    # range_tool = RangeTool(x_range=Range1d(start_date, dates[-1]))
    range_tool = RangeTool(x_range=player_line.x_range)
    range_tool.overlay.fill_color = "navy"
    range_tool.overlay.fill_alpha = 0.2

    if model_errors is not None:
        select.line('date', 'points', source=real_source, line_color='blue')
        select.line('date',
                    'points',
                    source=compare_source,
                    line_color='green')
        select.circle('date',
                      'points',
                      source=pred_source,
                      line_color='red',
                      fill_color='red')
    else:
        select.circle('gameNumber',
                      'points',
                      source=pred_source,
                      line_color='red',
                      fill_color='red')

    select.ygrid.grid_line_color = None
    select.add_tools(range_tool)
    select.toolbar.active_multi = range_tool
    select.add_layout(test_start)

    chart = column(player_line, select)

    st.bokeh_chart(chart)
    # st.write("{}'s dataframe:".format(player_name))
    # st.dataframe(series_dataframe)
    return chart
示例#28
0
p.line('date', 'close', source=source, line_width=2, alpha=0.7)
p.yaxis.axis_label = 'Traffic'
p.background_fill_color = "#f5f5f5"
p.grid.grid_line_color = "white"

select = figure(plot_height=50,
                plot_width=800,
                y_range=p.y_range,
                x_axis_type="datetime",
                y_axis_type=None,
                tools="",
                toolbar_location=None,
                sizing_mode="scale_width")

range_rool = RangeTool(x_range=p.x_range)
range_rool.overlay.fill_color = "navy"
range_rool.overlay.fill_alpha = 0.2

select.line('date', 'close', source=source)
select.ygrid.grid_line_color = None
select.add_tools(range_rool)
select.toolbar.active_multi = range_rool
select.background_fill_color = "#f5f5f5"
select.grid.grid_line_color = "white"
select.x_range.range_padding = 0.01

layout = column(p, select, sizing_mode="scale_width", name="line")

curdoc().add_root(layout)
示例#29
0
# create the select plot below
select = figure(title='Drag the selection box to change the range above',
                plot_height=130,
                plot_width=800,
                y_range=p.y_range,
                x_axis_type='datetime',
                y_axis_type='linear',
                tools='',
                toolbar_location=None,
                background_fill_color='#efefef')

# set hovertool
p.select_one(HoverTool).tooltips = [
    ('Date', '@year/@month/@day'),
    ('AQI', '@value'),
]

# set range_tool
range_tool = RangeTool(x_range=p.x_range)
range_tool.overlay.fill_color = 'navy'
range_tool.overlay.fill_alpha = 0.2

select.line('date', 'value', source=source)
select.ygrid.grid_line_color = None
select.add_tools(range_tool)
select.toolbar.active_multi = range_tool

output_file("aqi_sh.html")
show(column(p, select))
示例#30
0
            'image': [shaded.data],
            'x': [x_range.start],
            'y': [y_range.start],
            'dw': [x_range.end],
            'dh': [y_range.end]
        }

    # for (i, source) in enumerate(sub_data_sources):
    #     source.data['image'] = [datashade(data_frames[i], x_range, y_range, main_plot.plot_width, sub_plot.plot_height)]
    #     source.data['x'] = [x_range.start]
    #     source.data['y'] = [y_range.start]
    #     source.data['dw'] = [x_range.end]
    #     source.data['dh'] = [y_range.end]


range_tool = RangeTool(x_range=main_plot.x_range, y_range=main_plot.y_range)
sub_plot.add_tools(range_tool)
sub_plot.toolbar.active_multi = range_tool


def xrange_change_callback(attr, old, new):
    for i in checkbox_group.active:
        resample_data_source(i, main_data_sources, x_range)


x_range.on_change('start', xrange_change_callback)
x_range.on_change('end', xrange_change_callback)


def handle_reset(_):
    print("Trigger reset")