-
Notifications
You must be signed in to change notification settings - Fork 0
/
co2.py
71 lines (53 loc) · 2.25 KB
/
co2.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
from plotly.offline import init_notebook_mode, plot
import plotly.graph_objs as go
import pandas as pd
from numpy import arange,array,ones
import os.path
import time
from scipy import stats
start_time = time.time()
def getGlobalDataByDateRange(co2, start = 1960, end = 2016):
x = co2[(co2['Year'] > start) & (co2['Year'] <= end)]['Year']
y_co2 = co2[(co2['Year'] > start) & (co2['Year'] <= end)]['Global']
co2goal = float((co2[co2['Year'] == 1990]['Global']) / 2)
traceCO2 = go.Scatter(
x=x,
y=co2['Global'],
mode='lines',
marker=go.Marker(color='rgb(255, 153, 51)'),
name='CO₂ emmission'
)
lineCO2 = go.Scatter(
x=x,
y=[co2goal] * len(x),
line = dict(
color = ('rgb(205, 12, 24)'),
width = 2,
dash = 'dot'),
name='CO₂ Goal'
)
return [traceCO2, lineCO2]
def getFigure(co2, start = 1960, end = 2016):
print("--- %s seconds --- Getting figure of scatterplot with fitted line" % (time.time() - start_time))
data = getGlobalDataByDateRange(co2, start, end)
layout = dict(title = 'Global - average MtCO₂ emmission between ' + str(start) + ' - ' + str(end),
xaxis = dict(title = 'Year -->'),
yaxis = dict(title = 'Emmission in MtCO₂ -->'),
)
return dict(data = data, layout = layout)
def createScatter(co2, start = 1960, end = 2016):
print("--- %s seconds --- Plotting figure of scatterplot with fitted line" % (time.time() - start_time))
fig = getFigure(co2, start, end)
plot(fig, filename='climate_change_scatter-and-line.html')
def getData():
print("--- %s seconds --- Getting CO2 emmission" % (time.time() - start_time))
df = pd.read_csv('data/GlobalCarbonAtlas_territorial.csv', header=1)
columns = list(df)[1:]
df['Global'] = df[columns].mean(axis=1)
return df
if __name__ == '__main__':
country = 'Global'
start = 1960
end = 2016
co2 = getData()
createScatter(co2, start, end)