-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrafici.py
211 lines (183 loc) · 7.21 KB
/
grafici.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import pandas as pd
import numpy as np
import os
import plotly.plotly as py
import plotly.graph_objs as go
'''Trend CO2'''
def trend_co2(co2_anno):
# Create a trace
trace = go.Scatter(
x = co2_anno['Anno'],
y = co2_anno['Media CO2'],
mode = 'lines+markers',
name = 'CO2 Trend'
)
data = [trace]
# Edit the layout
layout = dict(title = 'Trend storico della concentrazione di CO2 in ppm (parti per milione)',
yaxis = dict(title = 'CO2 (ppm)')
)
annotations = []
annotations.append(dict(xref='paper', yref='paper', x=0.5, y=-0.1,
xanchor='center', yanchor='top',
text='Fonte: Osservatorio Mauna Loa, Hawaii (NOAA-ESRL)',
font=dict(family='Arial',
size=12,
color='rgb(150,150,150)'),
showarrow=False))
layout['annotations'] = annotations
fig = dict(data=data, layout=layout)
return fig
'''Trend Temperatura Terrestre'''
def trend_temp(global_temp):
trace = go.Scatter(
x = global_temp['Year'],
y = global_temp['Glob_C'],
mode = 'lines'
)
data = [trace]
# Edit the layout
layout = dict(title = 'Trend storico della temperatura terrestre in gradi C°',
yaxis = dict(title = 'gradi (C°)'),
shapes=[
{
'type':'line','x0': global_temp[:1].Year.values[0],'y0': global_temp[:1].Glob_C.values[0],
'x1': global_temp[-1:].Year.values[0],'y1': global_temp[:1].Glob_C.values[0],
'line': {'color': 'rgb(178,34,34)','width': 2}
},
{
'type':'line','x0': global_temp[:1].Year.values[0],'y0': global_temp[-1:].Glob_C.values[0],
'x1': global_temp[-1:].Year.values[0],'y1': global_temp[-1:].Glob_C.values[0],
'line': {'color': 'rgb(178,34,34)','width': 2}
}
])
annotations = []
annotations.append(dict(xref='paper', yref='paper', x=0.5, y=-0.1,
xanchor='center', yanchor='top',
text='Fonte: National Aeronautics and Space Administration (NASA)',
font=dict(family='Arial',
size=12,
color='rgb(150,150,150)'),
showarrow=False)
)
annotations.append(dict(x=global_temp[-1:].Year.values[0],y=global_temp[:1].Glob_C.values[0],xref='x',yref='y',
text=str(global_temp[:1].Year.values[0])+': '+str(global_temp[:1].Glob_C.values[0])+' C°',
ax=0,ay=-10)
)
annotations.append(dict(x=global_temp[-1:].Year.values[0],y=global_temp[-1:].Glob_C.values[0],xref='x',yref='y',
text=str(global_temp[-1:].Year.values[0])+': '+str(global_temp[-1:].Glob_C.values[0])+' C°',
ax=0,ay=-10)
)
layout['annotations'] = annotations
fig = dict(data=data, layout=layout)
return fig
'''% Riduzione Gas Serra'''
def perc_rid(df_all):
hover_text = []
for index, row in df_all.iterrows():
paese = 'Paese: ' + str(row['Name'])
perc_rid = '% riduzione: '+ '{:.1%}'.format(row['perc_rid']/100)
gdp = 'PIL: '+ '{:,}'.format(row['GDP'])+'M€'
pop = 'Popolazione: '+ '{:,}'.format(row['Habitant'])+'k'
hover_text.append(paese + '<br>' + perc_rid + '<br>' + gdp + '<br>' + pop)
trace0 = go.Scatter(
x=df_all['GDP'],
y=df_all['Habitant'],
mode='markers',
text = hover_text,
hoverinfo='text',
marker=dict(
color = df_all['perc_rid'],
colorbar=dict(title='% riduzione'),
colorscale='Blues',
size=df_all['perc_rid']
)
)
layout = go.Layout(
title='% di Riduzione dei Gas Serra rispetto al 1990',
xaxis=dict(
title='Prodotto Interno Lordo (PIL) in milioni di €'+'<br>'+'<i>Fonte: Eurostat</i>',
type='log',
autorange=True
),
yaxis=dict(
title='Numero di abitanti in migliaia',
type='log',
autorange=True
)
)
data = [trace0]
fig = go.Figure(data=data, layout=layout)
return fig
'''% Riduzione Gas Serra - Mappa'''
def perc_rid_map(df_all):
hover_text = []
for index, row in df_all.iterrows():
paese = 'Paese: ' + str(row['Name'])
perc_rid = '% riduzione: '+ '{:.1%}'.format(row['perc_rid']/100)
gdp = 'PIL: '+ '{:,}'.format(row['GDP'])+'M€'
pop = 'Popolazione: '+ '{:,}'.format(row['Habitant'])+'k'
hover_text.append(paese + '<br>' + perc_rid + '<br>' + gdp + '<br>' + pop)
data = [go.Choropleth(
colorscale = 'Blues',
autocolorscale = False,
locations = df_all['Name'],
z = df_all['perc_rid'],
locationmode = 'country names',
text = hover_text,
marker = go.choropleth.Marker(
line = go.choropleth.marker.Line(
color = 'rgb(255,255,255)',
width = 2
)),
colorbar = go.choropleth.ColorBar(
title = "% Riduzione")
)]
layout = go.Layout(
title = 'Mappa % di Riduzione dei Gas Serra rispetto al 1990',
geo = go.layout.Geo(
scope = 'europe',
projection = go.layout.geo.Projection(type = 'natural earth'),
showlakes = True,
lakecolor = 'rgb(255, 255, 255)'),
)
fig = go.Figure(data = data, layout = layout)
return fig
'''% Spesa Clima'''
def perc_spesa(df_all):
hover_text = []
for index, row in df_all.iterrows():
paese = 'Paese: ' + str(row['Name'])
perc_spesa = '% spesa clima: '+ '{:.1%}'.format(row['perc_spesa']/100)
gdp = 'PIL: '+ '{:,}'.format(row['GDP'])+'M€'
pop = 'Popolazione: '+ '{:,}'.format(row['Habitant'])+'k'
hover_text.append(paese + '<br>' + perc_spesa + '<br>' + gdp + '<br>' + pop)
trace0 = go.Scatter(
x=df_all['GDP'],
y=df_all['Habitant'],
mode='markers',
text = hover_text,
hoverinfo='text',
marker=dict(
color = df_all['perc_spesa'],
colorbar=dict(title='% spesa'),
colorscale='Blues',
size=df_all['perc_spesa']*100
)
)
layout = go.Layout(
title='% di Spesa per il Clima rispetto il Totale della Spesa (2016)',
xaxis=dict(
title='Prodotto Interno Lordo (PIL) in milioni di €'+'<br>'+'<i>Fonte: Eurostat</i>',
type='log',
autorange=True
),
yaxis=dict(
title='Numero di abitanti in migliaia',
type='log',
autorange=True
)
)
data = [trace0]
fig_spesa = go.Figure(data=data, layout=layout)
return fig_spesa