1

我最近遇到了holoviews作为 python 中很有前途的可视化库,并且作为一种实践,我想转移一些我现有的代码来看看它在hv. 我到达了一个我无法重新创建的给定情节。

我想可视化一个时间轴或路线图,其中x轴的类型是类型pd.datetime并且y是分类的。像这样的东西:

在此处输入图像描述

我应该使用什么元素?我应该如何定义部分的开始结束位置?

4

2 回答 2

3

HoloViews >= 1.13 现在有hv.Segments()
这使得创建甘特图变得更加容易:

from datetime import datetime
import holoviews as hv
hv.extension('bokeh')

data = {
    'start': [datetime(2018, 1,  3), datetime(2018, 3,  3), datetime(2018, 7, 15)],
    'end': [datetime(2018, 1, 31), datetime(2018, 3, 30), datetime(2018, 9, 23)],
    'start_event': ['Event1', 'Event2', 'Event2'],
    'end_event': ['Event1', 'Event2', 'Event2'],   
}

df = pd.DataFrame(data)

hv.Segments(
    data=df,
    kdims=['start', 'start_event', 'end', 'end_event']
).opts(line_width=4., width=1000)

带有 HoloViews 段的甘特图

于 2020-05-03T19:16:44.303 回答
2

这听起来像你需要一个甘特图
我正在使用hv.Curve()从时间起点到终点画一条线:

# import libraries
from datetime import datetime
import holoviews as hv
hv.extension('bokeh')

# drawing a simple line
line1 = hv.Curve([
    [datetime(2018, 1,  3), 'Event1'],
    [datetime(2018, 1, 31), 'Event1'],
])

# creating a dashed line
line2 = hv.Curve([
    [datetime(2018, 3,  3), 'Event2'],
    [datetime(2018, 3, 30), 'Event2'],
]).opts(line_dash='dashed')

# creating a black thicker line
line3 = hv.Curve([
    [datetime(2018, 7, 15), 'Event2'],
    [datetime(2018, 9, 23), 'Event2'],
]).opts(color='black', line_width=10.0)

# add all 3 lines together on 1 plot with the * symbol
# this creates an overlay
gantt_chart = (line1 * line2 * line3)

# change xlim and width of the whole chart and add xlabel
# just making the plot a bit nicer
gantt_chart.opts(
    width=800, 
    xlim=(datetime(2018, 1, 1), datetime(2018, 12, 31)),
    xlabel='start to stop',
    ylabel='',
)

如您所见,我还添加了一些选项作为示例来更改线条的样式并更改 x 轴的范围。

示例代码为您提供了此图: 在此处输入图像描述

另一种解决方案是生成此甘特图的更通用方法,它循环数据并为每个开始/停止点生成曲线:

# import libraries
from datetime import datetime
import holoviews as hv
hv.extension('bokeh')

# create sample data
data = [
    [datetime(2018, 1, 3), datetime(2018, 1, 31), 'Event1'],
    [datetime(2018, 3, 3), datetime(2018, 3, 30), 'Event2'],   
    [datetime(2018, 7, 15), datetime(2018, 9, 23), 'Event2'],
]

# loop over data to create a list with a line/curve for every start/stop point
all_lines_list = [
    hv.Curve([
        [row[0], row[2]],
        [row[1], row[2]],
    ]).opts(color='red') for row in data
]

# put all lines in an Overlay plot that can display all lines on 1 plot
gantt_chart = hv.Overlay(all_lines_list)

# make plot nicer
gantt_chart.opts(
    show_legend=False,
    width=800,
    xlim=(datetime(2018, 1, 1), datetime(2018, 12, 31)),
    xlabel='start to stop',
    ylabel='',
)
于 2019-10-18T14:12:49.603 回答