如何使用Bokeh 2.4.3绘制Pandas数据帧的选定列

huangapple go评论73阅读模式
英文:

How to plot selected columns of a Pandas dataframe using Bokeh 2.4.3

问题

以下是您要翻译的部分:

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)
英文:

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)

答案1

得分: 2

为了显示多条线,您需要多次使用p.line(),每次使用不同的列。请注意,在下面更新的代码中,我还使用了color来显示多条线。此外,我将数据框的行数减少到10,以便您可以清楚地看到这些线。希望这符合您的要求...

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)

输出图表

如何使用Bokeh 2.4.3绘制Pandas数据帧的选定列

英文:

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

如何使用Bokeh 2.4.3绘制Pandas数据帧的选定列

huangapple
  • 本文由 发表于 2023年3月9日 15:50:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定