gpt4 book ai didi

python - Holoviews Hovertool 显示额外行?

转载 作者:行者123 更新时间:2023-12-01 06:37:17 26 4
gpt4 key购买 nike

我有一个数据集,我想在 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")

data plotted with start on x-axis

plot(data_reduced, x_axis="item_id")

enter image description here

如何填写???

如果我想从单独的行获取数据 (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这是一个建立在 Holoviews 之上的库,由同一组开发人员制作。我认为这确实让生活变得更加轻松,并且一次性创建了你的情节。

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']))


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

using hvplot to create plot overlay with extra hover columns

关于python - Holoviews Hovertool 显示额外行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59609911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com