zoukankan      html  css  js  c++  java
  • python 形状区分市控国控,颜色表示aqi等级

    # 任务需求 从数据库取重点时刻前后各两小时的aqi求平均,画点图、面图

    import pandas as pd
    from datetime import timedelta, datetime
    import numpy as np
    from sqlalchemy import create_engine
    import matplotlib as mpl

    mpl.use("Agg")
    import matplotlib.pyplot as plt
    from mpl_toolkits.basemap import Basemap
    import matplotlib.font_manager as fm

    zhfont1 = fm.FontProperties(fname='/home/gzblue/xgx/xx_copy_files/SIMKAI.TTF')


    def get_station_aqi_mean(station, t_start, t_end):
    """求站点重点时刻平均AQI

    :param station: 站点ID,例如'51010101001'
    :param t_start: 开始时间,time类型或类似时间str
    :param t_end: 结束时间,time类型或类似时间str
    :return: 重点时刻平均AQI
    """

    sql_engine = create_engine('mssql+pymssql://xxxx:xxxx@xxxx:xxxx/AQI_HKY')
    data = pd.read_sql_query(
    "SELECT MN_, DATA_TIME_,AQI_ FROM MS_HOUR_DATA where MN_='%s' and DATA_TIME_>='%s' and DATA_TIME_<='%s'"
    % (station, t_start, t_end), sql_engine)
    # print(data)
    # print(data['AQI_'].mean())
    return data['AQI_'].mean()


    def main(station, goal_time):
    titlename = str(goal_time)
    savepath = r'/data01/home/gzblue/xgx/1205/chengduguoshi_plot/data/' + str(goal_time) + 'new_.png'
    time_mid = datetime.strptime(goal_time, '%Y%m%d%H')
    time_start = time_mid - timedelta(minutes=120)
    time_end = time_mid + timedelta(minutes=120)
    print(time_start)
    print(time_end)
    # 取数据
    station['AQI'] = None
    for mn in station['MN_'].values:
    station.loc[mn, 'AQI'] = get_station_aqi_mean(mn, time_start, time_end)
    print(station)
    # 画图
    station_list = ['51010101001', '51010401001', '51010501001', '51010501002',
    '51010601002', '51010801001', '51010901001']
    map = Basemap(llcrnrlon=102.8, llcrnrlat=30, urcrnrlon=105, urcrnrlat=31.5, projection='lcc',
    lat_1=30, lat_2=60, lat_0=33.3, lon_0=103.3, resolution='l')
    station.index = range(len(station))
    for ss in station_list:
    for i in range(40):
    if station.loc[i, 'MN_'] == ss:
    lons1 = station.loc[i, 'LONG_']
    lats1 = station.loc[i, 'LAT_']
    x1, y1 = map(lons1, lats1)
    if station.loc[i, 'AQI'] < 50:
    map.scatter(x1, y1, marker='s', color='limegreen', s=20)
    elif station.loc[i, 'AQI'] < 100:
    map.scatter(x1, y1, marker='s', color='yellow', s=20)
    elif station.loc[i, 'AQI'] < 150:
    map.scatter(x1, y1, marker='s', color='darkorange', s=20)
    elif station.loc[i, 'AQI'] < 200:
    map.scatter(x1, y1, marker='s', color='red', s=20)
    elif station.loc[i, 'AQI'] < 300:
    map.scatter(x1, y1, marker='s', color='purple', s=20)
    elif station.loc[i, 'AQI'] >= 300:
    map.scatter(x1, y1, marker='s', color='darkred', s=20)
    else:
    pass
    else:
    lons1 = station.loc[i, 'LONG_']
    lats1 = station.loc[i, 'LAT_']
    x1, y1 = map(lons1, lats1)
    if station.loc[i, 'AQI'] < 50:
    map.scatter(x1, y1, marker='v', color='limegreen', s=20, alpha=0.5)
    elif station.loc[i, 'AQI'] < 100:
    map.scatter(x1, y1, marker='v', color='yellow', s=20, alpha=0.5)
    elif station.loc[i, 'AQI'] < 150:
    map.scatter(x1, y1, marker='v', color='darkorange', s=20, alpha=0.5)
    elif station.loc[i, 'AQI'] < 200:
    map.scatter(x1, y1, marker='v', color='red', s=20, alpha=0.5)
    elif station.loc[i, 'AQI'] < 300:
    map.scatter(x1, y1, marker='v', color='purple', s=20, alpha=0.5)
    elif station.loc[i, 'AQI'] >= 300:
    map.scatter(x1, y1, marker='v', color='darkred', s=20, alpha=0.5)
    else:
    pass
    map.scatter(0, 0, marker='v', color='k', s=35, label='市控站点')
    map.scatter(0, 0, marker='s', color='k', s=35, label='国控站点')
    map.scatter(0, 0, marker='8', color='limegreen', s=35, label='优')
    map.scatter(0, 0, marker='8', color='yellow', s=35, label='良')
    map.scatter(0, 0, marker='8', color='darkorange', s=35, label='轻度污染')
    map.scatter(0, 0, marker='8', color='red', s=35, label='中度污染') map.
    scatter(0, 0, marker='8', color='purple', s=35, label='重度污染') map.
    scatter(0, 0, marker='8', color='darkred', s=35, label='严重污染') map.
    scatter(0, 0, marker='8', color='w', s=70) plt.
    legend(prop=zhfont1, fontsize=8, ncol=2, loc=0, columnspacing=0.2, handletextpad=0.2) map.
    readshapefile("/data01/home/share/chengdu_map_new/chengdu_city_merge_new", 'states', drawbounds=True,
    linewidth=0.3, color='gray') map.
    readshapefile("/data01/home/share/OnlyChengdu_Map/chengdu_xzqh_city", 'states', drawbounds=True, linewidth=0.8) plt.
    title(titlename, fontsize=15, fontproperties=zhfont1) plt.
    savefig(savepath, format='png', dpi=300)
    # plt.show()
    plt.close()
    print('完成')


    if __name__ == '__main__':
    # 获得国控与市控的列表
    station_data = pd.read_csv(r'/home/gzblue/xgx/1205/chengduguoshi_plot/data/guoshikong.csv')
    # print(station_data)
    # print(station)
    # print(station.loc['51010101001','NAME_'])

    # goal_time_list = ['2018121612'] # '2018112011'
    goal_time_list = ['2018121512','2018121612','2018121712','2018121812','2018121912','2018122011'] # '2018112011'
    for goal_time in goal_time_list:
    station_new = pd.DataFrame() station_new[
    'MN_'] = station_data['MN_'] station_new[
    'NAME_'] = station_data['NAME_'] station_new[
    'LONG_'] = station_data['LONG_'] station_new[
    'LAT_'] = station_data['LAT_'] station_new.
    set_index('MN_', drop=False, inplace=True)
    print(goal_time)
    main(station_new, goal_time)
  • 相关阅读:
    未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包
    Only one instance of a ScriptManager can be added to the page.
    springboot SSM
    spring mvc 请求转发和重定向
    Spring Boot 配置 Swagger2 接口文档引擎
    solr
    jQuery
    反向代理负载均衡-Nginx
    mybatis二级缓存
    Vue的路由
  • 原文地址:https://www.cnblogs.com/avivi/p/11238236.html
Copyright © 2011-2022 走看看