如何使 plotly.express.timeline 重叠条可见 [英] How to get plotly.express.timeline overlapped bars to be visible
本文介绍了如何使 plotly.express.timeline 重叠条可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有人有解决重叠时间轴栏的方法?
I'd like to know if anyone has a solution to overlapping timeline bars?
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()
在图片中很难看到:
- 工作 A 结束/工作 B 开始的地方
- 作业 D 根本不可见
有什么建议吗?
推荐答案
我经常发现为某些类别分配不同的 width
有助于使整个内容更易于阅读.因此,在您的情况下,我会在您的 df
中包含一列,其中指定了这一列,然后使用以下方法编辑该图:
I often find that assigning different width
to some of the categories helps make the whole thing easier to read. So in your case I would include a column in your df
where this is specified, and then edit the figure using:
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()
这篇关于如何使 plotly.express.timeline 重叠条可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文