Because of my love for spatial data visualization , Perhaps the official account is also more like this , Of course, it's also liked by little friends . stay R Language ggplot2 And its expansion package can be more simple to achieve all kinds of space visualization works , In the search for Python While drawing the space package , It is also found that geopandas、geoplot And so on , Today's tweet is easy to use geoplot Library to draw spatial kernel density estimation map , The knowledge involved is as follows ：
The data used are national PM2.5 Site data and China map files .
geoplot The library is equivalent to seaborn Yes matplotlib equally , Realized the encapsulation of many drawing functions , So that you can use less code to draw more beautiful charts , More details , You can go directly to the official website to check ：Geoplot On the official website, we only introduce the drawing functions needed in this tweet , First , Let's plot the spatial scatter , The data preview is as follows ：
Reuse geopandas The package deals with the related issues , The specific code is as follows ：
scattergdf = gpd.GeoDataFrame( scatter, geometry=gpd.points_from_xy(scatter. longitude , scatter. latitude ), crs="EPSG:4326")
In this way, the number can be processed into geoplot The data format needed for the drawing , The drawing code is as follows ：
plt.rcParams['font.family'] = ["Times New Roman"] fig,ax = plt.subplots(figsize=(8,5),dpi=200) proj = projection=gcrs.AlbersEqualArea(central_latitude=40, central_longitude=104) ax = plt.subplot(projection=proj,frameon=False) gplt.pointplot( scattergdf, projection=proj, s=4, hue="PM2.5", cmap='Spectral_r', edgecolor='black', linewidth=0.2, legend=True, ax=ax, ) gplt.polyplot(china_main, linewidth=.3,ax=ax) gplt.polyplot(china_nine, linewidth=.3,ax=ax) plt.title("Geoplot Test",fontsize = 20) plt.text(.88,.02,'\nVisualization by DataCharm',transform = ax.transAxes, ha='center', va='center',fontsize = 5,fontweight="bold")
The code is simple , We give the visualization directly , as follows ：
because geoplot The height of the package , We use it directly kdeplot() Function to draw , The specific code is as follows ：
fig,ax = plt.subplots(figsize=(8,5),dpi=200) proj = projection=gcrs.AlbersEqualArea(central_latitude=40, central_longitude=104) ax = plt.subplot(projection=proj) gplt.kdeplot( scattergdf, cmap = "Spectral_r", clip = china_main.geometry, projection=proj, levels=12, thresh=.0005, shade=True, ax=ax, ) gplt.polyplot(china_main, zorder=1,facecolor="none",linewidth=.3,ax=ax) plt.title("Geoplot Test2",fontsize = 18) plt.text(.85,.25,'\nVisualization by DataCharm',transform = ax.transAxes, ha='center', va='center',fontsize = 5,fontweight="bold")
The visualization of spatial kernel density estimation is completed , The drawing functions involved are relatively simple , Take a look at the official website tutorial, you can quickly master .
Python-geoplot The library can quickly draw the results for some spatial charts , It's relatively simple , But in practice , We also found some problems ( I'm fully aware of the process of drawing by myself , There may be personal reasons )：
This article is from WeChat official account. - DataCharm（shujumeili） , author ： Ning Haitao
The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the firstname.lastname@example.org Delete .
Original publication time ： 2020-11-26
Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .