多图例#

本示例展示了一个带有多个图例的图表。这可以用来描述两个数据集之间的比较。该图表使用股票数据集。

详细信息

示例数据:

bokeh.sampledata.stocks

Bokeh API:

figure.multi_line, figure.add_layout

关键词:

图例,股票

import numpy as np

from bokeh.models import Legend, LegendItem
from bokeh.plotting import figure, show
from bokeh.sampledata.stocks import AAPL, MSFT


def datetime(x):
    return np.array(x, dtype=np.datetime64)


p = figure(background_fill_color="#fafafa", x_axis_type="datetime",
           width=800, height=350)

r = p.multi_line([datetime(AAPL['date']), datetime(MSFT['date'])],
                 [AAPL['adj_close'], MSFT['adj_close']],
                 color=["navy", "crimson"], line_width=2, alpha=0.6)

legend = Legend(items=[
    LegendItem(label="AAPL", renderers=[r], index=0),
    LegendItem(label="MSFT", renderers=[r], index=1),
], location="top_left")
p.add_layout(legend)

show(p)