【问题标题】:How do I add a vertical line to a plotly gantt chart using pandas dataframe?如何使用 pandas 数据框向绘图甘特图添加垂直线?
【发布时间】:2019-01-29 15:59:58
【问题描述】:

我有一个包含Taskstring 类型)、Period_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)

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

【问题讨论】:

    标签: python pandas plotly


    【解决方案1】:

    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)
    

    【讨论】:

      【解决方案2】:

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

      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 数据框。

      【讨论】:

      • 你的回答有点混乱。你能解决这个问题吗?
      • 重读我的回答后,我明白措辞有点混乱。我对其进行了编辑,希望现在很清楚这为我解决了这个问题。
      猜你喜欢
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      相关资源
      最近更新 更多