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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章