Python Bokeh Tick标签

时间:2016-03-17 18:03:25

标签: python datetime labels bokeh ticker

我是新手,我正在尝试在线图中绘制股票数据 X轴应为日期,y轴为股票的收盘价。

以下是我的代码示例:

data = pd.DataFrame({'Symbol' : ['AMBA','FB','WWD','AMBA','FB','WWD','AMBA','FB','WWD','AMBA','FB','WWD','AMBA','FB','WWD','AMBA','FB','WWD',],
                     'Previous_Close' : [10,20,30,10,20,30,10,20,30,10,20,30,10,20,30,10,20,30,],
                     'Date' : pd.to_datetime(['01/01/16', '01/01/16', '01/01/16', '01/02/16', '01/02/16', '01/02/16', '01/03/16', '01/03/16', '01/03/16', '01/04/16',
                                            '01/04/16', '01/04/16', '01/05/16', '01/05/16', '01/05/16', '01/06/16', '01/06/16', '01/06/16'], format = '%m/%d/%y')})
Symbols = [sym for sym in data.Symbol.unique()]
Dates = [date for date in data.Date.unique()]
format_dates = [date.strftime('%b%d_%y') for date in dates]
colors = ['Darkred', 'Orange', 'Navy']

output_notebook()

TOOLS = ['hover','pan','box_zoom','resize', 'save', 'reset']
# CREATE FIGURE
p = figure(width = 800, height = 600, tools = TOOLS)

cnum = 0
# PLOT
for sym in Symbols:
    # plot stock closing price
    p.line(range(len(dates)), data[data.Symbol==sym].Previous_Close, color = colors[cnum])
    # add stock symbol as text label to end of line
    p.text(len(Symbols), data[data.Symbol==sym].Previous_Close.iloc[-1], [sym],
           text_color = colors[cnum], text_font_size = '10pt')
    cnum +=1
show(p)

显然数据是在这里编制的,但想法是一样的。我想要的是将x刻度标签设置为格式化的日期名称。在Pyplot我会这样做:

ax.set_xticklables(format_dates)
axis()

我尝试将x_axis_type设置为' datetime'但这只给我几毫秒。我尝试过使用DateTimeFormatter(),但要么我不理解如何使用它,要么它不起作用(可能是前者)。

我做过的事情(RE ANSWERS):

这会将x轴更改为毫秒:     p = figure(width = 800,height = 600,x_axis_type =' datetime',tools = TOOLS)

cnum = 0
# PLOT
for sym in Symbols:
    # NO CHANGE TO X ARG
    p.line(range(len(dates)), data[data.Symbol==sym].Previous_Close, color = colors[cnum])

这将x刻度从1970年改为几十年:     p = figure(width = 800,height = 600,x_axis_type =' datetime',tools = TOOLS)

cnum = 0
# PLOT
for sym in Symbols:
    # CHANGE X ARG TO DATETIME DATES
    p.line(dates, data[data.Symbol==sym].Previous_Close, color = colors[cnum])

2 个答案:

答案 0 :(得分:1)

使用:

p = figure(width = 800, height = 600, x_axis_type='datetime', tools = TOOLS)

答案 1 :(得分:1)

看起来您的pandas索引导致了问题。我认为这段代码应该有效:

import numpy as np
import pandas as pd
from bokeh.plotting import figure, output_file, show

data = pd.DataFrame({'Symbol' : ['AMBA','FB','WWD','AMBA','FB','WWD','AMBA','FB','WWD','AMBA','FB','WWD','AMBA','FB','WWD','AMBA','FB','WWD',],
                     'Previous_Close' : [10,20,30,10,20,30,10,20,30,10,20,30,10,20,30,10,20,30,],
                     'Date' : pd.to_datetime(['01/01/16', '01/01/16', '01/01/16', '01/02/16', '01/02/16', '01/02/16', '01/03/16', '01/03/16', '01/03/16', '01/04/16', '01/04/16', '01/04/16', '01/05/16', '01/05/16', '01/05/16', '01/06/16', '01/06/16', '01/06/16'], format = '%m/%d/%y')})

symbols = [sym for sym in data.Symbol.unique()]
dates = [date for date in data.Date.unique()]
dates.sort()
lastDate = dates[-1]
colors = ['Darkred', 'Orange', 'Navy']

output_notebook()

TOOLS = ['hover','pan','box_zoom','resize', 'save', 'reset']
# CREATE FIGURE
p = figure(width = 800, height = 600, tools = TOOLS, x_axis_type="datetime")

# PLOT
for cnum, sym in enumerate(symbols):
    # plot stock closing price
    p.line(data[data.Symbol==sym].Date.values, data[data.Symbol==sym].Previous_Close.values, color = colors[cnum])

p.text(data[data.Date==lastDate].Date.values, data[data.Date==lastDate].Previous_Close.values, data[data.Date==lastDate].Symbol.values, text_color = colors, text_font_size = '10pt')

show(p)

这是输出的样子: example using dates in an axis