繁体   English   中英

散景日期时间轴,控制次要刻度

[英]Bokeh datetime axis, control of minor ticks

我有一个Web应用程序为Bokeh图提供服务(使用Bokeh v0.12.7)。 x轴是日期时间,显示用户定义的天数(日期范围默认为31天)。 如果没有在Bokeh中定义任何刻度参数,主要刻度的数量默认为5,如此处的实时网络应用程序所示:

https://btac-web-plots.herokuapp.com/avyview?style=snowpacktracker

我希望在主要刻度之间显示每天的小刻度。 我尝试了以下内容,它允许任何日期范围长度,并指定每5天的主要刻度( startend是Pandas日期时间索引):

num_days = ((end - start) / np.timedelta64(1, 'D')).astype(int)
fig.xaxis.ticker = DaysTicker(
  days = np.arange(1, num_days, 5),
  num_minor_ticks = 4
  )

这样可以每隔5天正确显示主刻度,但不会显示次刻度。 另一个可能的解决方案可能是每天绘制主要刻度,然后将刻度标签设置为隐藏数天,除了每隔5天(不确定如何实现此...)。

在这些地块上显示每日小蜱的最佳方法是什么?

这是我的绘图代码(包括DaysTicker )的一部分,使用第一个面板作为示例:

fig = figure(
  title="New Snow, SWE, Snow Depth, and Settlement",
  name="newsnow_extra_fig",
  x_axis_type="datetime",
  y_axis_label='HN24 / SWE (in)',
  width=width, height=height2,
  tools=tools,
  toolbar_sticky=False,
  logo=None
  )
fig.y_range = Range1d(-12, 30)
fig.extra_y_ranges = {"Depth": Range1d(start=-72, end=180)}
fig.add_layout(LinearAxis(y_range_name="Depth", axis_label='HS (in)' ), 'right')
fig.yaxis.axis_label_standoff = 5

num_days = ((end - start) / np.timedelta64(1, 'D')).astype(int)
fig.xaxis.ticker = DaysTicker(
  days=np.arange(1,num_days,5),
  num_minor_ticks=4
  )

newcds, newcds_sums = create_newsnow_extra_cds(df, 'mid', start, end)

dline = fig.line(source=newcds,
  x='Date', y='depth', 
  line_color="blue", line_width=2, line_alpha=0.5, 
  legend="snow depth (HS)", name="depthline",
  y_range_name='Depth',
  )

nsbars = fig.vbar(source=newcds,
  x='newsnow_x', width=WIDTH, bottom=0, top='newsnow', 
  color="blue", alpha=0.9, legend='new snow (HN24)', name='nsbars'
  )
swebars = fig.vbar(source=newcds,
  x='swex10_x', width=WIDTH, bottom=0, top='swex10',
  color="blue", alpha=0.3, legend='SWE x 10', name='swebars'
  )

settlebars = fig.vbar(source=newcds, 
  x='Date', width=WIDTH*2.0, bottom='settle', top=0, 
  color="orange", alpha=0.75,
  name="settlebars", legend="settlement"
  )

内置的DatetimeAxis并不真正公开与您的用例相对应的次要刻度配置。 所以你的选择是:

  • 创建一个自定义扩展,以准确返回您想要的主要和次要刻度

  • 根据您的建议,每天使用“日期”自动收报机并隐藏您不希望看到的标记。

第二条路径(你的想法)可能是最简单的开始。 这是一个完整的代码示例,使用FuncTickFormatter生成“空白”刻度标签,除了每隔五天:

import numpy as np
import pandas as pd

from bokeh.io import output_file, show
from bokeh.models import DaysTicker, FuncTickFormatter
from bokeh.plotting import figure

x = pd.date_range("2017-12-01", "2017-12-31")
y = ([1,3,4]*11)[:31]

p = figure(plot_width=800, x_range=(x[0], x[-1]), x_axis_type="datetime")

p.line(x, y, line_dash="4 4", line_width=1, color='gray')

p.xaxis.ticker = DaysTicker(days=np.arange(1,32))
p.xaxis.major_label_orientation = 1.5

p.xaxis.formatter = FuncTickFormatter(code="""
    var date = new Date(tick)
    var day = date.getUTCDate(date)
    if ( day%5 == 0 ) { return day }
    else { return "" }
""")

output_file("foo.html")

show(p)

这会产生这个输出

在此输入图像描述

如果你想要的不仅仅是日期数,你可以调整FuncTickFormatter的JS代码中的第一个if分支,而不仅仅是return day

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM