英文:
Holoviews Hovertool show extra row?
问题
以下是您要翻译的内容:
我有一个数据集,我想要在X轴上制作两个不同的变量的图表(在两个不同的图表中),但我想要将另一个值放入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)
我想要制作一个图表,其中`item_id`位于X轴上,并且`start`日期也位于X轴上。我想要跟踪每个站点的拒绝情况以及总装的组合。
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")
[![使用start作为X轴的数据绘制][1]][1]
> plot(data_reduced, x_axis="item_id")
[![输入图像说明][2]][2]
如何填充`???`?
如果我想从单个线条中获取数据(`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
所以数据似乎在源中
[1]: https://i.stack.imgur.com/1iQBu.png
[2]: https://i.stack.imgur.com/YKFJh.png
希望这对您有所帮助!如果您需要进一步的帮助,请随时告诉我。
英文:
I have a dataset where I want to plot make plots with 2 different variables on the X-axis (in 2 different plots), but I want to get the other value into the 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)
I want to produce a graph with the item_id
on the x-axis and with the start
date on the x-axis. I want to track the rejects per station, and the combined of the assembly.
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
),
)
And then I make the graphs with plot(data_reduced, x_axis="start")
or plot(data_reduced, x_axis="item_id")
> plot(data_reduced, x_axis="start")
> plot(data_reduced, x_axis="item_id")
How do I get the ???
filled in?
If I want to get the data from an individual line (list(p.items())[0][1].data
), I get:
,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
So the data seems to be in the source
1: https://i.stack.imgur.com/1iQBu.png
2: https://i.stack.imgur.com/YKFJh.png
答案1
得分: 4
在这种情况下,我更喜欢使用hvplot,它是建立在holoviews之上的库,由相同的开发人员组创建。我认为这确实让生活变得更加容易,一次性创建您的图表。
1) 使用Hvplot,您可以轻松指定额外的悬停列,关键词为hover_cols=['your_column']:
# 导入这个库后,您可以在数据框上使用.hvplot()创建交互式的holoviews图表
import hvplot.pandas
item_plot = data_reduced.hvplot(
kind='scatter',
x='item_id',
y='rejects',
by='station', # 这创建了叠加图
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,
)
- 如果您想要一个纯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']))
带有额外悬停列的示例图:
英文:
In cases like this I prefer to use hvplot which is a library built on top of holoviews, made by the same group of developers. This really makes life I think a lot easier and creates your plot all in one go.<br>
1) With Hvplot you can specify extra hover columns easily with keyword 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,
)
<br>
2) If you want a pure Holoviews solution, you can do:
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']))
<br>
Example plot with extra hover columns:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论