0
0

More than 3 years have passed since last update.

Bokeh_リアルタイムトレンド書いてみた

Posted at

from bokeh.plotting import figure, output_file, show
from bokeh.models import DatetimeTickFormatter
from bokeh.models import ColumnDataSource, HoverTool, CrosshairTool, NumeralTickFormatter
from bokeh.models.widgets import Button, Select, RadioGroup, TextInput, TableColumn, DataTable
from bokeh.models.widgets import DateFormatter

from bokeh.layouts import Column, Row
from bokeh.plotting import curdoc
import pandas as pd

from bokeh.palettes import Dark2_5 as palette
import random
import itertools

global df

colors has a list of colors which can be used in plots

colors = ["red","blue","green","black"]

出力HTMLファイルパスを指定

df = pd.read_csv("stockchart_20201115.csv")

output_file("graph1.html", mode="inline")

描画レイアウト

df['日付']=pd.to_datetime(df['日付'])

data = {'x' : df["日付"],
'y' : df["始値"],
'y_end': df["終値"],
'y_start' : df["始値"]
}

source = ColumnDataSource(data= data)

p = figure(
title="simple line example",
plot_width=1000,
plot_height=500,
x_axis_type="datetime",
y_axis_label ="日経平均株価",
x_axis_label ="日時"
)

プロットに判例と線の暑さを指定したrenderer(今回は折れ線グラフ)を追加

p.line('x', 'y', source = source)

吹き出し

hover = HoverTool(
tooltips=[
("Date", "$x{%F}"),
("始値", "$y{0,0}"),
("終値", "@y_end{0,0}")

],
formatters={"$x": "datetime"},
mode="vline",
show_arrow=False, # 矢印を消します
)

columns = [
TableColumn(field="x", title="One", formatter=DateFormatter()),
TableColumn(field="y", title=" Two")
]

表を表示

data_table = DataTable(source=source, columns=columns,selectable = True,
sortable = True, width=600, height=500, fit_columns=False)

layout = Row(p,data_table)

def update():
df_new = pd.read_csv("stockchart_20201115.csv")

# 描画レイアウト

df['日付']=pd.to_datetime(df['日付'])

data = {'x' : df["日付"],
        'y' : df["始値"],
        'y_end': df["終値"],
        'y_start' : df["始値"]
       }
source.stream(data)

i=random.randint(0,3)

p.line('x', 'y', source = source,color=colors[i])

source.stream(data)

curdoc().add_root(layout)
curdoc().add_periodic_callback(update, 1000)

結果を描画

show(layout)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0