Holoviews Hovertool 显示额外的行?

Holoviews Hovertool show extra row?

我有一个数据集,我想在其中绘制 X 轴上有 2 个不同变量的图(在 2 个不同的图中),但我想将另一个值输入 Hovertool

from io import StringIO
import pandas as pd

data = """,item_id,start,station,rejects
0,item1,2019-10-14 19:00:00,assembly,4.297994269340974
1,item1,2019-10-14 19:00:00,ST1,0.20546537908362442
2,item1,2019-10-14 19:00:00,ST2,0.494539460127756
3,item1,2019-10-14 19:00:00,ST3,0.6892230576441103
4,item2,2019-10-14 23:30:00,assembly,4.432249894470241
5,item2,2019-10-14 23:30:00,ST1,0.19071837253655435
6,item2,2019-10-14 23:30:00,ST2,0.7651434643995749
7,item2,2019-10-14 23:30:00,ST3,0.7748600947051227
8,item3,2019-10-15 04:00:00,assembly,3.55576079427384
9,item3,2019-10-15 04:00:00,ST1,0.37002775208140615
10,item3,2019-10-19 04:00:00,ST2,0.7195914577530177
11,item3,2019-10-19 04:00:00,ST3,0.492379835873388
12,item4,2019-10-19 10:30:00,assembly,4.02656704026567
13,item4,2019-10-19 10:30:00,ST1,0.22926219258024177
14,item4,2019-10-19 10:30:00,ST2,0.690376569037657
15,item4,2019-10-19 10:30:00,ST3,0.838745695410320"""

data_reduced = pd.read_csv(StringIO(data), parse_dates=["start"], index_col=0)

我想生成一个 X 轴为 item_id 且 X 轴为 start 日期的图表。我想跟踪每个工位的废品,以及装配的总和。

import holoviews as hv
import bokeh
from holoviews import opts
hv.extension('bokeh')
bokeh.plotting.output_notebook()

def plot(data_reduced, x_axis="item_id"):
    x_label = x_axis if x_axis in {"start", "item_id"} else "item_id"
    key_dimensions = [(x_label, x_label), ("station", "station")]
    value_dimensions = [
        ("rejects", "rejects"),
        ("start", "start"),
        ("item_id", "item_id"),
        ("start", "start"),
    ]

    datatable = hv.Table(
        data_reduced, kdims=key_dimensions, vdims=value_dimensions
    )
    scatter_plot = datatable.to.scatter(x_label, ["rejects"])
    overlay = scatter_plot.overlay("station")

    tooltips = [
        ("item_id", "@item_id"),
        ("start", "@start{%Y-%m-%d %H:%M}"),
        ("station", "@station"),
        ("rejects", "@rejects"),
    ]
    hover = bokeh.models.HoverTool(
        tooltips=tooltips, formatters={"start": "datetime"}
    )

    return overlay.opts(
        opts.Scatter(
            color=hv.Cycle("Category10"),
            show_grid=True,
            padding=0.1,
            height=400,
            tools=[hover],
        ),
        opts.NdOverlay(
            legend_position="right", show_frame=False, xrotation=90
        ),
    )

然后我用 plot(data_reduced, x_axis="start")plot(data_reduced, x_axis="item_id")

制作图表
plot(data_reduced, x_axis="start")

plot(data_reduced, x_axis="item_id")

如何填写 ???

如果我想从单个行 (list(p.items())[0][1].data) 获取数据,我得到:

,item_id,start,station,rejects
1,item1,2019-10-14 19:00:00,ST1,0.2054653790836244
5,item2,2019-10-14 23:30:00,ST1,0.19071837253655435
9,item3,2019-10-15 04:00:00,ST1,0.37002775208140615
13,item4,2019-10-19 10:30:00,ST1,0.22926219258024175

因此数据似乎在源中

在这种情况下,我更喜欢使用 hvplot,这是一个建立在全息视图之上的库,由同一组开发人员制作。我认为这真的让生活变得更轻松,并且一次性创建您的情节。

1) 使用 Hvplot,您可以使用关键字 hover_cols=['your_column']:

轻松指定额外的悬停列
# with this import you can use .hvplot() on your df and create interactive holoviews plots
import hvplot.pandas

item_plot = data_reduced.hvplot(
    kind='scatter',
    x='item_id',
    y='rejects',
    by='station',  # this creates the overlay
    hover_cols=['start'],
    padding=0.1,
)

start_plot = data_reduced.hvplot(
    kind='scatter',
    x='start',
    y='rejects',
    by='station',
    hover_cols=['item_id'],
    padding=0.1,
)


2) 如果你想要一个 纯 Holoviews 解决方案,你可以这样做:

import holoviews as hv
from holoviews import opts

hv_df = hv.Dataset(
    data_reduced,
    kdims=['item_id', 'station'], 
    vdims=['rejects', 'start'],
)

hv_df.to(hv.Scatter).overlay().opts(opts.Scatter(tools=['hover']))


带有额外悬停列的示例图: