Created
June 26, 2018 22:35
-
-
Save donkirkby/457993268cd602be1e2f4bcc93069c53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
from matplotlib import pyplot as plt | |
import pandas as pd | |
import requests | |
import seaborn as sn | |
DATA_PATH = Path('data') | |
def main(): | |
co2_per_person = load_google_doc('phAwcNAVuyj1gkNuUEXOGag') | |
has_high_peak = co2_per_person.max() > 40 | |
top_co2 = co2_per_person[co2_per_person.columns[has_high_peak]] | |
related_countries = ['Canada', 'United States', 'United Kingdom'] | |
related_co2 = co2_per_person[related_countries] | |
sn.set() | |
ax = plt.subplot(211) | |
top_co2.plot(ax=ax) | |
ax.set_ylabel('MT per capita') | |
ax = plt.subplot(212, sharex=ax) | |
related_co2.plot(ax=ax) | |
ax.lines[0].set_linewidth(3) | |
ax.set_ylabel('MT per capita') | |
plt.suptitle('Carbon Dioxide Emissions') | |
plt.tight_layout(rect=[0, 0, 1, 0.95]) | |
plt.show() | |
def load_google_doc(key): | |
target = DATA_PATH / f'google_{key}.csv' | |
if not target.exists(): | |
response = requests.get('https://docs.google.com/spreadsheet/pub', | |
params=dict(key=key, output='csv')) | |
response.raise_for_status() | |
with target.open('wb') as target_file: | |
for chunk in response.iter_content(chunk_size=16*1024): | |
target_file.write(chunk) | |
data_frame = pd.read_csv(target, index_col=0) | |
column_names = data_frame.columns.values | |
column_names = column_names.astype(int) | |
data_frame.columns = column_names | |
data_frame = data_frame.T | |
data_frame.index.names = ['Year'] | |
return data_frame | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment