python - 如何在甘特图中指示预期日期和实际日期?

标签 python r excel plotly gantt-chart

我正在尝试创建一个甘特图,其中涉及计划和实际开始/结束日期。我希望相同的任务重叠,例如:
到目前为止,这是我的代码。

import plotly.figure_factory as ff
import pandas as pd

df = pd.DataFrame([
    dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28'),
    dict(Task="Job A", Start='2009-01-05', Finish='2009-02-15'),
    dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')
])
colors = {'Job A': 'rgb(220, 0, 0)',
          'Job C': (1, 0.9, 0.16), }

fig = ff.create_gantt(df, colors=colors, index_col="Task", show_colorbar=True, group_tasks=True)
fig.update_yaxes(autorange="reversed")  # otherwise tasks are listed from the bottom up
fig.show()

这是结果: Gantt chart without overlapping colors

如何区分计划日期和实际日期?欢迎使用 Python、R 和 Excel 解决方案。在解决方案中使用 Plotly 是完全可选的。

最佳答案

这是一个 R 解决方案:

library(tidyverse)

df %>%
  mutate(Task = factor(Task, rev(levels(factor(Task)))),
         yval = as.numeric(Task)) %>% {
  ggplot(., aes(xmin = Start, xmax = Finish)) + 
  geom_rect(data = filter(., Type == "Planned"), alpha = 0.5,
            aes(ymin = yval - 0.4, ymax = yval + 0.4, fill = "Planned")) +
  geom_rect(data = filter(., Type == "Actual"),
            aes(ymin = yval - 0.2, ymax = yval + 0.2, fill = "Actual"), 
            color = "black") +
  scale_fill_manual("", values = c("deepskyblue4", "olivedrab")) +
  scale_y_continuous(breaks = seq_along(levels(.$Task)),
                     labels = levels(.$Task)) +
  labs(title = "Planned versus actual job times", x = "Date") +
  coord_fixed(15) +
  theme_bw(base_size = 16)
}

enter image description here

为此,您的数据应采用以下格式:

df
#>    Task      Start     Finish    Type
#> 1 Job A 2009-01-01 2009-02-28 Planned
#> 2 Job A 2009-01-05 2009-02-15  Actual
#> 3 Job B 2009-02-01 2009-03-15 Planned
#> 4 Job B 2009-02-10 2009-03-15  Actual
#> 5 Job C 2009-02-20 2009-05-30 Planned
#> 6 Job C 2009-02-22 2009-05-20  Actual

reprex package 于 2022 年 5 月 23 日创建(v2.0.1)

关于python - 如何在甘特图中指示预期日期和实际日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72354057/

相关文章:

python - 3D 向量的平面方程

python - 删除除列表中的所有单词

python - 带有日期索引的 pandas 数据框 -> 插入 MySQL

r - 在 Shiny 中使用 LeafletProxy 更新时在 map 上显示加载微调器

c# - Excel 互操作货币格式

python - 使用 numpy/scipy 将 3D 点的最近点投影到 3D 三角形

r - 如何(最低限度地)调整 geom_text() 标签以避免在堆叠的 geom_col() 上重叠

Excel 2010索引匹配VBA

xml - 我可以使用 VBA 中的变量更新 XML 节点值吗

r - 整数和因子的 Pivot_longer