简体   繁体   English

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

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

I have a plotly Gantt / timeline diagram using plotly.express and want to specify the color of my bars as in the discrete colors tutorial, yet I'm stuck.我有一个使用plotly.express的 plotly甘特图/时间线图,并且想像在离散的 colors教程中一样指定我的条形的颜色,但我被卡住了。 No matter if color_discrete_map or color_discrete_sequence parameter is used, the output bars are mono-colored:无论使用color_discrete_map还是color_discrete_sequence参数,output 条都是单色的:

discrete_sequence_resource / same for discrete_sequence_task离散序列资源/离散序列任务相同

离散序列资源.png

discrete_map_resource / same for discrete_map离散映射资源/离散映射相同

离散地图资源.png

As one can see in the pictures above, the bar color is always mono-colored.如上图所示,条形颜色始终为单色。 If I just set color="Resource" the bars are multi-colored accordingly to the resource, which is what I want.如果我只是设置color="Resource" ,那么这些条就会根据资源显示为多种颜色,这就是我想要的。 Unfortunately, I need to exactly specify the color of the bars due to some later usage.不幸的是,由于以后的一些使用,我需要准确地指定条的颜色。

Here's the code I used to generate the pictures above:这是我用来生成上面图片的代码:

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')

Additional info (tested with v4.12 and v4.13 ):附加信息(使用v4.12v4.13测试):

plotly             4.13.0

Your code is almost working, you just need to combine color="Resource" and color_discrete_map=discrete_map_resource .您的代码几乎可以工作,您只需将color="Resource"color_discrete_map=discrete_map_resource结合起来。

There is also no need to convert colors to rgb(), you can directly use your hex colors, ie也不需要把colors转成rgb(),可以直接用你的hex colors,即

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM