散景:更改图表x轴上的时间频率

时间:2019-04-27 20:20:44

标签: python-3.x bokeh

以下代码代表散景中的烛台图:

from math import pi
import pandas as pd

from bokeh.plotting import figure, show, output_file
from bokeh.sampledata.stocks import MSFT

df = pd.DataFrame(MSFT)[:50]
df["date"] = pd.to_datetime(df["date"])

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

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

TOOLS = "pan,wheel_zoom,box_zoom,reset,save"

p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1000, toolbar_location="left")

#p.title = "MSFT Candlestick"
p.xaxis.major_label_orientation = pi/4
p.grid.grid_line_alpha=0.3

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

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

show(p)  # open a browser

如您在此结果中看到的,x轴日期与3月,1日和3月,15日等匹配。可以增加频率,因此下一个日期在3月之后,即1月为3月。 ,例如5号?

enter image description here

1 个答案:

答案 0 :(得分:1)

Bokeh documentation提供了多个选项。在某些情况下,像这样设置desired_num_ticks可能会有所帮助:

p.xaxis[0].ticker.desired_num_ticks = 20

或者您可以尝试例如:

from bokeh.models import DaysTicker

p.xaxis[0].ticker = DaysTicker(days = [1, 5, 10, 15, 20, 25, 30])

结果:

enter image description here