-
Notifications
You must be signed in to change notification settings - Fork 0
/
plots.py
149 lines (112 loc) · 4.24 KB
/
plots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import itertools
import os
from collections import defaultdict
from datetime import datetime
from typing import Iterable, Iterator
import attr
import httpx
import matplotlib
import numpy as np
import pandas as pd
import seaborn as sns
import snug
from dotenv import load_dotenv
from matplotlib import pyplot as plt
from matplotlib.axes import Axes
from matplotlib.cm import ScalarMappable
from matplotlib.colors import Colormap
from matplotlib.pyplot import colorbar
from mpl_toolkits.axes_grid1 import make_axes_locatable
from rich import print
import electron.schemas
from electron import electricity_maps, execute, execute_async, executor, token_auth
def _google_colormap(top_color: str = "green") -> Colormap:
center = 0.8 # 80% CFE considered "neutral"
return matplotlib.colors.LinearSegmentedColormap.from_list(
"mycmap", [(0, "black"), (center, "white"), (1, top_color)], N=1000
)
def plot_clock(
hourly_percentages: dict[int, float],
colormap: Colormap,
radius: float = 1,
ax: Axes = None,
) -> Axes:
_NUM_SLICES = 24
_SLICE_WIDTH = 2 * np.pi / _NUM_SLICES
thetas = [i * 2 * np.pi / 24 for i in range(_NUM_SLICES)]
y = [radius for _ in thetas]
ax = ax or plt.subplot(111, projection="polar")
bars = ax.bar([theta + _SLICE_WIDTH / 2 for theta in thetas], y, width=_SLICE_WIDTH)
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
ax.set_ylim([0, 1])
ax.set_rorigin(-1 / 10)
ax.set_xticks(thetas)
ax.set_xticklabels([i for i in range(_NUM_SLICES)])
ax.set_yticks([])
# Colour each slice based on hourly percentages
for h, bar in enumerate(bars):
# there might not be data for all hours of the day
cfe = hourly_percentages.get(h)
if cfe is None:
bar.set_visible(False)
else:
bar.set_facecolor(colormap(cfe / 100))
return ax
def colormap_plot(colormap: Colormap, ax=None):
ax = ax or plt.gca()
# im = ax.imshow(np.arange(100).reshape((10,10)), cmap=colormap)
# im.set_visible(False)
# ax.set_axis_off()
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=1, axes_class=Axes)
plt.colorbar(ScalarMappable(cmap=colormap), cax=cax, orientation="vertical")
return ax
if __name__ == "__main__":
sns.set_theme()
cmap = _google_colormap()
cmap_b = _google_colormap(top_color="blue")
load_dotenv()
API_TOKEN = os.environ.get("API_TOKEN")
with httpx.Client() as client:
query = electricity_maps.power_breakdown.get_history("DK")
power_breakdown_history = execute(
query, auth=token_auth(API_TOKEN), client=client
)
print(power_breakdown_history)
# bucket power breakdowns into individual days
power_breakdowns_by_date = itertools.groupby(
power_breakdown_history.history, key=lambda x: x.datetime.date()
)
for date, daily_power_breakdowns in power_breakdowns_by_date:
daily_power_breakdowns = list(daily_power_breakdowns)
hourly_fossil_free_energy_percentage = {
power_breakdown.datetime.hour: power_breakdown.fossil_free_percentage
for power_breakdown in daily_power_breakdowns
}
ax = plot_clock(hourly_fossil_free_energy_percentage, colormap=cmap)
ax.set_title(f"{date.isoformat()} ({power_breakdown_history.zone})")
hourly_renewable_energy_percentage = {
power_breakdown.datetime.hour: power_breakdown.renewable_percentage
for power_breakdown in daily_power_breakdowns
}
ax = plot_clock(
hourly_renewable_energy_percentage, colormap=cmap_b, radius=0.5, ax=ax
)
plt.show()
# for fossil_free_percentages in daily_datas:
# fig = plt.figure()
# ax1 = plt.subplot(121, projection='polar')
# clock_plot(fossil_free_percentages, colormap=cmap)
# plt.show()
"""
data = [[i * 100 / 24 for i in range(24)] for _ in range(365)]
# sns.set_theme()
cmap = _get_google_colormap()
ax = plt.subplot(111)
img = ax.imshow(np.array(data).T, origin="upper", cmap=cmap)
ax.set_xticks([])
ax.set_yticks([i for i in range(24)])
ax.set_aspect(4)
plt.colorbar(img, cax=ax, orientation='horizontal')
"""