【问题标题】:How do you add more x axis ticks and labels to datetime axis using Python's Bokeh?如何使用 Python 的 Bokeh 向日期时间轴添加更多 x 轴刻度和标签?
【发布时间】:2014-05-30 10:02:38
【问题描述】:

我一直在测试 Python 的 Bokeh,特别是烛台图工具,但无法弄清楚如何在我的图表中添加超过 5 个日期时间标签/刻度。任何见解将不胜感激。代码如下:

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()

这是生成的图像:

【问题讨论】:

    标签: python datetime matplotlib visualization bokeh


    【解决方案1】:

    首先,感谢您试用 Bokeh!回答您的问题,现在,您无法指定图中的刻度数。它是自动计算的。我在我们的问题跟踪器 (https://github.com/ContinuumIO/bokeh/issues/665) 中打开了一个问题,以增强功能以​​回答和解决您的请求。如果您愿意,请随时参加 cmets。

    干杯。

    【讨论】:

    • 这个功能是隐藏的,但它有效!我自己永远不会找到这个。
    猜你喜欢
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    相关资源
    最近更新 更多