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

考虑到以下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'],
}

有没有一种方法来创建看起来像https://imgur.com/a/lK8CT9P的图形表示(这是通过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()

输出类似于:

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

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python:如何根据另一列元素明智地查找一列中的空单元格计数? - python

df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice','Jane', 'Alice','Bob', 'Alice'], 'income…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

将字符串分配给numpy.zeros数组[重复] - python

This question already has answers here: Weird behaviour initializing a numpy array of string data                                                                    (4个答案)         …

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…