Good question. You showed one approach. Here's another approach to show the same thing you wanted. The main difference is that there is more configurability using my barebones approach rather than using a simplifying framework like in your approach.
from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
import math
"""
https://stackoverflow.com/questions/79554216/year-comparison
"""
def is_prefix_of_any(substring, string_list):
return any(item.startswith(substring) for item in string_list)
def nearest_neighbor(input_number, number_list):
nearest = min(number_list, key=lambda x: abs(x - input_number))
return nearest
def rgb2hex(r,g,b):
hex = "#{:02x}{:02x}{:02x}".format(r,g,b)
return hex
def compass_to_rgb(h, s=1, v=1):
h = float(h)
s = float(s)
v = float(v)
h60 = h / 60.0
h60f = math.floor(h60)
hi = int(h60f) % 6
f = h60 - h60f
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
r, g, b = 0, 0, 0
if hi == 0: r, g, b = v, t, p
elif hi == 1: r, g, b = q, v, p
elif hi == 2: r, g, b = p, v, t
elif hi == 3: r, g, b = p, q, v
elif hi == 4: r, g, b = t, p, v
elif hi == 5: r, g, b = v, p, q
r, g, b = int(r * 255), int(g * 255), int(b * 255)
return rgb2hex(r, g, b)
file_path = 'tga-deposits-taxes.csv'
with open(file_path) as file_in:
lines = []
for line in file_in:
lines.append(line.strip())
lines2 = []
lines2.append("year,month,date,amt")
for x in range(len(lines)):
if x != 0:
lines[x] = lines[x].split(",")
lines[x] = lines[x][0].split("-")+lines[x][1:]
lines2.append(','.join(lines[x]))
years = set()
months = set()
days = set()
date_objs = [datetime.strptime(date_str[:10], "%Y,%m,%d").date() for date_str in lines2[1:]]
for x in date_objs:
years.add(x.year)
months.add(x.month)
days.add(x.day)
years = sorted(years)
months = sorted(months)
days = sorted(days)
lines3 = {}
for y in months:
lines3[y] = {}
for x in years:
for y in months:
substring1 = str(x)+","+str(y).zfill(2)
if is_prefix_of_any(substring1,lines2):
lines3[y][x] = {}
for z in days:
substring2 = str(x)+","+str(y).zfill(2)+","+str(z).zfill(2)
if is_prefix_of_any(substring2,lines2):
for i in lines2:
if (str(x)==i.split(",")[0]) and (str(y).zfill(2)==i.split(",")[1]) and (str(z).zfill(2)==i.split(",")[2]):
lines3[y][x][z] = int(i.split(",")[3])
else:
number_list = set()
for i in lines2:
if (str(x)==i.split(",")[0]) and (str(y).zfill(2)==i.split(",")[1]):
number_list.add(int(i.split(",")[2]))
number_list = sorted(number_list)
nearest_neighbor_result = nearest_neighbor(z,number_list)
for i in lines2:
if (str(x)==i.split(",")[0]) and (str(y).zfill(2)==i.split(",")[1]) and (str(nearest_neighbor_result).zfill(2)==i.split(",")[2]):
lines3[y][x][z] = int(i.split(",")[3])
month_list = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
color_list = {}
for x in range(len(years)):
color_list[years[x]] = compass_to_rgb(360.0/float(len(years))*(x+1))
for y in range(len(months)):
plt.subplot(1, len(months)+1, y+1)
current_dict = lines3[months[y]]
current_dict_keys = current_dict.keys()
for x in current_dict.keys():
list_of_keys = []
list_of_values = []
for key,val in current_dict[x].items():
list_of_keys.append(key)
list_of_values.append(val)
plt.plot(list_of_keys,list_of_values, color=color_list[x])
plt.xlabel("Date")
plt.ylabel("Amt")
plt.title(month_list[months[y]-1])
plt.subplot(1, len(months)+1, len(months)+1)
keys = list(color_list.keys())
for x in range(len(keys)):
plt.text(0, x, keys[x], color=color_list[keys[x]], fontsize=10)
plt.plot([],[])
plt.xlabel("")
plt.ylabel("")
plt.ylim(0, len(keys) + 1) # ensure all labels fit
plt.axis('off')
plt.title("Legend")
plt.show()