如何使用pandas数据框向绘制的甘特图添加垂直线?

时间:2019-01-29 15:59:59

标签: python pandas plotly

我有一个数据集,其中包含列Task(类型为stringPeriod_MinPeriod_Max。我想使用plotly绘制经典的甘特图,并为特殊日子2017-12-31添加一条垂直线。

我已经尝试了与plotly的官方doc一致的基本方法。

这是我的代码:

fig=ff.create_gantt(data_user1,colors['#333F44','#93e4c1'],index_col='Diff_temps')
layout = {
'shapes': [
    # Line Vertical
    {
        'type': 'line',
        'x0': '2017-12-31',
        'y0': data_user1.Task.values[0],
        'x1': '2017-12-31',
        'y1': data_user1.Task.tail(1).values[0],
        'line': {
            'color': 'rgb(55, 128, 191)',
            'width': 3,
        }
    }
]
}

iplot(fig, layout)

很遗憾,添加此特定的布局不会改变我的图表中的任何内容。有帮助吗?

2 个答案:

答案 0 :(得分:0)

Plotly community post中提供的提示在这里可能会有所帮助。

尝试一下:

fig = ff.create_gantt(data_user1, colors['#333F44','#93e4c1'], index_col='Diff_temps')

shapes = [
    # Line Vertical
    {
        'type': 'line',
        'x0': '2017-12-31',
        'y0': data_user1.Task.values[0],
        'x1': '2017-12-31',
        'y1': data_user1.Task.tail(1).values[0],
        'line': {
            'color': 'rgb(55, 128, 191)',
            'width': 3,
        }
    }
]
fig['layout']['shapes'] += shapes

iplot(fig, layout)

答案 1 :(得分:0)

我知道这是一篇过时的文章,但我一直想做同样的事情,却找不到适合我的答案。 Plotly community post提到的roborative也不包含对我有用的答案。但是经过一些试验,我找到了一个可行的解决方案:

import plotly.figure_factory as ff
import plotly.graph_objects as go

fig=ff.create_gantt(data_user1,colors['#333F44','#93e4c1'],index_col='Diff_temps')

fig.add_trace(
    go.Scatter(
        x = ['2017-12-31', '2017-12-31'],
        y = [-1, len(data_user1.index) + 1],
        mode = "lines",
        line = go.scatter.Line(color = "gray", width = 1),
        showlegend = False
    )
)

fig.show()

这假设data_user1是熊猫数据框。