Reputation: 105
I was consulting the bokeh user guide (link) on creating interactive legends (see subsection on "Hiding glyphs").
The code given allows me to create up to 4 stacked line charts. For example, if I try to create a fifth line chart ("AAP"):
p = figure(width=800, height=250, x_axis_type="datetime")
p.title.text = 'Click on legend entries to hide the corresponding lines'
import pandas as pd
from bokeh.palettes import Spectral4
from bokeh.plotting import figure, show
from bokeh.sampledata.stocks import AAPL, GOOG, IBM, MSFT
for data, name, color in zip([AAPL, GOOG, IBM, MSFT, AAPL], ["AAPL", "GOOG", "IBM", "MSFT", "AAP"], Spectral4):
df = pd.DataFrame(data)
df['date'] = pd.to_datetime(df['date'])
p.line(df['date'], df['close'], line_width=2, color=color, alpha=0.8, legend_label=name)
p.legend.location = "top_left"
p.legend.click_policy="hide"
show(p)
it will still only show four.
I was wondering if it is possible to extend this code and create many more line charts (say 20) and if so, how this can be done.
Thank you
Upvotes: 1
Views: 128
Reputation: 6337
Your approach is working fine and would work, if you use Spectral5
instead of Spectral4
as palette. You have to know, that zip iterates over all lists and stops if one list has no more items. That was the case.
Minimal working example:
import pandas as pd
from bokeh.plotting import figure, show, output_notebook
from bokeh.sampledata.stocks import AAPL, GOOG, IBM, MSFT
output_notebook()
p = figure(width=800, height=250, x_axis_type="datetime")
p.title.text = 'Click on legend entries to hide the corresponding lines'
dataset = [AAPL, GOOG, IBM, MSFT, AAPL]
nameset = ["AAPL", "GOOG", "IBM", "MSFT", "AAP"]
colorset = ['blue', 'red', 'green', 'magenta', 'black']
for data, name, color in zip(dataset, nameset, colorset):
df = pd.DataFrame(data)
df['date'] = pd.to_datetime(df['date'])
p.line(df['date'], df['close'], line_width=2, color=color, alpha=0.8, legend_label=name)
p.legend.location = "top_left"
p.legend.click_policy="hide"
show(p)
Output:
Comment
For this I used bokeh 2.4.3 but with bokeh 3.+ this should work, too.
Upvotes: 2