Overriding x-axis labels


Here is my need. I plot all my points based on decimal indices but I’m trying to override their labels with dates, only on integer values:

from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show

import pandas as pd
import datetime

data = pd.DataFrame({"Index":list(range(10)),
					 "Value":[x * 10 for x in list(range(10))],
					 "Time":[datetime.datetime(2021, 3, i) for i in range(1, 11)]})

p = figure()

p.xaxis.major_label_orientation = 3.1415 / 4
p.xaxis.major_label_overrides = {
	i: date.strftime("%x %X") for i, date in enumerate(data["Time"])

source = ColumnDataSource(data)
source.data["Left"] = source.data["Index"] - 0.5
p.line("Left", "Value", source=source)

The problem is when I zoom, decimal indices appear:

How can I only associate labels with integer values?


Hi @Mark531 This is a rather specialized ask, I am not sure there is a perfect solution out of the box. For starters you, can set the mantissas property on the the ticker to [1] so that only multiples of 1 are considered “nice” numbers for tick values. But note that e.g. 0.1 is considered a multiple of 1 for these purposes. What exactly are you expecting to happen if user zoom in far enough that there are not any integers at all in the range?

Hi Bryan, well I care to admit that when you read it that way, my query looks quite artificial, but I just tried to provide a minimal example showing the issue.

So, I tried your solution with mantissas, but as you mentioned, it also labels multiples of 0.1. But it’s ok since I won’t zoom between two points.

Could you just tell me what the default ticker for the x-axis is? I cannot find this information in the documentation.

It’s the BasicTicker I linked to above.

In [1]: from bokeh.plotting import figure

In [2]: p = figure()

In [3]: p.xaxis.ticker
Out[3]: BasicTicker(id='1012', ...)


But it’s ok since I won’t zoom between two points.

I guess I should also mention that ranges have a min_interval that can be set to enforce this.

Awesome! Thank you very much. :slight_smile: