I would like to give a pandas dataframe to Bokeh to plot a line chart with multiple lines.
The x-axis should be the df.index and each df.columns should be a separate line.
This is what I would like to do:
import pandas as pd
import numpy as np
from bokeh.plotting import figure, show
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'))
p = figure(width=1200, height=900, x_axis_type="datetime")
p.multi_line(df)
show(p)
However, I get the error:
RuntimeError: Missing required glyph parameters: ys
Instead, I've managed to do this:
import pandas as pd
import numpy as np
from bokeh.plotting import figure, show
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'))
ts_list_of_list = []
for i in range(0,len(toy_df.columns)):
ts_list_of_list.append(toy_df.index)
vals_list_of_list = toy_df.values.T.tolist()
p = figure(width=1200, height=900, x_axis_type="datetime")
p.multi_line(ts_list_of_list, vals_list_of_list)
show(p)
That (ineligantly) does the job but it uses the same color for all 3 lines, see below:
Questions:
1) How can I pass a pandas dataframe to bokeh's multi_line?
2) If not possible directly, how can I manipulate the dataframe data so that multi_line will create each line with a different color?
Thanks in advance.
See Question&Answers more detail:
os