如何用全息图可视化时间跨度?

How to visualize time span with holoviews?

我最近在 python 中发现 holoviews 是一个很有前途的可视化库,并且 - 作为一种实践 - 我想转移我现有的一些代码以查看它在 [=10= 中的样子].我到达了一个我无法重新创建的给定地块。

我想可视化一个时间线或路线图,其中 x 轴的类型为 pd.datetimey 是分类的。像这样:

我应该使用什么 Element?我该如何定义部分的 startend 位置?

听起来您需要一个甘特图
我正在使用 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 轴的范围。

示例代码为您提供此图:

另一个解决方案 是一种更通用的生成此甘特图的方法,它遍历数据并为每个 start/stop 点生成一条曲线:

# 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='',
)

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)