Tobias Hotzenplotz Tobias Hotzenplotz - 2 months ago 13x
Python Question

Infinite horizontal line in Bokeh

Is there a way to plot an infinite horizontal line with Bokeh?
The endpoints of the line should never become visible, no matter how far out the user is zooming.

This is what I've tried so far. It just prints an empty canvas:

import bokeh.plotting as bk
import numpy as np

p = bk.figure()
p.line([-np.inf,np.inf], [0,0], legend="y(x) = 0")

One way would be to set the endpoints extremely high/low and the figure's x_range and y_range very small in relation to them.

import bokeh.plotting as bk
import numpy as np

p = bk.figure(x_range=[-10,10])
p.line([-np.iinfo(np.int64).max, np.iinfo(np.int64).max], [0,0], legend="y(x) = 0")

However, I am hoping that somebody has a more elegant solution.

Edit: removed outdated solution


You are looking for "spans":

Spans (line-type annotations) have a single dimension (width or height) and extend to the edge of the plot area.

Please, take a look at

So, the code will look like:

import numpy as np
import bokeh.plotting as bk
from bokeh.models import Span

p = bk.figure()

# Vertical line
vline = Span(location=0, dimension='height', line_color='red', line_width=3)
# Horizontal line
hline = Span(location=0, dimension='width', line_color='green', line_width=3)

p.renderers.extend([vline, hline])

With this solution users are allowed to pan and zoom at will. The end of the lines will never show up.