Infinite horizontal line in Bokeh

Question:

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")
bk.show(p)

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")
bk.show(p)

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

Edit: removed outdated solution

Asked By: Tobias Hotzenplotz

||

Answers:

If you plot two rays from the middle they won’t get smaller as you zoom in or out since the length is in pixel. So something like this:

p.ray(x=[0],y=[0],length=300, angle=0, legend="y(x) = 0")
p.ray(x=[0],y=[0],length=300, angle=np.pi, legend="y(x) = 0")

But if the user pans in either direction the end of the ray will show up. If you can prevent the user from panning at all (even when they zoom) then this is a little nicer code for a horizontal line.

If the user is able to zoom and pan anywhere they please, there is no good way (as far as I can tell) to get a horizontal line as you describe.

Answered By: Alejandro

The Bokeh documentation on segments and rays indicates the following solution (using ray):

To have an “infinite” ray, that always extends to the edge of the
plot, specify 0 for the length.

And indeed, the following code produces an infinite, horizontal line:

import numpy as np
import bokeh.plotting as bk
p = bk.figure()
p.ray(x=[0], y=[0], length=0, angle=0, line_width=1)
p.ray(x=[0], y=[0], length=0, angle=np.pi, line_width=1)
bk.show(p)
Answered By: Eike P.

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
http://docs.bokeh.org/en/latest/docs/user_guide/annotations.html#spans

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])
bk.show(p)

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

Answered By: Daniel Perez-Gil

In case you are wondering how to use spans in combination with time series, convert your dates to unix timestamps:

start_date = time.mktime(datetime.date(2018, 3, 19).timetuple())*1000
vline = Span(location=start_date,dimension='height', line_color='red',line_width=3)

Or see this link for a full example.

Answered By: Guido
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.