import json from datetime import datetime, timedelta import matplotlib.pyplot as plt from matplotlib.patches import Patch def is_night(dt: datetime): return dt.hour < 6 or dt.hour >= 22 with open("history.json", "r") as f: data = json.load(f) data = data["normal"] times = [] probs = [] colors = [] corrects = [] for timestamp, d in data: time = datetime.fromtimestamp(timestamp) times.append(time) probs.append(d["weighted_probabilities_mean"]) correct = d["night"] == is_night(time) corrects.append(correct) colors.append("#95c1a6" if correct else "#ffa695") fig, ax = plt.subplots() #ax.grid() ax.margins(0.01) #ax.plot(times, probs, "black", marker=".", markeredgecolor="green", markerfacecolor="green") ax.plot(times, probs, "black", zorder=1, marker=".", markersize=3) #ax.scatter(times, probs, c=colors, alpha=1.0, marker=".", zorder=2) plt.xlabel('Time') plt.ylabel('Probability of Night') #ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) night_color = "#e5e5ff" day_color = "#ffffe5" tfrom = datetime(2019, 4, 8, 22) tto = datetime(2019, 4, 9, 6) ticks = [] while True: ax.axvspan(tfrom, tto, facecolor=night_color if is_night(tfrom) else day_color, alpha=1.0) ticks.append((tfrom, tfrom.strftime("%H:%M"))) if tto >= max(times): break tfrom = tto if is_night(tfrom): tto = tto.replace(hour=6) + timedelta(days=1) else: tto = tto.replace(hour=22) plt.xticks(*zip(*ticks)) ax.axhline(0.5, color="black", linewidth=0.5) ax.fill_between(times, probs, 0.5, where=[is_night(t) != bool(round(p)) for t, p in zip(times, probs)], facecolor="#ffa695", interpolate=True, zorder=100) ax.fill_between(times, probs, 0.5, where=[is_night(t) == bool(round(p)) for t, p in zip(times, probs)], facecolor="#95c1a6", interpolate=True, zorder=100) legend_elements = [Patch([0], [0], color=night_color, label='Night'), Patch([0], [0], color="#ffff70", label='Day'), Patch([0], [0], color='#95c1a6', label='Correct'), Patch([0], [0], color='#ffa695', label='Wrong')] ax.legend(handles=legend_elements, loc="upper left", framealpha=1.0) plt.title(f"Nightr: {sum(corrects) / len(corrects):.0%} of the time, it works every time") plt.show()