ixxmu / mp_duty

抓取网络文章到github issues保存
https://archives.duty-machine.now.sh/
122 stars 30 forks source link

实时碳数据可视化[第二篇] #1488

Closed ixxmu closed 2 years ago

ixxmu commented 2 years ago

https://mp.weixin.qq.com/s/c4OxtPB-Td5kSaJWZsYtqw

github-actions[bot] commented 2 years ago

实时碳数据可视化[第二篇] by world of statistics

前言

昨天分享了 碳排放数据的爬虫教程,当时看网上还没有给出对应的教程,然后发布的就比较匆忙,没有做可视化部分。这里基于上一篇的内容,继续把碳排放数据的可视化给做了。

想要了解更多细节的,可以查看我这篇文章👇:

[原创]全球实时碳数据网站爬虫[全网首发 免费分享]

文章代码和数据,我也已经放在了GitHub上了。链接在上篇文章里面。


画图

导入包

```python
import requests

from bs4 import BeautifulSoup
from itertools import chain
import re
import pandas as pd
import datetime
from itertools import chain

import os
import shutil
import time
import random

from tqdm import tqdm

import plotly.graph_objects as go

下载数据

## 下载所有数据
def download_region(by):
    data = get_carbon_data(by=by)
    data['region_name'] = by
    return data


all_data_list = [download_region(by=i) for i in tqdm(region_list)]

各地区整体对比

第一个图是对比各个国家的整体碳排放情况。我这里写了代码并且提供了一个展示结果,有很多交互按钮。

fig = go.Figure()
for index in tqdm(range(len(region_chinese_list))):
    if region_chinese_list[index] == '中国':
        fig.add_trace(go.Scatter(x=all_data_list[index]['date'],
                                 y=all_data_list[index][region_chinese_list[index]],
                                 name=region_chinese_list[index], mode='lines+markers'))
    else:
        fig.add_trace(go.Scatter(x=all_data_list[index]['date'],
                                 y=all_data_list[index][region_chinese_list[index]],
                                 name=region_chinese_list[index], mode='lines'))
fig.update_layout(template='simple_white',
                  xaxis=dict(
                      rangeselector=dict(
                          buttons=list([
                              dict(count=1,
                                   label="1m",
                                   step="month",
                                   stepmode="backward"),
                              dict(count=6,
                                   label="6m",
                                   step="month",
                                   stepmode="backward"),
                              dict(count=1,
                                   label="YTD",
                                   step="year",
                                   stepmode="todate"),
                              dict(count=1,
                                   label="1y",
                                   step="year",
                                   stepmode="backward"),
                              dict(step="all")
                          ])
                      ),
                      rangeslider=dict(
                          visible=True
                      ),
                      type="date"
                  ),
                  title="全球各地区实时碳数据",
                  xaxis_title="date",
                  yaxis_title="MtCO_2 per day",
                  legend_title="地区",
                  font=dict(
                      family="Courier New, monospace",
                      size=18,
                      color="RebeccaPurple"
                  ))
fig.show()
fig.write_html('全球碳数据.html')

各地区的行业对比

因为代码样式差不多,我这里直接写了个函数,后面直接更改函数里面的参数即可。代码如下:

def plot_hangye(hangye):
    """
    这个函数是从各行业的维度,查看各国的每天的碳排放。
    :param hangye:输入的内容是:'Domestic Aviation',
         'Ground Transport',
         'Industry',
         # 'International Aviation',
         'Power',
         'Residential','Industry'

    :return: none 返回的就是一个plotly图,并且将图都保存起来
    """

    hangye_list = ['Domestic Aviation''Industry',
                   'Ground Transport',
                   'Industry',
                   # 'International Aviation',
                   'Power',
                   'Residential']

    if hangye not in hangye_list:
        raise ValueError(f"输入的:{hangye} 不在列表里面")
    fig = go.Figure()
    for index in tqdm(range(len(region_chinese_list))):
        if region_chinese_list[index] == '中国':
            fig.add_trace(go.Scatter(x=all_data_list[index]['date'],
                                     y=all_data_list[index][hangye],
                                     name=region_chinese_list[index], mode='lines+markers'))
        else:
            fig.add_trace(go.Scatter(x=all_data_list[index]['date'],
                                     y=all_data_list[index][region_chinese_list[index]],
                                     name=region_chinese_list[index], mode='lines'))
    fig.update_layout(template='simple_white',
                      xaxis=dict(
                          rangeselector=dict(
                              buttons=list([
                                  dict(count=1,
                                       label="1m",
                                       step="month",
                                       stepmode="backward"),
                                  dict(count=6,
                                       label="6m",
                                       step="month",
                                       stepmode="backward"),
                                  dict(count=1,
                                       label="YTD",
                                       step="year",
                                       stepmode="todate"),
                                  dict(count=1,
                                       label="1y",
                                       step="year",
                                       stepmode="backward"),
                                  dict(step="all")
                              ])
                          ),
                          rangeslider=dict(
                              visible=True
                          ),
                          type="date"
                      ),
                      title=f"行业: {hangye} 实时碳数据",
                      xaxis_title="date",
                      yaxis_title="MtCO_2 per day",
                      legend_title="地区",
                      font=dict(
                          family="Courier New, monospace",
                          size=18,
                          color="RebeccaPurple"
                      ))
    fig.show()
    fig.write_html(f'{hangye}.html')

# test function
plot_hangye(hangye='Industry')

批量画图:


hangye_list = ['Domestic Aviation''Industry',
               'Ground Transport',
               'Industry',
               # 'International Aviation',
               'Power',
               'Residential']

for i in hangye_list:
    plot_hangye(i)

后续

1. 上面运行生产的内容都保存在文件夹中,大家可以直接在文件夹中查看。
2. 数据来源已经数据的爬取,可以看我之前的那篇文章。
3. 如果爬虫脚本失败了,可以向我反馈。
4. 后续可能也会出一个R语言的爬虫教程。主要是看需求量了。


阅读更多


往期推荐



[原创]全球实时碳数据网站爬虫[全网首发 免费分享]

[自制]python获得中国地图数据_介绍

python获得百度指数脚本[免费分享]

[自制]创意的中国地图 3d版本(demo)

[喜大普奔]dataV对getchinamap包没多大意见

一次痛苦且有趣的查看sklearn源码经历(一次有趣的复现经历)