import datetime
from matplotlib import pyplot as plt
data = {1: ['2018-09-20 20:40:50', '2018-09-20 21:11:14'], \
2: ['2018-09-20 12:45:44', '2018-09-20 13:22:24']}
fig, ax = plt.subplots() # open figure; create axis
ylabels = [] # extract the dates
yticks = [] # track the position on the y-axis
for k, v in data.items():
# extract the time > see python docs for meaning of the symbols
times = [datetime.datetime.strptime(i, '%Y-%m-%d %H:%M:%S') for i in v]
offset= times[0].hour # offset from the left
delta = times[1].hour - times[0].hour # compute stream time
ax.barh(k, delta, left = offset, align = 'center') # plot
ylabels.append(v[0].split(' ')[0]) # extract date
yticks.append(k)
# format figure
ax.set(**dict(xlabel = 'Time[hour]', xlim = (0, 24), \
yticks = yticks, yticklabels = ylabels))
fig.show()
Plotly如果您不局限于Matplotlib,那么它有现成的交互式甘特图。你知道吗
Output
下面是一个例子。甘特图的诀窍是计算原点和所需时间之间的偏移量。请参阅下面的代码和一些注释。你知道吗
你想画的似乎是甘特图。 您可以看到一个关于在this link上使用matplot绘制甘特图的好例子
相关问题 更多 >
编程相关推荐