如何在散景图上添加 1W、1M、MTD、1Q、1H、1Y、YTD 范围按钮

问题描述

我试图在散景图上获得 1W、1M、MTD、1Q、1H、1Y、YTD 按钮。

我遇到了一个 Plotly example,如下所示:

c_candlestick.update_xaxes(
    title_text = 'Date',rangeslider_visible = True,rangeselector = dict(
        buttons = list([
            dict(count = 1,label = '1M',step = 'month',stepmode = 'backward'),dict(count = 6,label = '6M',dict(count = 1,label = 'YTD',step = 'year',stepmode = 'todate'),label = '1Y',dict(step = 'all')])))

Chart with Date Range Buttons

我想在 Google Colab 的 bokeh 中做同样的事情。


我到目前为止的代码是:

import pandas as pd
import numpy as  np
from bokeh.plotting import figure,output_notebook,show
from bokeh.models import Linearaxis,Range1d,Segment,Legend
from bokeh.models.formatters import NumeralTickFormatter,DatetimeTickFormatter
from bokeh.palettes import Category20
output_notebook()

def init_chart(name):
  # Tools Selections:
  TOOLS = "pan,xwheel_zoom,Box_zoom,crosshair,undo,redo,reset,save"

  p = figure(x_axis_type="datetime",tools=TOOLS,title = name)
  p.add_layout(Legend(click_policy="hide",orientation='horizontal',spacing=20),'below')
  p.sizing_mode = 'scale_both'

  return p

def init_price_volume_chart(p,df):
  RED = Category20[7][6]
  GREEN = Category20[5][4]

  inc = df.Close > df.Open
  dec = df.Open > df.Close
  w = 12*60*60*1000 # half day in ms

  p.x_range = Range1d(df.index[0],df.index[-1])
  p.xaxis[0].formatter = DatetimeTickFormatter(months="%b %Y",days="%d %b %Y")
  p.xaxis.axis_label = 'Date'
  p.xaxis.major_label_orientation = 3.1415/4
  p.xaxis.ticker.desired_num_ticks = 40

  # left y axis
  low,high  = df.Low.min(),df.High.max()
  p.y_range = Range1d(low * 0.9,high * 1.1)
  p.yaxis.axis_label = 'Price'
  p.yaxis[0].formatter = NumeralTickFormatter(format="0,0.00")
  p.segment(df.index,df.High,df.index,df.Low,color=GREEN,legend_label='Candlestick')
  p.vbar(df.index[inc],w,df.Open[inc],df.Close[inc],fill_color=GREEN,line_color=GREEN,legend_label='Candlestick')
  p.vbar(df.index[dec],df.Open[dec],df.Close[dec],fill_color=RED,line_color=RED,legend_label='Candlestick')

  # right y axis
  p.extra_y_ranges.update({'two':  Range1d(0,1.1*df.Volume.max())})
  p.add_layout(Linearaxis(y_range_name='two',axis_label='Volume' ),'right')
  p.yaxis[1].formatter = NumeralTickFormatter(format="0a")
  p.vbar(df.index[inc],df.Volume[inc],[0]*inc.sum(),alpha=0.5,level='underlay',legend_label='Volume',y_range_name='two')
  p.vbar(df.index[dec],df.Volume[dec],[0]*dec.sum(),y_range_name='two')
  
  return p

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)