嗨,
我发现了一篇关于宠物排放的小文章,因此我决定显示二氧化碳排放(如果它们不存在)。
代码:
https://github.com/victordalet/kaggle_analysis/tree/feat/dog_co2
来源:
https://www.lekaba.fr/article/l-empreinte-carbone-des-chiens-et-des-chats-un-amour-qui-pese-lourd-sur-le-climat
https://www.umweltbundesamt.de/en/image/global-f-gas-emissions-up-to-2050-total
https://www.rover.com/fr/blog/combien-y-a-t-il-de-chats-dans-le-monde/
i-获取数据
首先,我获得了估算世界二氧化碳消耗量、狗和猫的平均排放量以及这些宠物的数量的数据。
import plotly.express as px
class main:
def __init__(self):
self.estimation = {
"2005": 750,
"2010": 900,
"2020": 1300,
"2030": 1800,
"2040": 2700,
"2050": 4000,
}
self.estimation_no_cat = {
"2005": 750,
"2010": 900,
"2020": 1300,
"2030": 1800,
"2040": 2700,
"2050": 4000,
}
self.estimation_no_dog = {
"2005": 750,
"2010": 900,
"2020": 1300,
"2030": 1800,
"2040": 2700,
"2050": 4000,
}
self.estimation_no_cat_and_dog = {
"2005": 750,
"2010": 900,
"2020": 1300,
"2030": 1800,
"2040": 2700,
"2050": 4000,
}
self.cat_emission = 240
self.dog_emission = 358
self.nb_cats = 600000000
self.nb_dogs = 900000000
二、转型
总排放量以百万吨为单位,因此我创建了一种转换动物数据的方法,单位为公斤。
@staticmethod
def transform_to_million_of_tonnes(value):
return value / (1000000 * 1000)
iii - 计算
要修改没有猫或狗的估计,请执行第一个估计,并将其他字典的值替换为第一步中找到的值。
def calculate(self):
for year, value in self.estimation.items():
self.estimation_no_cat[year] = value - self.transform_to_million_of_tonnes(
self.cat_emission * self.nb_cats
)
self.estimation_no_dog[year] = value - self.transform_to_million_of_tonnes(
self.dog_emission * self.nb_dogs
)
self.estimation_no_cat_and_dog[year] = (
value
- self.transform_to_million_of_tonnes(self.cat_emission * self.nb_cats)
- self.transform_to_million_of_tonnes(self.dog_emission * self.nb_dogs)
)
iv - 显示结果
为了显示包含所有数据的图表,我使用了plotly 库。
pip 安装代码:
pip install plotly
显示三个估计值的代码:
def display(self):
fig = px.line(
x=list(self.estimation.keys()),
y=[
list(self.estimation.values()),
list(self.estimation_no_cat.values()),
list(self.estimation_no_dog.values()),
list(self.estimation_no_cat_and_dog.values()),
],
labels={
"x": "Year",
"y": "CO2 Emission (in million of tonnes)",
"color": "Legend",
},
title="CO2 Emission with and without cats and dogs",
color_discrete_map={
"CO2 Emission": "blue",
"CO2 Emission without cats": "green",
"CO2 Emission without dogs": "red",
"CO2 Emission without cats and dogs": "orange",
},
)
fig.show()
现在我们有了包含结果的图表。