我有一个情节甘特图/时间线图,plotly.express
并希望像离散颜色教程中那样指定我的条形图的颜色,但我被卡住了。无论是否使用color_discrete_map
或color_discrete_sequence
参数,输出条都是单色的:
离散序列资源/离散序列任务相同
离散映射资源/离散映射相同
如上图所示,条形颜色始终为单色。如果我只是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.12
and测试v4.13
):
plotly 4.13.0