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

问题描述 投票:1回答:1

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

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

python-3.x bokeh
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.