如何使用pandas数据帧将垂直线添加到绘图甘特图中



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

我已经根据plotly的官方文档尝试了基本的方法。

这是我的代码:

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)

不幸的是,添加这个特定的布局并没有改变我的图表中的任何内容。有什么帮助吗?

这篇Plotly社区文章中提供的提示在这里可能会有所帮助。

试试看:

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)

我知道这是一篇旧帖子,但我想做同样的事情,但找不到对我有用的答案。roborative提到的Plotly社区帖子也没有包含对我有效的答案。但经过一些实验,我找到了一个有效的解决方案:

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是Pandas数据帧。

最新更新