我想知道是否有人有重叠时间线栏的解决方案?
import plotly.express as px
import pandas as pd
df = pd.DataFrame([
dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Resource="Alex"),
dict(Task="Job B", Start='2009-02-25', Finish='2009-04-15', Resource="Alex"),
dict(Task="Job C", Start='2009-02-23', Finish='2009-05-23', Resource="Max"),
dict(Task="Job D", Start='2009-02-20', Finish='2009-05-30', Resource="Max")
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Resource", color="Resource")
fig.show()
在图像中很难看到:
有什么建议吗?
我经常发现为某些类别分配不同的
width
有助于使整个内容更易于阅读。因此,在您的情况下,我会在您的 df
中包含一个指定的列,然后使用以下命令编辑该图:
for i, d in enumerate(fig.data):
d.width = df[df['Task']==d.name]['width']
import plotly.express as px
import pandas as pd
resource = ['Alex', 'Max']
df = pd.DataFrame([
dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Resource="Alex", width = 0.5),
dict(Task="Job B", Start='2009-02-25', Finish='2009-04-15', Resource="Alex", width = 0.2),
dict(Task="Job C", Start='2009-02-23', Finish='2009-05-23', Resource="Max", width = 0.5),
dict(Task="Job D", Start='2009-02-20', Finish='2009-05-30', Resource="Max", width = 0.2)
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Resource", color="Task")
for i, d in enumerate(fig.data):
d.width = df[df['Task']==d.name]['width']
fig.show()
我能想到的最好办法是对所有
"Tasks"
或"Jobs"
使用不同的颜色,并且必须降低opacity
,以便可以看到被覆盖的痕迹/条
代码如下:
import plotly.express as px
import pandas as pd
df = pd.DataFrame([
dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Resource="Alex"),
dict(Task="Job B", Start='2009-02-25', Finish='2009-04-15', Resource="Alex"),
dict(Task="Job C", Start='2009-02-23', Finish='2009-05-23', Resource="Max"),
dict(Task="Job D", Start='2009-02-20', Finish='2009-05-30', Resource="Max")
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Resource", color="Task", opacity=0.5)
fig.show()
您可以通过此命令更新布局:
fig.update_layout(barmode='group')