如何使用Python的Bokeh向datetime轴添加更多的x轴刻度和标签?

时间:2022-01-07 00:35:49

I've been testing out Python's Bokeh, specifically the candlestick chart tools but have been unable to figure out how to add more than 5 datetime labels/ticks to my chart. Any insight would be appreciated. Here's the code:

我一直在测试Python的Bokeh,特别是烛台图表工具,但一直无法找到如何向我的图表添加超过5个datetime标签/tick的方法。任何见解都将受到赞赏。这是代码:

from math import pi
import pandas as pd
import pandas.io.data as web
from bokeh.plotting import *


stocks = 'FB'

#######################################################
date_today = time.strftime("%x") 

def get_px(stock, start, end):
    return web.get_data_yahoo(stock, start, end)

x = get_px(stocks, '1/1/2013', date_today)    
######################################################
######## Function Defined ##############################
def dateConvert(df):
    dt = df.index
    df['Date'] = dt
    df.reset_index(drop=True)
    return df

##### transform data for useability #####

df = dateConvert(x)
df = df.reset_index(drop=True)
df = df.sort_index(axis=1)

cols = ['adj_close', 'close', 'date', 'high', 'low', 'open', 'volume']
df.columns = cols

ewma50 = pd.ewma(df.close, span=50)
ewma200 = pd.ewma(df.close, span=200)


#######################################################
##### PLOT CODE #######################################

mids = (df.open + df.close)/2
spans = abs(df.close-df.open)

inc = df.close > df.open
dec = df.open > df.close
w = 24*60*60*1000 # half day in ms

output_file("FB_candlestick.html", title="FB_candlestick.py example")

figure(x_axis_type = "datetime", tools="pan,wheel_zoom,box_zoom,reset,previewsave",
       width=1000, name="candlestick")

hold()

segment(df.date, df.high, df.date, df.low, color='black')
rect(df.date[inc], mids[inc], w, spans[inc], fill_color="#D5E1DD", line_color="black")
rect(df.date[dec], mids[dec], w, spans[dec], fill_color="#F2583E", line_color="black")

line(df.date, ewma50, color='blue', legend='ewma 50 day')
line(df.date, ewma200, color='red', legend='ewma 200 day')

curplot().title = "FB Candlestick"
xaxis().major_label_orientation = pi/4

grid().grid_line_alpha=0.3
curplot().width=1200

show()

Here is the resulting image:

这里是结果图像:

如何使用Python的Bokeh向datetime轴添加更多的x轴刻度和标签?

1 个解决方案

#1


5  

First, thanks for trying Bokeh! Answering to your question, right now, you are not able to specify the amounts of ticks in the plot. It is automatically calculated. I have opened an issue in our issue tracker (https://github.com/ContinuumIO/bokeh/issues/665) to work in an enhancement to answer and solve your request. Feel free to participate in the comments if you want.

首先,谢谢你的努力!回答你的问题,现在,你不能指定的数量的滴答。它是自动计算。我在我们的问题跟踪器(https://github.com/continuumio/bokeh/issues es/665)中打开了一个问题,以改进您的请求。如果你愿意的话,可以参与评论。

Cheers.

欢呼。

#1


5  

First, thanks for trying Bokeh! Answering to your question, right now, you are not able to specify the amounts of ticks in the plot. It is automatically calculated. I have opened an issue in our issue tracker (https://github.com/ContinuumIO/bokeh/issues/665) to work in an enhancement to answer and solve your request. Feel free to participate in the comments if you want.

首先,谢谢你的努力!回答你的问题,现在,你不能指定的数量的滴答。它是自动计算。我在我们的问题跟踪器(https://github.com/continuumio/bokeh/issues es/665)中打开了一个问题,以改进您的请求。如果你愿意的话,可以参与评论。

Cheers.

欢呼。