Python bokeh: Multiple color segments on same line
<p>I am using python 3.5 and bokeh 0.12.1, and I am trying to plot a simple line with multiple colors on separate segments. Basically I want the line to have different colors based on a column value.
Here is a simplified version of my code:</p>

<pre><code>import numpy as np
from numpy import vectorize
import pandas as pd
from bokeh.plotting import figure, show, output_file


def f(x):
    return 2 * x


def color(x):
    if x &lt; 20:
        return 0
    if 20 &lt;= x &lt; 60:
        return 1
    if 60 &lt;= x &lt; 80:
        return 0
    else:
        return 1


v_color = vectorize(color)
x = np.arange(0, 100, 1)
data = {'x': x, 'y': f(x), 'colors': v_color(x)}
df = pd.DataFrame(data=data)
# print(df)

p = figure(title="Line example")

p.line(df['x'], df['y'], legend="y=f(x)",
       # line_color="tomato",
       line_color="olivedrab",
       line_width=2)

p.legend.location = "top_left"

output_file("basic_line_test.html", title="line plot example")

show(p)  # open a browser
</code></pre>

<p>Basically the line should have one color, let's say 'olivedrab' when the column 'colors' is 0 and 'tomato' when the value is 1.
How can I do that?</p>

<p>As of Bokeh <code>0.12.1</code> this is not currently supported. Lines can only have one color at a time. Your next best bet is to try the <code>multi_line</code> or <code>segments</code> glyph functions, but doing that will be a bit more verbose (you will have to compute and provide the start/end points of eery individual segment). </p>

<p>It's possible this could be added as a feature in some future release, I'd encourage you to submit a feature request on the project GitHub <a href="https://github.com/bokeh/bokeh/issues" rel="nofollow">issue tracker</a>.</p>

python bokeh