【问题标题】:plotly gantt chart with colorbar带有彩条的情节甘特图
【发布时间】:2020-05-09 00:00:51
【问题描述】:

我正在制作一个甘特图,像这里https://plot.ly/python/gantt/#index-by-numeric-variable 但是,我想用作索引的数字变量是一个远大于 100 的正数。 当我使用示例代码时,颜色条限制为 [0,100],导致条的颜色全部为 100。 有没有办法提升使用示例中的代码创建的情节甘特图中的最大值?

我希望颜色与索引值“成比例”。

这是示例代码:

import plotly.figure_factory as ff

df = [dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Complete=10),
      dict(Task="Job B", Start='2008-12-05', Finish='2009-04-15', Complete=60),
      dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Complete=95)]

fig = ff.create_gantt(df, colors='Viridis', index_col='Complete', show_colorbar=True)
fig.show()

明确一点:在我的例子中,变量 Complete 可以达到高达 700000 的值

【问题讨论】:

    标签: python python-3.x plotly gantt-chart


    【解决方案1】:

    根据文档,它应该在给出数字索引时进行缩放。但是,查看代码,似乎最小/最大值已被硬编码为 0 和 100。请参阅 code 第 285 和 286 行。

    一种解决方法是在 0-100 之间缩放您的索引,并手动将颜色条的标签设置为原始最小值/最大值:

    import warnings
    warnings.filterwarnings('ignore', category=FutureWarning)  # plotly returns a FutureWarning due to using .ix
    
    from sklearn import preprocessing
    import plotly.figure_factory as ff
    
    # create a dataframe for easier processing wrt the scaled index
    df = pd.DataFrame([dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Complete=10),
          dict(Task="Job B", Start='2008-12-05', Finish='2009-04-15', Complete=60),
          dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Complete=995)])
    
    # scale index (0-100) and add as new column
    min_max_scaler = preprocessing.MinMaxScaler()
    index_scaled = min_max_scaler.fit_transform(df.Complete.values.reshape(-1,1))
    df['index_scaled'] = index_scaled * 100
    
    # create figure based on scaled index
    fig = ff.create_gantt(df, index_col='index_scaled', colors='Viridis', show_colorbar=True)
    
    # set scale of color bar
    fig.data[-1]['marker']['cmin'] = df.Complete.min()
    fig.data[-1]['marker']['cmax'] = df.Complete.max()
    fig.data[-2]['marker']['cmin'] = df.Complete.min()
    fig.data[-2]['marker']['cmax'] = df.Complete.max()
    
    # Due to indexing on the scaled value, the tooltip of the bars shows this values instead of its original.
    # You could iterate over them and adjust, but you'll need to match the sorting of fig.data to the sorting of the dataframe.
    # fig.data[0].name = original_value
    
    fig.show()
    

    注意,如上面 cmets 中所述,您可能需要注意条形图的工具提示(显示索引值)。这些也可以手动设置,但您需要将 fig.data 的顺序与原始数据帧的顺序相匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2022-11-28
      相关资源
      最近更新 更多