“This is the 23rd day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

preface

Using Python to realize the air quality data analysis and visualization of Beijing, Shanghai, Guangzhou and Shenzhen in 2018. Without further ado.

Let’s have a good time

The development tools

Python version: 3.6.4

Related modules:

Requests the module

Numpy module

Pandas module

Pyecharts module;

And some modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required related modules.

Web analytics

Annual AQI trend chart of Beijing, Shanghai, Guangzhou and Shenzhen

Here is a simple popular science about AQI, PM2.5 knowledge

Extract the data

Get data code

import time
import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent':'the Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
for i in range(1.13):
    time.sleep(5)
    Convert 1 to 01
    url = 'http://www.tianqihoubao.com/aqi/tianjin-2017' + str("%02d" % i) + '.html'
    response = requests.get(url=url, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')
    tr = soup.find_all('tr')
    # Remove the TAB bar
    for j in tr[1:]:
        td = j.find_all('td')
        Date = td[0].get_text().strip()
        Quality_grade = td[1].get_text().strip()
        AQI = td[2].get_text().strip()
        AQI_rank = td[3].get_text().strip()
        PM = td[4].get_text()
        with open('air_tianjin_2017.csv'.'a+', encoding='utf-8-sig'as f:
            f.write(Date + ', ' + Quality_grade + ', ' + AQI + ', ' + AQI_rank + ', ' + PM + '\n')
Copy the code

Data Obtained Successfully

Let’s start with Tianjin

AQI annual trend chart

92.5 is the average annual AQI value. As can be seen from the above popular science knowledge, tianjin’s overall air quality in 2017 is only at the lower level of “good”, close to mild pollution.

Monthly trend chart of AQI

From the monthly chart, we can see that the air quality in January is the worst, and the air quality in August is the best. When it is not very good, it is at best a “good”!

AQI quarterly box chart

Box chart, a statistical chart showing a set of data dispersion data.

There are maximum, minimum, median and two quartiles in the data.

It can be seen that the quarterly AQI mean difference of Tianjin in 2017 is not very large.

But there are obvious fluctuations in the first, second and fourth quarters, and the air quality sometimes becomes very bad.

Annual trend chart of PM2.5

59.87 is the average annual PM2.5 level, which is well above the second-level national limit of 35.

In fact, tianjin gave me the impression that the weather is often gray, often change a little color, such as yellow ~

It rains only a few times a year and is extremely dry. So the lowest value is 11.

Monthly trend chart of PM2.5

Similar to AQI, the highest in January and lowest in August.

PM2.5 quarterly box chart

Basically, it exceeded the standard in four quarters, and it’s only a few times a year.

Calendar chart of PM2.5 index

The average daily PM2.5 level 2 national standard is 75, and the heat map above shows that light pollution is more than half.

Annual air quality in Tianjin

“Good” and “light pollution” accounted for the majority, “excellent” can only shiver in the corner, enough to illustrate the poor air.

Let’s take a look at Beijing, Shanghai, Guangzhou and Shenzhen

Annual AQI trend chart of Beijing, Shanghai, Guangzhou and Shenzhen

Annual air quality in Beijing, Shanghai, Guangzhou and Shenzhen

Shenzhen is almost “excellent” and “good”, Shanghai and Guangzhou are the same, Beijing has a lot of “excellent”.