How to plot selected columns of a Pandas dataframe using Bokeh 2.4.3
Question:
For future visitors: My original question was closed as a duplicate, but the old ‘multiline’ function in the linked answer is depreciated years ago.
I would like to plot a selection of pandas columns using bokeh, i.e. multiple lines in one chart. Code showing one column/line works:
import pandas as pd
import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
df = pd.DataFrame(np.random.randint(0,100,size=(100, 5)), columns=list('ABCDF'))
sss = ColumnDataSource(df)
p = figure(height=300, width=300)
x = df.index
y = df[['B']]
p.line(x, y)
show(p)
Now, how do I plot columns A, C and F in the same line chart?
I tried this, but does not work:
x = df.index
y = df[['A', 'C', 'F']]
p.line(x, y)
show(p)
Answers:
To show multiple lines, you need to use the p.line()
multiple times with different columns. Note that in the below updated code, I have used color
as well to show multiple lines. Also, reduced the df rows to 10 so you can see the lines clearly. Hope this is what you are looking for…
import pandas as pd
import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
df = pd.DataFrame(np.random.randint(0,100,size=(10, 5)), columns=list('ABCDF'))
sss = ColumnDataSource(df)
p = figure(height=300, width=300)
p.line(x=df.index, y = df.A, color="blue")
p.line(x=df.index, y = df.B, color="red")
p.line(x=df.index, y = df.C, color="green")
p.line(x=df.index, y = df.F, color="black")
show(p)
Output plot
For future visitors: My original question was closed as a duplicate, but the old ‘multiline’ function in the linked answer is depreciated years ago.
I would like to plot a selection of pandas columns using bokeh, i.e. multiple lines in one chart. Code showing one column/line works:
import pandas as pd
import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
df = pd.DataFrame(np.random.randint(0,100,size=(100, 5)), columns=list('ABCDF'))
sss = ColumnDataSource(df)
p = figure(height=300, width=300)
x = df.index
y = df[['B']]
p.line(x, y)
show(p)
Now, how do I plot columns A, C and F in the same line chart?
I tried this, but does not work:
x = df.index
y = df[['A', 'C', 'F']]
p.line(x, y)
show(p)
To show multiple lines, you need to use the p.line()
multiple times with different columns. Note that in the below updated code, I have used color
as well to show multiple lines. Also, reduced the df rows to 10 so you can see the lines clearly. Hope this is what you are looking for…
import pandas as pd
import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
df = pd.DataFrame(np.random.randint(0,100,size=(10, 5)), columns=list('ABCDF'))
sss = ColumnDataSource(df)
p = figure(height=300, width=300)
p.line(x=df.index, y = df.A, color="blue")
p.line(x=df.index, y = df.B, color="red")
p.line(x=df.index, y = df.C, color="green")
p.line(x=df.index, y = df.F, color="black")
show(p)
Output plot