Hi,
I’d like to have a plot with two datetime X axes: one for exact values and the other for approximate ones. Below I include the working example and animated gif with explanation of the problem. The problem arises with formatting of the second axis. It works but not consequently, i.e. some DatetimeTickFormatter
units seem to be ignored. Anyone can explain that behavior and hopefully help with fixing that?
import numpy as np
import pandas as pd
from numpy import sqrt
from bokeh.plotting import figure, show, output_file
from bokeh.models import LinearAxis, Range1d, ColumnDataSource, WheelZoomTool, DatetimeTickFormatter, Range1d, DatetimeAxis
timestamp = pd.date_range(start='1/1/2020', end='31/12/2020', freq='10min')
np.random.seed(0)
value = np.cumsum(np.random.randn(len(timestamp)) * sqrt(.030) * sqrt(1 / 252.))
source = ColumnDataSource({'timestamp': timestamp, 'value': value})
p = figure(x_axis_type='datetime', tools="pan,reset,wheel_zoom,box_zoom", width=900, height=400)
p.line(x = 'timestamp', y = 'value', source=source, line_alpha=0.2)
p.toolbar.active_scroll = p.select_one(WheelZoomTool)
p.axis.formatter = DatetimeTickFormatter(minutes=["%H:%M (minutes)"],
hourmin=["%H:%M (hourmin)"],
hours=["%Y-%m-%d %H:%M (hours)"],
days=["%Y-%m-%d (days)"],
months=["%Y-%m-%d (months)"])
p.extra_x_ranges = {'second_axis': Range1d(start=timestamp[0], end=timestamp[-1])}
p.add_layout(DatetimeAxis(x_range_name='second_axis'), 'below')
# some formatting
p.axis[1].major_tick_line_color = None
p.axis[1].minor_tick_line_color = None
p.axis[1].axis_line_color = None
p.axis[1].major_label_text_font_style = 'bold'
p.axis[1].major_label_text_color = 'blue'
p.axis[1].major_label_text_font_size = '12px'
p.axis[1].ticker.num_minor_ticks = 2
p.axis[1].ticker.desired_num_ticks = 2
p.axis[1].formatter = DatetimeTickFormatter(seconds=['%Ss'],
minsec=['%M:%S (minsec)'],
minutes=["%m-%d (minutes)"],
hourmin=["%m-%d (hourmin)"],
hours=["%B %Y (hours)"],
days=["%B %Y (days)"],
months=["%Y (months)"])
show(p)
edit: better quality of the gif
edit2: improvement in the code