处理数据并进行克里金插值
# 处理数据后进行克里金插值
import pandas as pd
pm25_region=pd.read_csv(r"C:\Users\LHW\Desktop\Hubei task120210918\extract_pm25\PM25_region.csv")
del pm25_region['type']
year_list=['2019','2020','2021']
data_list=[]
for year in year_list:
pm25_region['date'] = pd.to_datetime(pm25_region['date'])
pm25_region1=pm25_region.set_index('date')
pm2019=pm25_region1[year+'-02':year+'-03']
pm2019_mean=pm2019.mean().to_frame()
pm2019_mean.columns=[year+'_pm25']
data_list.append(pm2019_mean)
ini=data_list[0]
ini=ini.join(data_list[1])
ini=ini.join(data_list[2])
loc=pd.read_csv("region_aim_loc_info.csv")
loc=loc.set_index('监测点编码')
total_info=loc.join(ini)
del total_info['Unnamed: 0']
total_info=total_info.dropna()
# 进行克里金插值
import numpy as np
import geopandas as gpd
js=gpd.read_file(r'Hubei.json')
js_box = js.geometry.total_bounds
grid_lon = np.linspace(js_box[0],js_box[2],400)
grid_lat = np.linspace(js_box[1],js_box[3],400)
lons = total_info["经度"].values
lats = total_info["纬度"].values
data = total_info["2019_pm25"].values
from pykrige.ok import OrdinaryKriging
OK = OrdinaryKriging(lons, lats, data, variogram_model='gaussian',nlags=6)
z1, ss1 = OK.execute('grid', grid_lon, grid_lat)
#转换成网格
xgrid, ygrid = np.meshgrid(grid_lon, grid_lat)
#将插值网格数据整理
df_grid =pd.DataFrame(dict(long=xgrid.flatten(),lat=ygrid.flatten()))
#添加插值结果
df_grid["Krig_gaussian"] = z1.flatten()
站点数据可视化
import plotnine
from plotnine import *
plotnine.options.figure_size = (6, 4)
idw_scatter = (ggplot() +
geom_map(js,fill='none',color='gray',size=0.6) +
geom_point(total_info,aes(x='经度',y='纬度',fill='2019_pm25'),size=5) +
scale_fill_cmap(cmap_name='Spectral_r',name='PM2.5',limits = (0, 100),
breaks=[30,40,60,80]
)+
scale_x_continuous(breaks=[108,110,112,114,116,118])+
#labs(title="Map Charts in Python Exercise 02: Map IDM point",)+
#添加文本信息
#annotate('text',x=116.5,y=35.3,label="processed map charts with plotnine",ha="left",size=10)+
#annotate('text',x=120,y=30.6,label="Visualization by DataCharm",ha="left",size=9)+
theme(
text=element_text(family = "SimHei"),
#修改背景
panel_background=element_blank(),
axis_ticks_major_x=element_blank(),
axis_ticks_major_y=element_blank(),
axis_text=element_text(size=12),
axis_title = element_text(size=14,weight="bold"),
panel_grid_major_x=element_line(color="gray",size=.5),
panel_grid_major_y=element_line(color="gray",size=.5),
))
idw_scatter.save('test.jpg',dpi=300)
克里金插值后数据可视化
import plotnine
from plotnine import *
plotnine.options.figure_size = (6.4, 4)
Krig_inter_grid = (ggplot() +
geom_tile(df_grid,aes(x='long',y='lat',fill='Krig_gaussian'),size=0.1) +
geom_map(js,fill='none',color='gray',size=0.6) +
scale_fill_cmap(cmap_name='Spectral_r',name='PM2.5',
breaks=[30,40,60,80]
)+
scale_x_continuous(breaks=[108,110,112,114,116,118])+
scale_y_continuous(breaks=[28,30,32,34])+
#labs(title="Map Charts in Python Exercise 02: Map point interpolation",)+
#添加文本信息
#annotate('text',x=116.5,y=35.3,label="processed map charts with plotnine",ha="left", size=10)+
#annotate('text',x=120,y=30.6,label="Visualization by DataCharm",ha="left",size=9)+
theme(
text=element_text(family="SimHei"),
#修改背景
panel_background=element_blank(),
axis_ticks_major_x=element_blank(),
axis_ticks_major_y=element_blank(),
axis_text=element_text(size=12),
axis_title = element_text(size=14),
panel_grid_major_x=element_line(color="gray",size=.5),
panel_grid_major_y=element_line(color="gray",size=.5),
))
Krig_inter_grid.save('test1.jpg',dpi=300)
裁剪后数据可视化
df_grid_geo = gpd.GeoDataFrame(df_grid, geometry=gpd.points_from_xy(df_grid["long"], df_grid["lat"]),crs="EPSG:4326")
#根据之前的js geojson格式的地图文件进行裁剪
js_kde_clip = gpd.clip(df_grid_geo,js)
import plotnine
from plotnine import *
plotnine.options.figure_size = (6.4, 4)
Krig_inter_grid = (ggplot() +
geom_tile(js_kde_clip,aes(x='long',y='lat',fill='Krig_gaussian'),size=0.1) +
geom_map(js,fill='none',color='gray',size=0.6) +
scale_fill_cmap(cmap_name='Spectral_r',name='PM2.5',
breaks=[30,40,60,80]
)+
scale_x_continuous(breaks=[108,110,112,114,116,118])+
scale_y_continuous(breaks=[29,31,33,35])+
#labs(title="Map Charts in Python Exercise 02: Map point interpolation",)+
#添加文本信息
#annotate('text',x=116.5,y=35.3,label="processed map charts with plotnine",ha="left", size=10)+
#annotate('text',x=120,y=30.6,label="Visualization by DataCharm",ha="left",size=9)+
theme(
text=element_text(family="SimHei"),
#修改背景
panel_background=element_blank(),
axis_ticks_major_x=element_blank(),
axis_ticks_major_y=element_blank(),
axis_text=element_text(size=12),
axis_title = element_text(size=14),
panel_grid_major_x=element_line(color="gray",size=.5),
panel_grid_major_y=element_line(color="gray",size=.5),
))
Krig_inter_grid.save('test2.jpg',dpi=300)
参考文献:
https://www.bilibili.com/video/BV1jy4y1m7wU