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

时间:2017-12-30 20:53:17

标签: python datetime bokeh

我有一个提供Bokeh图的Web应用程序(使用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"
  )

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

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

  • 创建一个custom extension以准确返回您想要的主要和次要刻度

  • 使用" day"每天都有自动收报机,并按照你的建议隐藏你不想看的标记。

第二条路径(您的想法)可能是最简单的入门方式。以下是使用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)

生成此输出

enter image description here

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

相关问题