Hi,
I’m trying to plot multiple lines from a pandas dataframe that has gone though a “melt” transformation. This converts the dataframe from a wide format to a long format. I can’t figure out how to get the y= portion of the line to reference the correct set of data when the column data source is arranged this way.
I’m melting my original dataframe so that I can get the original column names as a value in a hover tool that is unique for each line. This will be used in a bokeh server application where I need to be able to update the column data source using a line of code similar to this: source.data.update(ColumnDataSource(melted_df)). Maybe there is a better way to accomplish this without melting the dataframe?
Some example code is below:
import pandas as pd
from bokeh.plotting import figure, ColumnDataSource
from bokeh.io import show, reset_output
from bokeh.models import HoverTool
reset_output()
raw_data = {‘time’: [0,1,2,3,4,5],
‘var1’: [5,6,7,8,9,10],
‘var2’: [6,7,8,9,10,11],
‘var3’: [7,8,9,10,11,12]}
df=pd.DataFrame(raw_data)
melted_df=df.melt(id_vars=‘time’)
cds=ColumnDataSource(data=melted_df)
plot=figure()
plot.line(x=‘time’, y=‘var1’, source=cds)
plot.line(x=‘time’, y=‘var2’, source=cds)
plot.line(x=‘time’, y=‘var3’, source=cds)
hover = HoverTool(tooltips=[
(‘Time’,’@time’),
(‘Variable’,’@variable’),
(‘Value’,’@value’)
])
plot.add_tools(hover)
show(plot)
Thanks!
Willis