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

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 )

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 yunjia_community@tencent.com Delete .

Original publication time : 2020-11-26

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[DataCharm]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/02/20210222130146232q.html

  1. 使用Python开发DeFi项目
  2. python 函数详解
  3. Python工程师是做什么的?前景如何?
  4. Python - zip() 函数
  5. 30 周年生日,Python 先驱是怎么评价这门语言的?
  6. python将excel自适应导入数据库
  7. 从小白到大师,这里有一份Pandas入门指南
  8. [Python] 茎叶图和复合饼图的画法
  9. [Python interface automation] - regular use case parameterization
  10. Translation: practical Python Programming 02_ 02_ Containers
  11. Two years of Java, to write Python and go
  12. Translation: practical Python Programming 02_ 02_ Containers
  13. Two years of Java, to write Python and go
  14. Python-geoplot 空间核密度估计图绘制
  15. Python-seaborn 经济学人经典图表仿制
  16. python空间绘图- regionmask掩膜操作示例
  17. Python 空间绘图 - Cartopy 经纬度添加
  18. Python-pykrige包-克里金(Kriging)插值计算及可视化绘制
  19. Python 批量重采样、掩膜、坡度提取
  20. python - 多种交通方式可达圈分析
  21. Python 空间绘图 - 房价气泡图绘制
  22. Translation: practical Python Programming 02_ 02_ Containers
  23. Research on Portfolio Optimization Based on particle swarm optimization
  24. Ubuntu deploying Django project
  25. Two years of Java, write Python and go without byte beating
  26. Translation: practical Python Programming 02_ 02_ Containers
  27. So learn python, grandfather learned! Introduction to super simple Python
  28. python3 多线程 与 mongo亿级消费日志数据 新鲜demo 【优化第一版】
  29. Summary of Chinese word segmentation based on Jieba
  30. I've heard it n times, but I'm not impressed. After reading this, you'll understand
  31. Summary of Chinese word segmentation based on Jieba
  32. From movie art to Python code to realize God's reverse thinking mode
  33. Summary of Chinese word segmentation based on Jieba
  34. ARIMA模型预测CO2浓度时间序列-python实现
  35. Python belongs to back-end development or front-end development? Introduction to Python!
  36. python isinstance()
  37. I've heard it n times, but I'm not impressed. After reading this, you'll understand
  38. This article will familiarize you with the transformation process of Python - & gt; cafe - & gt; om model
  39. 如何用Python一键修改上万个文件名
  40. One day quick start to Python
  41. Python 学习笔记: List
  42. 翻译:《实用的Python编程》02_03_Formatting
  43. Is there any age requirement for learning Python? Is 30 OK?
  44. Professor Tsinghua! The most complete Python tutorial in 12 hours (free sharing at the end of the article)
  45. Using Python to develop defi project
  46. Detailed explanation of Python function
  47. Python 可变类型作为函数默认参数时的副作用
  48. What do Python engineers do? What's their future?
  49. 这是我见过最好的Python教程:十分钟带你认识Python
  50. Python欢喜冤家:爬虫与反爬虫带着处理方案来给大家拜年了
  51. Python - zip() function
  52. 写Python会遇到如下的错误:ModuleNotFoundError: No module named 'email.mime'; 'email' is not a package
  53. Python类的调用以及私有和公有属性方法的调用
  54. Python类的专有方法
  55. Python基础之:数字字符串和列表
  56. How did Python pioneers evaluate this language on their 30th birthday?
  57. Python基础之:数字字符串和列表
  58. Python基础之:数字字符串和列表
  59. 窥探未来不是梦,python数据分析轻松实现
  60. This article will familiarize you with the transformation process of Python - & gt; cafe - & gt; om model