Avs
Avs

Reputation: 75

How to use Bokeh datetimetickformatter to customize ticks

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.

Upvotes: 6

Views: 15598

Answers (3)

György Baffia
György Baffia

Reputation: 1

I just deleted all parameters, using the defaults:

formatter=DatetimeTickFormatter(),

It works!

Upvotes: 0

JackByte
JackByte

Reputation: 116

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)

Upvotes: 4

Avs
Avs

Reputation: 75

I solved it using "%m/%d %H:%M" for all the fields: months, days, hours, minutes:

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

Initially I was trying to use "%m" for months, "%d" for days, "%H" for hours and "%M" for minutes but since I want the same format across all those scales, they all need to be configured the same.

Upvotes: -1

Related Questions