I have an issue where I can look at data by operating hours or by datetime. I would like to show both of these on along the x axis. I have tried combining them into one categorical axis but it ends up looking like a catastrophe as there is typically 1-2 years of data shown.
So far my best (but still non working) solution is to add an extra x_range, but then I cannot figure out how to get it to link to the existing x axis. Is there a way to do this or some other solution that would get me in the right direction?
Output with one axis.
Output shifted over when second x_range is added same data plotted but against the second range.
Ideally, the data should overlay.
Sample code below.
from bokeh.models import DatetimeTickFormatter, Range1d, NumeralTickFormatter, LinearAxis
from bokeh.plotting import figure
from bokeh.layouts import row
import pandas as pd
from bokeh.io import output_notebook, show
output_notebook()
data_sample = {‘operating_hours’: {‘0’: 5641.0, ‘1’: 5642.0, ‘2’: 5643.0, ‘3’: 5644.0, ‘4’: 5645.0},
‘sensor_reading’: {‘0’: 0.068132542073726654, ‘1’: 0.078682750463485718, ‘2’: 0.071529373526573181, ‘3’: 0.062236662954092026, ‘4’: 0.079991012811660767},
‘timestamp’: {‘0’: ‘2017-01-31 00:00:00’, ‘1’: ‘2017-01-31 00:59:59.999996672’, ‘2’: ‘2017-01-31 02:00:00.000003328’, ‘3’: ‘2017-01-31 03:00:00’, ‘4’: ‘2017-01-31 03:59:59.999996672’}}
df = pd.DataFrame.from_dict(data_sample)
df.timestamp = pd.to_datetime(df.timestamp,infer_datetime_format=True)
p = figure(width=800, height=400, webgl=True)
p.line(df.timestamp, df.sensor_reading,
line_width=1, alpha=0.8, color=’#005195’)
p.circle(df.timestamp, df.sensor_reading,
color=’#005195’, size=2, alpha=0.8)
p.xaxis.formatter = DatetimeTickFormatter(
hours=["%d %B %Y"],
days=["%d %B %Y"],
months=["%d %B %Y"],
years=["%d %B %Y"])
p.extra_x_ranges = {“Hours”: Range1d(start=df.operating_hours.min(),
end=df.operating_hours.max())}
p.circle(df.operating_hours, df.sensor_reading,
x_range_name=‘Hours’, color=“green”, size=2, alpha=0.8)
p.add_layout(LinearAxis(x_range_name=“Hours”, axis_label=“Hours”,
axis_label_text_align=‘left’), “below”)
show(p)
``
Thanks for your help.