♚ \
Author: Yishui Hancheng, CSDN blog expert, personal research interests: machine learning, deep learning, NLP, CV
Blog: yishuihancheng.blog.csdn.net
Pollutant rose chart is a relatively domain-specific and specialized term, which is often used in the field of environmental data analysis. If it is not easy to understand pollutant rose chart at the beginning, here is a more general example, which is wind direction rose chart.
In order to better explain the academic term wind Direction rose chart, I found its relevant introduction from Baidu Encyclopedia, as follows:
Wind direction rose chart (referred to as wind rose chart) is also called wind direction frequency rose chart. It is based on the average statistics of various wind directions and wind speeds in a certain area for many years, and is drawn in proportion to a certain proportion. Generally, it is expressed by 8 or 16 compass directions, so it is named because its shape resembles a rose flower. The direction of the wind shown in the rose chart is the direction from the outside to the center of the region. The line segment drawn by each party upward according to the statistics indicates the wind frequency in this direction. The longer the line segment, the more frequent the wind direction occurs. The line segments representing wind frequency in each direction are drawn into different colored line segments according to the numerical percentage of wind speed, that is, the average wind speed of each wind direction is shown. Such statistical graphs are called wind frequency wind speed rose diagrams. Let’s take a quick look at some examples of roses, as follows:
A similar effect can be drawn by using echarts polar stacked histogram, but there is no way to set different colors for different ranges of concentration values. A simple effect is shown below:
In environmental protection, meteorology and other fields, wind direction rose chart is a very common means of data analysis and visualization. A similar rose chart, pollutant rose chart, is derived from it. It is similar to wind direction rose chart in terms of specific calculation logic and overall form. The difference is that the rose chart of pollutants is mainly used to analyze problems related to pollutant traceability and transmission, and the concentration data of pollutants are superimposed during visualization.
Want to draw contaminants rose first we need to get to the need of factor concentration data and corresponding meteorological data, here is our data comes from the national meteorological center and China’s environmental monitoring station, among them, the former provides us the required meteorological data, which provides the monitoring data of factor concentration we need.
The screenshot of the data area on the home page of China Environmental Monitoring Station is as follows:
Click on the details to view this part of data collection. I found open source implementation on the Internet and directly used it, so I won’t make more introductions here. If necessary, you can leave a message, AND I will provide you with the project link to obtain real-time air data of the city. Including AQI, PM2.5, PM10, CO, O3, NO2, SO2, primary pollutants, life advice, pollution description and other data.
The screenshot of the data area in the CENTRAL Meteorological Station is as follows:
Here I had the specific implementation, details please refer to my blog: yishuihancheng.blog.csdn.net/article/det…
I have been engaged in relevant research projects in my previous work. I have developed a relatively complete implementation of some commonly used tool modules, which can be easily called here. The specific Demo code implementation is as follows:
def plotPollutionRoseDemo(value,direction,title,save_path='demo.png') :' ''Pollutant Visualization Demo'' '
plt.clf()
ax=WindroseAxes.from_ax()
ax.bar(direction,value,normed=True,opening=0.8,edgecolor='white')
ax.set_legend(loc='best',bbox_to_anchor=(1.1.1.1)) #loc='lower center',ncol=3
plt.title(title,chinese)
plt.savefig(save_path)
Copy the code
The drawing code is really simple, and here is the result:
I think it is pretty, of course, if it needs to be put into the product, it may need to be further beautified.
There are some bloggers who need to set the parameters of drawing. Here I give them together. If necessary, you can use them directly:
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['font.sans-serif'] = ['SimHei'[plt.rcparams]'axes.unicode_minus']=False # English ={'family':'Times New Roman'.'weight':'normal'.'size':12}
chinese={'family':'SimHei'.'weight':'normal'.'size':12}
color_list=['#CD853F'.'#DC143C'.'#00FF7F'.'#FF6347'.'#8B008B'.'#00FFFF'.'#0000FF'.'#8B0000'.'#FF8C00'.'#1E90FF'.'#00FF00'.'#FFD700'.'# 008080'.'#008B8B'.'#8A2BE2'.'#228B22'.'#FA8072'.'# 808080']
Copy the code
Personally, I prefer to put some parameters in a dictionary and encapsulate them so that I can call them easily.
After the drawing module is developed, the API can be developed. It is a common practice to expose the local tool modules into API services that can be called directly based on flask. The specific code is as follows:
#! usr/bin/env python # encoding:utf-8
import os
import sys
import time
import json
from flask import Flask
from flask import request
from flask import make_response
from flask import jsonify
from windRosePloter import *
app = Flask(__name__)
reload(sys)
sys.setdefaultencoding("utf-8")
@app.route("/")
def init():
return u"Wind rosette module activated!"
@app.route('/onlinerose',methods=['GET'])
def windRoseFunc():
' ''the wind rose diagram visualization functions of http://IP:port/onlinerose? host=localhost&user=username&passwd=passwd&port=3306&db=database&table=tablename&code=code&factor=pm25&start=2019-01-01 00:00:00&end=2019-09-30 23:00:00 '' '
if request.method=='GET':
host=request.args['host']
user=request.args['user']
passwd=request.args['passwd']
port=request.args['port']
db=request.args['db']
table=request.args['table']
code=request.args['code']
factor=request.args['factor']
start=request.args['start']
end=request.args['end']
mysql['host']=host
mysql['user']=user
mysql['passwd']=passwd
mysql['port']=int(port)
mysql['db']=db # select_sql="SELECT wd,"+factor+" FROM "+table+" where station_code='"+code+"' AND data_time>='"+\
start+"' AND data_time<='"+end+"'"
#print select_sql
data=selectAllRecords(mysql, select_sql)
result=[]
for i in range(len(data)):
if data[i][0] and data[i][1] and data[i][0]! ='None'and data[i][1]! ='None':
result.append([float(data[i][0]),float(data[i][1])])
dd=[float(data[i][0]) for i in range(len(data)) if data[i][0]]
ff=[float(data[i][1]) for i in range(len(data)) if data[i][1]]
length=min(len(dd),len(ff))
wd=windDirectionTransform(dd)
title=factor+u"Factor Wind Roses"
pic_path='rosePics/'+code+'_'+factor+'.png'
plotPollutionRoseDemo(ff[:length],dd[:length],title,save_path=pic_path)
image_data=open(pic_path,"rb").read()
response=make_response(image_data)
response.headers['Content-Type'] ='image/png'
return response
if __name__ == "__main__":
app.run(host=0.0. 0. 0,port=5000)
Copy the code
After completing the above API development work, you can use the analysis, the following is a simple example test results:
The convenient invocation of apI-based implementation tools is very helpful for improving work efficiency or assisting field personnel to complete data analysis work.
I am very glad to write something to share while reviewing my knowledge. If you think my content can or is enlightening and helpful to you, I hope to get your encouragement and support!
Appreciate the author
Python Chinese community as a decentralized global technology community, to become the world’s 200000 Python tribe as the vision, the spirit of Chinese developers currently covered each big mainstream media and collaboration platform, and ali, tencent, baidu, Microsoft, amazon and open China, CSDN industry well-known companies and established wide-ranging connection of the technical community, Have come from more than 10 countries and regions tens of thousands of registered members, members from the ministry, tsinghua university, Peking University, Beijing university of posts and telecommunications, the People’s Bank of China, the Chinese Academy of Sciences, cicc, huawei, BAT, such as Google, Microsoft, government departments, scientific research institutions, financial institutions, and well-known companies at home and abroad, nearly 200000 developers to focus on the platform.
Click to become a registered member of the community