## Drawing of Python geoplot spatial kernel density estimation map

DataCharm 2021-02-22 23:07:19
drawing python geoplot spatial kernel

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 ：

• geoplot library pointplot() Function to draw a space point graph
• geoplot library kdeplot() Function to draw spatial kernel density estimation graph

The data used are national PM2.5 Site data and China map files .

## pointplot() Draw a space point map

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 ：

## kdeplot() Plot the spatial kernel density estimation map

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,
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 .

## summary

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 )：

1. Due to the high encapsulation , Relative to some drawing elements ( Legend, etc ) Customization can be difficult .
2. There are few documents on the official website , I may not be very friendly to my friends who are just learning .
3. The installation is more troublesome ( Unless you can access the Internet scientifically , Otherwise, even if the installation is successful , There will also be small problems )