如何在 plotly 甘特图中为元素指定颜色?
How to specify color for elements in plotly Gannt chart?
我在此处学习 python 教程:https://plotly.com/python/gantt/
import pandas as pd
import plotly.express as px
df = pd.DataFrame([
dict(Task="Job A", Start='2009-01-01', Finish='2009-02-25'),
dict(Task="Job A", Start='2009-02-26', Finish='2009-03-28'),
dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')
])
print(df)
Task Start Finish
0 Job A 2009-01-01 2009-02-25
1 Job A 2009-02-26 2009-03-28
2 Job B 2009-03-05 2009-04-15
3 Job C 2009-02-20 2009-05-30
上面教程中的代码给我图表:
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task")
fig.update_yaxes(autorange="reversed") # otherwise tasks are listed from the bottom up
fig.show()
请注意 Job A
.
有两个事件
我想为每个起点和终点交替使用颜色。例如第一事件红色,第二事件蓝色,第三事件红色,第四事件蓝色等
我已经尝试了 color_discrete_sequence
和 color_discrete_map
参数,但颜色没有交替。
我尝试过的:
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task",color_discrete_sequence=["red", "blue"])
fig.update_yaxes(autorange="reversed")
fig.show()
和
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task",color_discrete_map={"Start": "red","Finish":'blue'})
fig.update_yaxes(autorange="reversed")
fig.show()
关于如何交替颜色有什么想法吗?
我希望工作 A 看起来像的示例:
只需添加一个描述所需颜色的列。
import pandas as pd
import plotly.express as px
df = pd.DataFrame([
dict(Task="Job A", Start='2009-01-01', Finish='2009-02-25', Color='blue'),
dict(Task="Job A", Start='2009-02-26', Finish='2009-03-28', Color='red'),
dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15', Color='blue'),
dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Color='blue')
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Color")
fig.update_yaxes(autorange="reversed") # otherwise tasks are listed from the bottom up
fig.show()
我在此处学习 python 教程:https://plotly.com/python/gantt/
import pandas as pd
import plotly.express as px
df = pd.DataFrame([
dict(Task="Job A", Start='2009-01-01', Finish='2009-02-25'),
dict(Task="Job A", Start='2009-02-26', Finish='2009-03-28'),
dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')
])
print(df)
Task Start Finish
0 Job A 2009-01-01 2009-02-25
1 Job A 2009-02-26 2009-03-28
2 Job B 2009-03-05 2009-04-15
3 Job C 2009-02-20 2009-05-30
上面教程中的代码给我图表:
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task")
fig.update_yaxes(autorange="reversed") # otherwise tasks are listed from the bottom up
fig.show()
请注意 Job A
.
我想为每个起点和终点交替使用颜色。例如第一事件红色,第二事件蓝色,第三事件红色,第四事件蓝色等
我已经尝试了 color_discrete_sequence
和 color_discrete_map
参数,但颜色没有交替。
我尝试过的:
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task",color_discrete_sequence=["red", "blue"])
fig.update_yaxes(autorange="reversed")
fig.show()
和
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task",color_discrete_map={"Start": "red","Finish":'blue'})
fig.update_yaxes(autorange="reversed")
fig.show()
关于如何交替颜色有什么想法吗?
我希望工作 A 看起来像的示例:
只需添加一个描述所需颜色的列。
import pandas as pd
import plotly.express as px
df = pd.DataFrame([
dict(Task="Job A", Start='2009-01-01', Finish='2009-02-25', Color='blue'),
dict(Task="Job A", Start='2009-02-26', Finish='2009-03-28', Color='red'),
dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15', Color='blue'),
dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Color='blue')
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Color")
fig.update_yaxes(autorange="reversed") # otherwise tasks are listed from the bottom up
fig.show()