from pylab import *
from datetime import datetime, timezone
mi, ma, gu, t = [], [], [], []
matplotlib.rcParams.update({'font.size': 16})
# CSV file from https://ccaf.io/cbeci/api/v1.4.0/download/data?price=0.05
f = open("export.csv")
for num, l in enumerate(f.readlines()):
if num < 2: continue
x = l.split(",")
ts = int(x[0])
d = datetime.fromtimestamp(ts, timezone.utc)
t.append(d)
ma.append(float(x[5]))
mi.append(float(x[6]))
gu.append(float(x[7]))
figure(figsize = (9, 7))
fill_between(t, mi, ma, color = "#d8d8d8")
plot(t, ma, label = "Upper bound" , lw = 3.5, c = "#1829af", alpha = .5)
plot(t, mi, label = "Lower bound" , lw = 3.5, c = "#18af23", alpha = .5)
plot(t, gu, label = "Estimate", lw = 5.5, c = "#af2318")
# Estimate is drawn last (so that it is not obscured by the other curves),
# but it should come in _second_ place in the legend, so shuffle legend order accordingly:
handles, labels = gca().get_legend_handles_labels()
order = [0, 2, 1]
legend([handles[idx] for idx in order], [labels[idx] for idx in order], loc = "upper left")
#xlabel("Year")
ylabel("[TWh]")
title("Bitcoin annualized electricity consumption")
axis([datetime(2016, 1, 1), datetime(2025, 4, 4), 0, 600])
grid()
gca().tick_params(axis = "both", pad = 14)
gca().set_position([.15, .15, .8, .75])
text(datetime(2024, 1, 1), -90, 'Source: University of Cambridge', va = 'bottom', ha = 'right' , size = 14)
savefig("Bitcoin electricity consumption.svg")
show()