sleep_data / draw_db.py
thelou1s's picture
fix FileNotFoundError
c846304
import math
import os
import dateutil.tz
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from util.debug import DEBUG
from util.debug import debug_print
def up_down_list(_list):
min_value = min(_list)
max_value = max(_list)
return [-(x - min_value) + max_value for x in _list]
def min_max_list(src_list, tar_min, tar_max):
src_min = min(src_list)
src_max = max(src_list)
src_range = abs(src_max - src_min)
tar_range = abs(tar_max - tar_min)
scale_ratio = 0 if src_range == 0 else tar_range / src_range
floor_up = [v - src_min for v in src_list]
scale_to = [v * scale_ratio for v in floor_up]
floor_down = [v + tar_min for v in scale_to]
return floor_down
def draw_lists(title='', date_list=None, **kv_list):
debug_print('draw_lists, date_list = ', str(date_list))
idx_list = 0
idx_axs = 0
rows = math.ceil(len(kv_list.items()) / 3) + 1
fig, axs = plt.subplots(rows, 1, sharex=True)
for key, values in kv_list.items():
if type(key) is str and type(values) is list:
if key == 'predict':
axs[rows - 1].plot(date_list, values, label=key)
axs[rows - 1].legend()
else:
axs[idx_axs].plot(date_list, values, label=key)
axs[idx_axs].legend()
idx_list = idx_list + 1
idx_axs = int(idx_list / 3)
# tz = dateutil.tz.gettz('Asia/Shanghai')
# # date_format = date_list.DateFormatter('%d-%m-%Y')
# plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b/%d %H:%M', tz=tz))
# # plt.gca().xaxis.set_major_formatter(date_format)
# plt.xticks(rotation=25)
plt.gcf().autofmt_xdate()
axs[0].set_title(title)
tmp_dir = 'tmp'
if not os.path.exists(tmp_dir):
os.mkdir(tmp_dir)
if DEBUG:
# plt.show()
file_uri = tmp_dir + '/' + title + '.png'
fig.savefig(file_uri)
else:
file_uri = tmp_dir + '/' + title + '.png'
fig.savefig(file_uri)
return fig