要使用Bokeh库绘制多条线的折线图,并进行灵活的映射,可以按照以下步骤进行:
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
p = figure()
source = ColumnDataSource(data=dict(
x=[1, 2, 3, 4, 5],
y1=[2, 4, 6, 8, 10],
y2=[1, 3, 5, 7, 9],
y3=[10, 8, 6, 4, 2]
))
# 绘制第一条线
p.line('x', 'y1', source=source, line_color='red', line_width=2)
# 绘制第二条线
p.line('x', 'y2', source=source, line_color='green', line_width=2)
# 绘制第三条线
p.line('x', 'y3', source=source, line_color='blue', line_width=2)
show(p)
完整的示例代码如下所示:
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
p = figure()
source = ColumnDataSource(data=dict(
x=[1, 2, 3, 4, 5],
y1=[2, 4, 6, 8, 10],
y2=[1, 3, 5, 7, 9],
y3=[10, 8, 6, 4, 2]
))
p.line('x', 'y1', source=source, line_color='red', line_width=2)
p.line('x', 'y2', source=source, line_color='green', line_width=2)
p.line('x', 'y3', source=source, line_color='blue', line_width=2)
show(p)
运行上述代码,将会显示一个包含三条线的折线图,每条线分别用不同的颜色表示。
上一篇:Bokeh - Div 不更新