Skip to content

Instantly share code, notes, and snippets.

@protolambda
Created March 13, 2021 20:17
Show Gist options
  • Save protolambda/f7b8895337850e1ea1212f2cb75406c7 to your computer and use it in GitHub Desktop.
Save protolambda/f7b8895337850e1ea1212f2cb75406c7 to your computer and use it in GitHub Desktop.
bitcoin price vs energy usage
import pandas as pd
energy = pd.read_csv('energy.csv')
price = pd.read_csv('btcusd.csv', skiprows=1, header='infer')
day_seconds = 24*60*60
energy['Timestamp'] = (energy['Timestamp'] // day_seconds) * day_seconds
energy.set_index('Timestamp', drop=True, inplace=True)
# Some are in ms precision, others are not. Convert everything to seconds
price['Unix Timestamp'] = price['Unix Timestamp'].apply(lambda x: x if x < 1800_000_000 else x//1000)
price['Unix Timestamp'] = (price['Unix Timestamp'] // day_seconds) * day_seconds
price.set_index('Unix Timestamp', drop=True, inplace=True)
price.sort_index(inplace=True)
energy.sort_index(inplace=True)
combined = price.join(energy)
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
from matplotlib.dates import DateFormatter
import matplotlib.pyplot as plt
price_per_energy = combined['Close'] / combined['GUESS']
price_per_energy = price_per_energy.dropna()
plt.figure()
# Create figure and plot space
fig, ax = plt.subplots(figsize=(12, 12))
# Add x-axis and y-axis
ax.plot(pd.to_datetime(price_per_energy.index.values, unit='s'),
price_per_energy,
color='purple')
# Set title and labels for axes
ax.set(xlabel="Date",
ylabel="BTCUSD Close / Est yearly TWh",
title="Price/energy of Bitcoin over time")
# Define the date format
date_form = DateFormatter("%b %Y")
ax.xaxis.set_major_formatter(date_form)
plt.text(x=0.1, y=0.01, s="""
data:
BTCUSD Gemini - http://www.cryptodatadownload.com/data/gemini/
Cambridge Bitcoin Electricity Consumption Index - https://cbeci.org/
""", fontsize=13, ha="left", transform=fig.transFigure)
plt.text(x=0.98, y=0.02, s="<3 proof of stake, proto", fontsize=13, ha="right", transform=fig.transFigure)
fig.subplots_adjust(bottom=0.15, top=0.95, left=0.08, right=0.98)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment