Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Bokeh datetimetickformatter to customize ticks

Tags:

python

bokeh

I am relatively new to python and I am using bokeh to create a html file that plots some timeseries data.

I would like to format the x-axis ticks as "DD/MM HH:SS". I am writing a simplified version of the code:

from bokeh.plotting import figure, output_file, save, show
from bokeh.models import DatetimeTickFormatter
import datetime as dt
t=[dt.datetime(2017, 1, 9, 16, 14, 10),dt.datetime(2017, 1, 9, 16, 15, 20)]
Temp=[200,210]
output_file("Burner SPC test.html")
p1=figure(title="Tip1 TC", x_axis_label="Time", y_axis_label="Temp Diff", x_axis_type="datetime")
p1.line(t,Temp)
p1.xaxis.formatter=DatetimeTickFormatter(formats=dict(
days=["%??"],
months=["%??"],
hours=["%???"],
minutes=["%???"])) #not sure how to format here to get the desired output
show(p1)

Any help is highly appreciated. Thanks in advance.

like image 440
Avs Avatar asked Nov 08 '22 03:11

Avs


1 Answers

DateTimeTickFormatter is not expecting dicts anymore (as mentioned by bigreddot here). With my version of Bokeh (1.0.4) it worked by removing "formats=dict()" and providing the format strings.

p1.xaxis.formatter=DatetimeTickFormatter(days="%m/%d %H:%M",
months="%m/%d %H:%M",
hours="%m/%d %H:%M",
minutes="%m/%d %H:%M")

You could do both. Use the same format string for all of them, or use individual format strings. The Format of the axis adjusts when zooming in.

from bokeh.plotting import figure, output_file, save, Show
from bokeh.models import DatetimeTickFormatter
import datetime as dt
import datetime as dt
t=[dt.datetime(2017, 1, 9, 16, 14, 10),dt.datetime(2017, 1, 10, 17, 15, 20)]
Temp=[200,210]
output_file("Burner SPC test.html")
p1=figure(title="Tip1 TC", x_axis_label="Time", y_axis_label="Temp Diff", x_axis_type="datetime")
p1.line(t,Temp)
p1.xaxis.formatter=DatetimeTickFormatter(days="%m/%d",
hours="%H",
minutes="%H:%M")
show(p1)
like image 176
JackByte Avatar answered Nov 14 '22 21:11

JackByte