Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bokeh multiline make one line invisible

I would like to be able to select a single line from a Bokeh multi_line plot. I can only find a way to select all multi-lines at once. My example works by using p.line instead, but that's really slow if you have a lot of lines. Here's an example:

import pandas as pd
import numpy as np
from bokeh.palettes import Spectral11
from bokeh.plotting import figure, show, output_file
output_file('temp.html')

toy_df = pd.DataFrame(data=np.random.rand(5,3), columns = ('a', 'b' ,'c'), index = pd.DatetimeIndex(start='01-01-2015',periods=5, freq='d'))   
print(toy_df)
numlines=len(toy_df.columns)
mypalette=Spectral11[0:numlines]

p = figure(width=500, height=300, x_axis_type="datetime") 

line1 = p.line(toy_df.index,toy_df["a"])
line2 = p.line(toy_df.index,toy_df["b"])
line3 = p.line(toy_df.index,toy_df["c"])
line3.visible=False
show(p)

The previous code perfectly creates three lines and makes the third one invisible. For efficiency reasons, I would like replace line1, line2, line3 with a multi-line:

p.multi_line(xs=[toy_df.index.values]*numlines,
             ys=[toy_df[name].values for name in toy_df],
             line_color=mypalette,
             line_width=5)

However, I cannot find a way to select one of the individual lines and to make it invisible. I also tried it using custom javascript, but that didn't provide me a solution either.

like image 596
Guido Avatar asked Jan 19 '26 14:01

Guido


1 Answers

One way is to set alpha to zero for the line you want to hide:

p.multi_line(xs=[toy_df.index.values]*numlines,
             ys=[toy_df[name].values for name in toy_df],
             line_color=mypalette,
             line_width=5,
             line_alpha=[1,0,1]) # hides second line

Alternatively, you can make the line_color value be None for the line you want to hide.

like image 118
bigreddot Avatar answered Jan 22 '26 02:01

bigreddot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!