英文:
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)
输出图表
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论