How do I make bokeh omit missing dates when using datetime as x-axis

后端 未结 2 933
迷失自我
迷失自我 2020-12-09 23:22

I am looking at the candlestick example in the bokeh docs, found here:

https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/candlestick.py

and I

2条回答
  •  不思量自难忘°
    2020-12-09 23:40

    UPDATE: As of Bokeh 0.12.6 you can specify overrides for major tick labels on axes.

    import pandas as pd
    
    from bokeh.io import show, output_file
    from bokeh.plotting import figure
    from bokeh.sampledata.stocks import MSFT
    
    df = pd.DataFrame(MSFT)[:50]
    inc = df.close > df.open
    dec = df.open > df.close
    
    p = figure(plot_width=1000, title="MSFT Candlestick with Custom X-Axis")
    
    # map dataframe indices to date strings and use as label overrides
    p.xaxis.major_label_overrides = {
        i: date.strftime('%b %d') for i, date in enumerate(pd.to_datetime(df["date"]))
    }
    
    # use the *indices* for x-axis coordinates, overrides will print better labels
    p.segment(df.index, df.high, df.index, df.low, color="black")
    p.vbar(df.index[inc], 0.5, df.open[inc], df.close[inc], fill_color="#D5E1DD", line_color="black")
    p.vbar(df.index[dec], 0.5, df.open[dec], df.close[dec], fill_color="#F2583E", line_color="black")
    
    output_file("custom_datetime_axis.html", title="custom_datetime_axis.py example")
    
    show(p)
    

    If you have a very large number of dates, this approach might become unwieldy, and a Custom Extension might become necessary.

提交回复
热议问题