如何更改图例栏和颜色以分别显示 Dataframe 中的每一行?我当前的图表如下所示:
代码如下:
def chart(task_list, filename):
fig = ff.create_gantt(task_list, colors='Rainbow', index_col='Resource', show_colorbar=True, group_tasks=True)
# plot(fig, filename=filename)
return plot(fig, filename=filename, include_plotlyjs=False, output_type='div')
#print(fig)
' Rainbow ' 是默认的绘图色标。但是,如果我没记错的话,它仅限于两种颜色。
我只希望图表有一个与此类似的图例(无论是点还是线,而不是像当前甘特图那样的渐变条):
代码如下:
df = [dict(Task="Job A", Start='2016-01-01', Finish='2016-01-02', Resource='Apple'),
dict(Task="Job B", Start='2016-01-02', Finish='2016-01-04', Resource='Grape'),
dict(Task="Job C", Start='2016-01-02', Finish='2016-01-03', Resource='Banana')]
colors = dict(Apple = 'rgb(220, 0, 0)',
Grape = 'rgb(170, 14, 200)',
Banana = (1, 0.9, 0.16))
fig = ff.create_gantt(df, colors=colors, index_col='Resource', show_colorbar=True)
py.iplot(fig, filename='gantt-dictioanry-colors', world_readable=True)
但是,当我使用文档中的代码时,它只有有限数量的颜色,由 dict colors设置。我希望每当 Dataframe 中出现新行时,颜色都能够自行生成。
TLDR;需要一个 COLORSCALE,它能够适应 Dataframe 中越来越多的行,并将渐变条更改为线/点/任何东西,以作为每个 Dataframe 行的 LEGENDS,根据 index_col。
试试这个
Resource
在将其传递给def chart(task_list, filename)
So之前更改为字符串,这plot.ly
将Resource
作为离散类别而不是连续度量。
task_list.Resource = task_list.Resource.apply(str)
import random
all_the_colors = list((x,y,z) for x in range(256) for y in range(256) for z in range(256))
colors = [f"rgb({random.choice(all_the_colors)})" for x in df.Resource.unique()]
def chart(task_list, filename):
fig = ff.create_gantt(task_list, colors=colors, index_col='Resource', show_colorbar=True, group_tasks=True)
# plot(fig, filename=filename)
return plot(fig, filename=filename, include_plotlyjs=False, output_type='div')
#print(fig)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句