-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
411 lines (341 loc) · 12.8 KB
/
plot.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
from rng import q_1_rng, q_2_rng, q_3_rng, q_4_rng, q_5_rng, q_6_rng, q_6_rng, q_7_rng, q_8_rng, q_9_rng, q_10_rng
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as pltlyexp
import numpy as np
import seaborn as sns
import plotly.figure_factory as ff
from plotly.offline import iplot
import plotly.offline
import plotly.graph_objs as go
import random
q1_df = q_1_rng()
q2_df = q_2_rng()
q3_df = q_3_rng()
q4_df = q_4_rng()
q5_df = q_5_rng()
q7_df_bar = q_7_rng(isBar = True)
q7_df_heat = q_7_rng(isBar = False)
q6_df = q_6_rng()
q8_df = q_8_rng()
q9_df = q_9_rng()
q10_df = q_10_rng()
def plot_bar_q1():
ax = q1_df.plot.bar(x="countries", title = 'Plastic pollution of different countries').set_ylabel("Plastic Pollution")
plt.show()
def plot_bar_q2():
ax = q2_df.plot.bar(x="countries", title = 'Plastic pollution of different countries').set_ylabel("Plastic Pollution")
plt.show()
def plot_bar_q3():
ax = q3_df.plot.bar(x="countries", title = 'Plastic pollution of different countries').set_ylabel("Plastic Pollution")
plt.show()
def plot_bar_q4():
ax = q4_df.plot.bar(x="countries", title = 'Plastic pollution of different countries').set_ylabel("Plastic Pollution")
plt.show()
def plot_bar_q5():
plt.clf()
ax = q5_df.plot.bar(x="countries", stacked = True, title = 'Different types of plastic pollution of different countries').set_ylabel("Plastic Pollution")
plt.legend(title="Plastic Types",
loc='upper right', fontsize='small', fancybox=True)
plt.show()
def plot_choropleth_q1():
data = dict(type = 'choropleth',
locations =q10_df['countries'],
locationmode = 'country names',
colorscale= 'greens',
text= q1_df['countries'],
z=q1_df['PlasticPolution'],
colorbar = {'title':'Polution produced'})
layout = dict(geo={'scope':'europe'})
chmap = go.Figure(data=[data],layout=layout)
chmap.update_layout(
title_text = 'Polution produced by western countries'
)
chmap.show()
def plot_tree_q2():
sizes = q2_df['PlasticPolution'].values
labels = q2_df['countries'].values
fig = pltlyexp.treemap(q2_df, path=[labels], values=sizes, title="Countries which produces most polution")
fig.show()
def plot_tree_q3():
sizes = q3_df['PlasticPolution'].values
labels = q3_df['countries'].values
fig =pltlyexp.treemap(q3_df, path = [labels],values = sizes, color = q3_df['PlasticPolution'], title="Polution produced by western countries")
fig.show()
def plot_tree_q4():
sizes = q4_df['PlasticPolution'].values
labels = q4_df['countries'].values
fig = pltlyexp.treemap(q4_df, path=[labels], values=sizes, title="Polution produced by western countries")
fig.show()
def plot_heat_q5():
countries = q5_df['countries']
Polution_Type = ["PET", "HDPE", "LDPE", "PVC"]
harvest = np.array([q5_df['PET'],
q5_df['HDPE'],
q5_df['LDPE'],
q5_df['PVC']])
fig, ax = plt.subplots()
im = ax.imshow(harvest)
# Show all ticks and label them with the respective list entries
ax.set_xticks(np.arange(len(countries)))
ax.set_yticks(np.arange(len(Polution_Type)))
ax.set_xticklabels(countries)
ax.set_yticklabels(Polution_Type)
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
# Loop over data dimensions and create text annotations.
for i in range(len(Polution_Type)):
for j in range(len(countries)):
text = ax.text(j, i, harvest[i, j],
ha="center", va="center", color="w")
ax.set_title("Types of Plastic Polution emmitted ")
fig.tight_layout()
plt.show()
def plot_bar_q6():
plt.clf()
ax = q6_df.plot.bar(x='Countries', stacked=True, title='Plastic consumption (weight) vs pollution emmission (CO2) for countries in Western Europe').set_ylabel("Metric tonnes (kg)")
plt.legend(title="Measure",
loc='upper right', fontsize='small', fancybox=True)
plt.show()
def plot_tree_q6():
sizes = q6_df['Plastic consumption'].values
labels = q6_df['Countries'].values
fig = pltlyexp.treemap(q6_df, path = [labels],values = sizes, color = q6_df['Pollution emmission'], title="Plastic consumption shown by size (weight) vs pollution emmission (CO2) for countries in Western Europe")
fig.show()
def plot_bar_q7():
plt.clf()
my_colors = list((['b', 'r', 'g', 'y', 'm']))
labels = [2016, 2017, 2018, 2019, 2020]
q7_df_bar.unstack().plot(kind='bar', stacked=True, color=my_colors)
plt.xlabel("Country")
plt.ylabel("Metric tonnes (kg)")
plt.title("Plastic pollution emmission (CO2) by countries over time")
plt.legend(labels=labels, title="Year",
loc='upper right', fontsize='small', fancybox=True)
plt.show()
def plot_heat_q7():
q7_df_heat2 = q7_df_heat.pivot(index='Countries',columns='Year')
q7_df_heat2.drop(q7_df_heat2.tail(1).index,inplace=True)
q7_df_heat2.columns = q7_df_heat2.columns.droplevel(0)
plt.rcParams['font.size'] = '9'
ax = sns.heatmap(q7_df_heat2)
plt.show()
def plot_bar_q8():
plt.clf()
ax = q8_df.plot.bar(x='Countries', stacked=True, title="Recyclable plastic (%) vs Non-recyclable plastic (%) for countries in Western Europe")
plt.legend(title="Percentage",
loc='upper right', fontsize='small', fancybox=True)
plt.show()
def plot_tree_q8():
sizes = q8_df['Recyclable plastic %'].values
labels = q8_df['Countries'].values
fig = pltlyexp.treemap(q8_df, path = [labels],values = sizes, color = q8_df['Non-Recyclable plastic %'], title="Recyclable plastic (%) shown by size vs Non-recyclable plastic (%) for countries in Western Europe")
fig.show()
def plot_bar_q9():
q9_df.set_index('countries').plot(kind='bar', rot=0, title="Plastic Waste Generated per person (2022)").set_ylabel("Plastic Waste Per Person Per Year (Kg)")
plt.show()
#make user interact with tree
def plot_tree_q9():
labels = q9_df['countries'].values
sizes = q9_df['plastic waste per capita'].values
fig = pltlyexp.treemap(q9_df, path=[labels], values=sizes, title="Plastic Waste Generated per person (2022)")
fig.show()
def plot_choropleth_q10():
data = dict(type = 'choropleth',
locations =q10_df['countries'],
locationmode = 'country names',
colorscale= 'greens',
text= q10_df['countries'],
z=q10_df['money spent processing plastic'],
colorbar = {'title':'Amount Spent'})
layout = dict(geo={'scope':'europe'})
chmap = go.Figure(data=[data],layout=layout)
chmap.update_layout(
title_text = 'Amount of money spent recycling plastic in 2022'
)
chmap.show()
#Which country spends the most money recycling plastic?
def plot_bar_q10():
q10_df.set_index('countries').plot(kind='bar', rot=0, title="Amount of money spent recycling plastic per country (2022)").set_ylabel("Money spent (€)")
plt.show()
def return_options_q1():
q1_df_sorted = q1_df
q1_df_sorted = q1_df_sorted.sort_values(by = ['PlasticPolution'], ascending=False).head(4)
options = q1_df_sorted['PlasticPolution'].to_list()
for i in range(len(options)):
options[i] = [options[i], 0]
options[0][1] = 1
random.shuffle(options)
return options
def return_options_q2():
q2_df_sorted = q2_df
q2_df_sorted = q2_df_sorted.sort_values(by = ['PlasticPolution'], ascending=False).head(4)
options = q2_df_sorted['countries'].to_list()
for i in range(len(options)):
options[i] = [options[i], 0]
options[0][1] = 1
random.shuffle(options)
return options
def return_options_q3():
q3_df_sorted = q3_df
q3_df_sorted = q3_df_sorted.sort_values(by = ['PlasticPolution'], ascending=True).head(4)
options = q3_df_sorted['PlasticPolution'].to_list()
for i in range(len(options)):
options[i] = [options[i], 0]
options[0][1] = 1
random.shuffle(options)
return options
def return_options_q4():
q4_df_sorted = q4_df
q4_df_sorted = q4_df_sorted.sort_values(by = ['PlasticPolution'], ascending=True).head(4)
options = q4_df_sorted['countries'].to_list()
for i in range(len(options)):
options[i] = [options[i], 0]
options[0][1] = 1
random.shuffle(options)
return options
def return_options_q5():
q5_df_sorted = q5_df
q5_df_sorted = q5_df_sorted.head(1)
options = ['PET', 'HDPE', 'LDPE','PVC']
x = 0
high = ''
for i in options:
if q5_df_sorted[i][0]>x:
x = q5_df_sorted[i][0]
high = i
options2 = []
options2.append([high, 1])
for i in options:
options2.append([i, 0])
options2.remove([high, 0])
random.shuffle(options2)
return options2
# returns shuffled list of options for q6
def return_options_q6():
q6_df_sorted = q6_df
q6_df_sorted['Ratio'] = q6_df['Plastic consumption']/q6_df['Pollution emmission']
q6_df_sorted = q6_df_sorted.sort_values(by=['Ratio'],ascending=False).head(4)
options = q6_df_sorted['Countries'].to_list()
for i in range(len(options)):
options[i] = [options[i], 0]
options[0][1] = 1
random.shuffle(options)
return options
def return_options_q7(type):
if type == "bar":
q7_df_options = q7_df_bar.groupby('Countries')['Pollution emmission'].sum().reset_index()
else:
q7_df_options = q7_df_heat.groupby('Countries')['Pollution emmission'].sum().reset_index()
q7_df_options = q7_df_options.sort_values(by='Pollution emmission', ascending=False).head(4)
options = q7_df_options['Pollution emmission'].to_list()
for i in range(len(options)):
options[i] = [options[i], 0]
options[0][1]=1
random.shuffle(options)
return options
#returns shuffled list of options for q8
def return_options_q8():
q8_df_sorted = q8_df
q8_df_sorted['Ratio'] = q8_df['Recyclable plastic %']/q8_df['Non-Recyclable plastic %']
q8_df_sorted = q8_df_sorted.sort_values(by=['Ratio'],ascending=False).head(4)
options = q8_df_sorted['Countries'].to_list()
for i in range(len(options)):
options[i] = [options[i], 0]
options[0][1] = 1
random.shuffle(options)
return options
#returns shuffled list of options for q9
def return_options_q9():
q9_df_sorted = q9_df.sort_values(by=['plastic waste per capita'], ascending=False).head(4)
options = q9_df_sorted['countries'].to_list()
for i in range(len(options)):
options[i] = [options[i], 0]
options[0][1] = 1
random.shuffle(options)
return options
#returns shuffled list of options for q10
def return_options_q10():
q10_df_sorted = q10_df.sort_values(by=['money spent processing plastic'], ascending=False).head(4)
options = q10_df_sorted['countries'].to_list()
for i in range(len(options)):
options[i] = [options[i], 0]
options[0][1] = 1
random.shuffle(options)
return options
def display_charts(x,type):
if x == 1:
if type == "bar":
plot_bar_q1()
else:
plot_choropleth_q1()
if x == 2:
if type == "bar":
plot_bar_q2()
else:
plot_tree_q2()
if x == 3:
if type == "bar":
plot_bar_q3()
else:
plot_tree_q3()
if x == 4:
if type == "bar":
plot_bar_q4()
else:
plot_tree_q4()
if x == 5:
if type == "bar":
plot_bar_q5()
else:
plot_heat_q5()
if x == 6:
if type == "bar":
plot_bar_q6()
else:
plot_tree_q6()
if x == 7:
if type == "bar":
plot_bar_q7()
else:
plot_heat_q7()
if x == 8:
if type == "bar":
plot_bar_q8()
else:
plot_tree_q8()
if x == 9:
if type == "bar":
plot_bar_q9()
else:
plot_tree_q9()
if x == 10:
if type == "bar":
plot_bar_q10()
else:
plot_choropleth_q10()
#need to finish this
def return_options(x,type):
to_return = []
if x == 1:
to_return = return_options_q1()
if x == 2:
to_return = return_options_q2()
if x == 3:
to_return = return_options_q3()
if x == 4:
to_return = return_options_q4()
if x == 5:
to_return = return_options_q5()
if x == 6:
to_return = return_options_q6()
if x == 7:
to_return = return_options_q7(type)
if x == 8:
to_return = return_options_q8()
if x == 9:
to_return = return_options_q9()
if x == 10:
to_return = return_options_q10()
return to_return