gpt4 book ai didi

python - 如何创建表示每天多个时间间隔的图表

转载 作者:太空宇宙 更新时间:2023-11-03 20:59:39 30 4
gpt4 key购买 nike

给定以下包含每天开放/关闭时间对的dict:

timetable = {
'monday': ['08:00', '12:00', '13:00', '18:00'],
'tuesday': ['08:00', '12:00', '13:00', '18:00'],
'wednesday': ['08:00', '12:00', '13:00', '18:00'],
'thursday': ['08:00', '12:00', '13:00', '18:00'],
'friday': ['08:00', '12:00', '13:00', '18:00', '19:00', '23:00'],
'saturday': ['10:00', '16:00'],
'sunday': ['10:00', '16:00'],
}

有没有办法创建像这样的图形表示 /image/fe9ND.jpg (这是用 gimp 完成的,只是为了大致了解它的样子)?

最佳答案

尝试这样的事情。

import matplotlib.pyplot as plt
import datetime
timetable = {
'monday': ['08:00', '12:00', '13:00', '18:00'],
'tuesday': ['08:00', '12:00', '13:00', '18:00'],
'wednesday': ['08:00', '12:00', '13:00', '18:00'],
'thursday': ['08:00', '12:00', '13:00', '18:00'],
'friday': ['08:00', '12:00', '13:00', '18:00', '19:00', '23:00'],
'saturday': ['10:00', '16:00'],
'sunday': ['10:00', '16:00'],
}
COLOR_LIST = ['blue', 'red', 'green', 'pink', 'brown', 'orange', 'yellow']
HEIGHT = 2
fig, ax = plt.subplots()
y_parameter = 0
for day, day_data in timetable.iteritems():
st = [datetime.datetime.strptime(i, "%H:%M") for i in day_data[::2]]
et = [datetime.datetime.strptime(i, "%H:%M") for i in day_data[1::2]]
color = COLOR_LIST.pop()
for start_time, end_time in zip(st, et):
diff = (end_time - start_time).total_seconds()
x_paramter = datetime.timedelta(hours=start_time.hour, minutes=start_time.minute,
seconds=start_time.second).total_seconds()
ax.add_patch(plt.Rectangle((x_paramter, y_parameter), int(diff), HEIGHT, facecolor=color))
centerx = x_paramter + 100
centery = y_parameter + 1
plt.text(centerx, centery, day, fontsize=5, wrap=True)
plt.text(centerx, centery - 1, str(int(diff)), fontsize=5, wrap=True)
ax.autoscale()
ax.set_ylim(0, 24)
y_parameter += 3
plt.show()

输出类似于:

enter image description here

关于python - 如何创建表示每天多个时间间隔的图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55787964/

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