Plotly:如何在甘特图/时间线图中获取定义的颜色条?

Plotly: How to get defined color bars in Gantt / timeline diagram?

我有一个情节 Gantt / timeline diagram using plotly.express and want to specify the color of my bars as in the discrete colors 教程,但我卡住了。无论使用 color_discrete_map 还是 color_discrete_sequence 参数,输出条都是单色的:

discrete_sequence_resource / discrete_sequence_task

相同

discrete_map_resource / discrete_map

相同

如上图所示,条形颜色始终为单色。 如果我只是设置 color="Resource",则条形图会根据资源显示为多色,这正是我想要的。不幸的是,由于稍后的使用,我需要准确指定条形的颜色。

这是我用来生成上面图片的代码:

import pandas as pd
import plotly.express as px
from io import StringIO
import numpy as np

csv = """Task,Start,Finish,Workstation,Resource
1,1970-01-01 01:00:00.000,1970-01-01 01:00:05.400,1,ABL
2,1970-01-01 01:00:00.000,1970-01-01 01:00:01.300,2,ABS
3,1970-01-01 01:00:00.000,1970-01-01 01:00:01.000,3,ABU
4,1970-01-01 01:00:00.000,1970-01-01 01:00:02.000,4,ACC
5,1970-01-01 01:00:02.000,1970-01-01 01:00:03.300,4,ACC
6,1970-01-01 01:00:03.300,1970-01-01 01:00:05.300,4,ACC
7,1970-01-01 01:00:00.000,1970-01-01 01:00:01.300,5,ABS
8,1970-01-01 01:00:00.000,1970-01-01 01:00:01.000,6,ACT
9,1970-01-01 01:00:00.000,1970-01-01 01:00:02.000,7,ACC
10,1970-01-01 01:00:02.000,1970-01-01 01:00:03.300,7,ACC
11,1970-01-01 01:00:03.300,1970-01-01 01:00:05.300,7,ACC
12,1970-01-01 01:00:00.000,1970-01-01 01:00:01.300,8,ABS
13,1970-01-01 01:00:00.000,1970-01-01 01:00:01.000,9,ABU
14,1970-01-01 01:00:00.000,1970-01-01 01:00:01.300,10,ACC
15,1970-01-01 01:00:00.000,1970-01-01 01:00:01.300,11,ABS
16,1970-01-01 01:00:00.000,1970-01-01 01:00:01.000,12,ABU
17,1970-01-01 01:00:00.000,1970-01-01 01:00:01.300,13,ACC
18,1970-01-01 01:00:01.300,1970-01-01 01:00:03.300,13,ACC
19,1970-01-01 01:00:00.000,1970-01-01 01:00:01.300,14,ABS
20,1970-01-01 01:00:00.000,1970-01-01 01:00:01.000,15,ABP
21,1970-01-01 01:00:00.000,1970-01-01 01:00:01.500,16,ABZ
22,1970-01-01 01:00:00.000,1970-01-01 01:00:01.300,17,ACC
23,1970-01-01 01:00:01.300,1970-01-01 01:00:03.300,17,ACC
24,1970-01-01 01:00:00.000,1970-01-01 01:00:01.300,18,ABS
25,1970-01-01 01:00:00.000,1970-01-01 01:00:01.000,19,AAW
26,1970-01-01 01:00:00.000,1970-01-01 01:00:02.000,20,ACC
27,1970-01-01 01:00:02.000,1970-01-01 01:00:03.300,20,ACC
28,1970-01-01 01:00:00.000,1970-01-01 01:00:01.300,21,ABS
29,1970-01-01 01:00:00.000,1970-01-01 01:00:01.000,22,ABU
30,1970-01-01 01:00:00.000,1970-01-01 01:00:01.300,23,ACC"""

df = pd.read_csv(StringIO(csv))


def rgb(): return 'rgb(' + ','.join(map(str, np.random.randint(0, 255, size=3))) + ')'

discrete_map = { 'ABL': '#0d0887', 'ABS': '#46039f', 'ABU': '#7201a8', 'ACC': '#9c179e', 'ACT': '#bd3786', 'ABP': '#d8576b', 'ABZ': '#ed7953', 'AAW': '#fb9f3a'}

discrete_map_resource = {r: rgb() for r in df.Resource}
discrete_sequence_resource = [rgb() for resource in df.Resource.unique()]
discrete_sequence_task = [rgb() for task in df.Task]

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color_discrete_sequence=discrete_sequence_resource, text="Task", width=1600, height=800)
fig.write_image('discrete_sequence_resource.png')

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color_discrete_sequence=discrete_sequence_task, text="Task", width=1600, height=800)
fig.write_image('discrete_sequence_task.png')

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color_discrete_map=discrete_map, text="Task", width=1600, height=800)
fig.write_image('discrete_map.png')

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color_discrete_map=discrete_map_resource, text="Task", width=1600, height=800)
fig.write_image('discrete_map_resource.png')

附加信息(使用 v4.12v4.13 测试):

plotly             4.13.0

您的代码几乎可以正常工作,您只需组合 color="Resource"color_discrete_map=discrete_map_resource

也不需要将颜色转换为rgb(),你可以直接使用你的十六进制颜色,即

df = pd.read_csv(StringIO(csv))
discrete_map_resource = { 'ABL': '#0d0887', 'ABS': '#46039f', 'ABU': '#7201a8', 'ACC': '#9c179e', 'ACT': '#bd3786', 'ABP': '#d8576b', 'ABZ': '#ed7953', 'AAW': '#fb9f3a'}
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Resource",  color_discrete_map=discrete_map_resource, text="Task", width=1600, height=800)
fig.show()