📑

holoviews (bokeh)で株価をプロット

2022/03/27に公開

holoviewsでtimseseriseデータをcurveでplotする方法

import pandas as pd
import holoviews as hv
from holoviews import opts

from holoviews.plotting.links import RangeToolLink

hv.extension('bokeh')

import bokeh.sampledata
bokeh.sampledata.download()
from bokeh.sampledata.stocks import AAPL

aapl_df = pd.DataFrame(AAPL['close'], columns=['close'], index=pd.to_datetime(AAPL['date']))
aapl_df.index.name = 'Date'

aapl_curve = hv.Curve(aapl_df, 'Date', ('close', 'Price ($)'))
tgt = aapl_curve.relabel('AAPL close price').opts(width=800, labelled=['y'], tools=['hover'])
src = aapl_curve.opts(width=800, height=100, yaxis=None, default_tools=[])

RangeToolLink(src, tgt)

layout = (tgt + src).cols(1)
plot_obj = layout.opts(opts.Layout(shared_axes=False, merge_tools=False))
plot_obj

保存方法

renderer = hv.renderer('bokeh').instance(fig='html')
renderer.save(plot_obj, 'fname', style=dict(Image={'cmap':'jet'}))

html+jsで保存されるので、pythonが起動してなくもグリグリ動かせる

Discussion