Search code examples
pythonbokeh

How to start graph lines at 0 in the Y axis with Bokeh (Python)


I'm using Bokeh for showing line graphs at my Django/Python web.

By default, the graphs start at the minimum value provided, but I want them to start always at 0 in the Y-axis.

For example, in the following example it starts at 167 in the Y-axis (the minimum value in that data set), but I wanted to start at 0.

graph

y_range seems to work fine if I want to define a minimum and a maximum, but I only want to define the minimum (0) and let the data "decide" the maximum.

I've tried using y_range=(0, None), min_border=0, start=0 and a bunch of other things, without success. ChatGPT keeps recommending me alternatives that don't really work or even exist.

This is my current code:

y = WHATEVER
x = WHATEVER  

plot = figure(title='TITLE', 
        x_axis_type='datetime',
        sizing_mode="stretch_width", 
        max_width=600, 
        height=400)

plot.line(x, y, line_width=4)
script, div = components(plot)

Solution

  • ChatGPT keeps recommending me alternatives that don't really work or even exist.

    This should not be surprising, ChatGPT is not a serious or reliable source of accurate information.

    In any case, the only thing you need to do is set:

    plot.y_range.start = 0
    

    with the default range (i.e. don't pass a range value to figure). That will keep auto-ranging for the upper y-axis but pin the start to 0.