带有颜色条的 plotly 甘特图
plotly gantt chart with colorbar
我正在制作像这里一样的甘特图 https://plot.ly/python/gantt/#index-by-numeric-variable
但是,我想用作索引的数字变量是一个远大于 100 的正数。
当我使用示例代码时,颜色条限制为 [0,100],导致条形全部为 100 的颜色。
有没有办法提升使用示例中的代码创建的绘图甘特图中的最大值?
我希望颜色 "proportional" 到索引的值。
这是示例代码:
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
根据文档,它应该在给定数字索引时缩放。但是,查看代码,似乎 min/max 值已被硬编码为 0 和 100。请参阅 code 第 285 和 286 行。
一种解决方法是在 0-100 之间缩放索引并手动将颜色条的标签设置为原始 min/max:
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()
请注意,如上面的评论所述,您可能需要注意条形图的工具提示(显示索引值)。这些也可以手动设置,但您需要将 fig.data 的顺序与原始数据帧的顺序相匹配。
我正在制作像这里一样的甘特图 https://plot.ly/python/gantt/#index-by-numeric-variable 但是,我想用作索引的数字变量是一个远大于 100 的正数。 当我使用示例代码时,颜色条限制为 [0,100],导致条形全部为 100 的颜色。 有没有办法提升使用示例中的代码创建的绘图甘特图中的最大值?
我希望颜色 "proportional" 到索引的值。
这是示例代码:
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
根据文档,它应该在给定数字索引时缩放。但是,查看代码,似乎 min/max 值已被硬编码为 0 和 100。请参阅 code 第 285 和 286 行。
一种解决方法是在 0-100 之间缩放索引并手动将颜色条的标签设置为原始 min/max:
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()
请注意,如上面的评论所述,您可能需要注意条形图的工具提示(显示索引值)。这些也可以手动设置,但您需要将 fig.data 的顺序与原始数据帧的顺序相匹配。