python 绘制中国地图并利用经纬度标注散点
python 繪制中國地圖并利用經緯度標注散點
所需要的包:GeoPandas,安裝教程有很多,自行百度即可。
用到的中國地級市shp文件:鏈接:https://pan.baidu.com/s/18aaxczrz4tIRMeCusOrDQA?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 提取碼:rav1?
一、GeoPandas類簡單介紹
? GeoPandas實現了兩個主要的數據結構,GeoSeries和GeoDataFrame。它們分別是pandas中Series和DataFrame的子類。實際上,如果你了解DataFrame結構,這個就很好理解了。
??一個GeoSeries就是包含一個幾何圖形的序列,比如多邊形、點。具體形式就在下圖,如果想要繪圖,很簡單,只要用plot方法就可以,多邊形GeoSeries繪制出來的就是多邊圖形,?點GeoSeries當然就是繪制點了。
多邊形GeoSeries:
?點GeoSeries:
繪圖實例:
?讀取shp文件繪制中國地圖
china_map=gp.GeoDataFrame.from_file("C:/Users/Administrator/Desktop/map_shp/shipnew/中國行政區劃2004_shp/dijishi_2004.shp",\encoding = 'gb18030') china_map.plot(figsize=(20,12),color="white", edgecolor="black")?
利用經緯度繪制散點圖:
lng = data2002com['lng'] lat= data2002com['lat'] pts = gp.GeoSeries([Point(x, y) for x, y in zip(lng, lat)])pts.plot(figsize=(12,8))?
二、世界地圖標注實例
? 這一部分主要是解釋官方網站的一個例子,為自己繪制地圖標注散點做準備。
import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib inline import os import geopandas as gp from shapely.geometry import Point# 官方數據 world = gp.read_file(gp.datasets.get_path('naturalearth_lowres')) cities = gp.read_file(gp.datasets.get_path('naturalearth_cities'))#注意world.shape =(177,6) # cities.shape = (202,2)fig, ax = plt.subplots()ax.set_aspect('equal')world.plot(ax=ax, color='white', edgecolor='black') cities.plot(ax=ax, marker='o', color='red', markersize=5) plt.show()?
? ?繪圖的機制其實很好理解,就是第一個圖層是一個世界地圖的多邊形,第二個圖層是一些城市的經緯度點,把這些經緯度點映射到世界地圖上就是這個樣了。
三、中國地圖標注實例
只需要地圖數據的shp文件,還有就是經緯度,就可以實現自己的地圖。
china_map=gp.GeoDataFrame.from_file("C:/Users/Administrator/Desktop/map_shp/shipnew/中國行政區劃2004_shp/dijishi_2004.shp",\encoding = 'gb18030') data2002com = pd.read_excel(r'C:\Users\Administrator\Desktop\經緯度數據\按公司名稱 \2002.xlsx')# 幾何圖形 geo_ploy = china_map['geometry'] # 地圖點 geo_point = gp.GeoSeries([Point(x, y) for x, y in zip(lng, lat)])fig, ax = plt.subplots(figsize=(12,8))ax.set_aspect('equal') # 幾何圖形繪制 geo_ploy.plot(ax=ax, color='white', edgecolor='black')# 地圖點標注 geo_point.plot(ax=ax, marker='o', color='black', markersize=0.1)總結
以上是生活随笔為你收集整理的python 绘制中国地图并利用经纬度标注散点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Turbo码基本框架
- 下一篇: websocket python爬虫_p