I'm trying to add an axis to a Bokeh plot in Python, with ticks at locations specified and labels as specified.
The plot I am making is a collection of daily data, but I am not using x_axis_type="datetime" because I do not want to assign a specific year to the data. That is, I am plotting data for the years 1950-2015 but I want the axis to say only "1 January", not "1-January-2000".
Originally I was using a CategoricalAxis, but this crowds out the labels because it places one for each of 366 ticks. I would like to reduce this to around 12 ticks to show each month.
I am able to use a LinearAxis to place the ticks in the location that I want, which is the first date of each month. However the tick labels are numeric and I would like to use string labels instead.
How can I change the tick labels to use a specific string?
# Remove default axis
p.xaxis.visible = False
# Add custom axis
ticker = FixedTicker()
ticker.ticks = first_day_of_month
xaxis = LinearAxis(ticker=ticker)
p.add_layout(xaxis, 'below')
1st attempt using standard CategoricalAxis, the x-axis is crowded:

2nd attempt using code as posted, I want to change the numbers to labels:

EDIT
The DatetimeTickFormatter was not immediately useful because the source format for the data was not a proper date. The x data is actually strings in the form "mmdd", eg "1230", which is why I was using CategoricalAxis in the first place.
Using FuncTickFormatter has been successful, if a little cumbersome. I'm using the code below which gives basically what I want.
CustomFuncTickFormatter = FuncTickFormatter(code = '''
return {0: 'January', 366: '', 335: 'December', 305: 'November', 274: 'October', 244: 'September', 213: 'August', 182: 'July', 152: 'June', 121: 'May', 91: 'April', 60: 'March', 31: 'February'}
[tick]
''')

You should be able to use x_axis_type="datetime", FixedTicker and a custom instance of DatetimeTickFormatter (documentation) with all fields set to [%d %m]. If that doesn't work for some reason, you can always use FuncTickFormatter (documentation).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With