gpt4 book ai didi

python - 如何使用 matplotlib 获取甘特图

转载 作者:太空狗 更新时间:2023-10-29 22:06:22 29 4
gpt4 key购买 nike

我得到了一些数据,比如:

a0:86:c6:52:4e:e8,0.006568,0.006620,Out
a0:86:c6:52:4e:e8,0.006663,0.006695,In
a0:86:c6:52:4e:e8,0.008089,0.008141,Out
a0:86:c6:52:4e:e8,0.008185,0.008217,In
01:00:5e:00:00:fb,0.033096,0.035016,Out
33:33:00:00:00:fb,0.034997,0.037077,Out
01:00:5e:7f:ff:fa,0.039969,0.042057,Out
ff:ff:ff:ff:ff:ff,0.059823,0.061639,Out
a0:86:c6:52:4e:e8,0.068865,0.068917,Out
a0:86:c6:52:4e:e8,0.068962,0.068994,In
a0:86:c6:52:4e:e8,0.083492,0.083544,Out
a0:86:c6:52:4e:e8,0.083588,0.083620,In
...

实际上,它持续了 120 秒。我想使用 matplotlib 将这些数据绘制成类似甘特图的图形。原因是大多数甘特图 api 只支持时间格式 YY-MM-DD HH:MM:SS,而不是这么小(微秒)的大小。

请告诉我该怎么做?

最佳答案

为了在 matplotlib 中生成甘特图,可以使用 plt.broken_barh功能。有 a broken_barh example在 matplotlib 页面上。

为了过滤和分组数据,可以使用 pandas,尤其是数据框的 groupby 功能。

这是一个完整的例子:

inp = u"""a0:86:c6:52:4e:e8,0.006568,0.006620,Out
a0:86:c6:52:4e:e8,0.006663,0.006695,In
a0:86:c6:52:4e:e8,0.008089,0.008141,Out
a0:86:c6:52:4e:e8,0.008185,0.008217,In
01:00:5e:00:00:fb,0.033096,0.035016,Out
33:33:00:00:00:fb,0.034997,0.037077,Out
01:00:5e:7f:ff:fa,0.039969,0.042057,Out
ff:ff:ff:ff:ff:ff,0.059823,0.061639,Out
a0:86:c6:52:4e:e8,0.068865,0.068917,Out
a0:86:c6:52:4e:e8,0.068962,0.068994,In
a0:86:c6:52:4e:e8,0.083492,0.083544,Out
a0:86:c6:52:4e:e8,0.083588,0.083620,In"""

import pandas as pd
import io
import matplotlib.pyplot as plt

df = pd.read_csv(io.StringIO(inp), header=None, names=["Task", "Start", "Finish", "Resource"] )
df["Diff"] = df.Finish - df.Start

color = {"In":"turquoise", "Out":"crimson"}
fig,ax=plt.subplots(figsize=(6,3))

labels=[]
for i, task in enumerate(df.groupby("Task")):
labels.append(task[0])
for r in task[1].groupby("Resource"):
data = r[1][["Start", "Diff"]]
ax.broken_barh(data.values, (i-0.4,0.8), color=color[r[0]] )

ax.set_yticks(range(len(labels)))
ax.set_yticklabels(labels)
ax.set_xlabel("time [ms]")
plt.tight_layout()
plt.show()

产生以下情节:

enter image description here

关于python - 如何使用 matplotlib 获取甘特图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43367690/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com