Python callback function not working as expected for bokeh server plot

  Kiến thức lập trình

I have just started using Bokeh to make a data viewing platform. In order to make it so the y axis adjusts according to the maximum and minimum points on the screen, I have written a Python callback function which will get executed each time there is a range update.

def t():
    dates = source.data["x"]
    data = source.data["y"]
    start = list(dates).index(min(dates,key=lambda x:abs(x-dt.datetime.fromtimestamp(p.x_range.start / 1000))))
    end = list(dates).index(min(dates,key=lambda x:abs(x-dt.datetime.fromtimestamp(p.x_range.end / 1000))))
    mn = float("inf")
    mx = -float("inf")
    for i in range(start, end + 1):
        mx = max(data[i], mx)
        mn = min(data[i], mn)
    pad = (mx - mn) * 0.09
    p.y_range.start = mn - pad
    p.y_range.end = mx + pad
    print(mn,mx)

where source is a ColumnDataSource, x are datetimes every two minutes and y are floats.

When running my script the autoscaling functions as intended nearly all of the time except I get instances like this:

Example

I have tried debugging this by printing the mn and mx values and it looks like when this issue occurs mx is the value of the second highest point on the screen.

Is there something wrong with my function? Or is the issue most likely occurring elsewhere in my code?

LEAVE A COMMENT