簡體   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