python 绘制中国地图并利用经纬度标注散点
python 繪制中國地圖并利用經(jīng)緯度標(biāo)注散點(diǎn)
所需要的包:GeoPandas,安裝教程有很多,自行百度即可。
用到的中國地級(jí)市shp文件:鏈接:https://pan.baidu.com/s/18aaxczrz4tIRMeCusOrDQA?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 提取碼:rav1?
一、GeoPandas類簡單介紹
? GeoPandas實(shí)現(xiàn)了兩個(gè)主要的數(shù)據(jù)結(jié)構(gòu),GeoSeries和GeoDataFrame。它們分別是pandas中Series和DataFrame的子類。實(shí)際上,如果你了解DataFrame結(jié)構(gòu),這個(gè)就很好理解了。
??一個(gè)GeoSeries就是包含一個(gè)幾何圖形的序列,比如多邊形、點(diǎn)。具體形式就在下圖,如果想要繪圖,很簡單,只要用plot方法就可以,多邊形GeoSeries繪制出來的就是多邊圖形,?點(diǎn)GeoSeries當(dāng)然就是繪制點(diǎn)了。
多邊形GeoSeries:
?點(diǎn)GeoSeries:
繪圖實(shí)例:
?讀取shp文件繪制中國地圖
china_map=gp.GeoDataFrame.from_file("C:/Users/Administrator/Desktop/map_shp/shipnew/中國行政區(qū)劃2004_shp/dijishi_2004.shp",\encoding = 'gb18030') china_map.plot(figsize=(20,12),color="white", edgecolor="black")?
利用經(jīng)緯度繪制散點(diǎn)圖:
lng = data2002com['lng'] lat= data2002com['lat'] pts = gp.GeoSeries([Point(x, y) for x, y in zip(lng, lat)])pts.plot(figsize=(12,8))?
二、世界地圖標(biāo)注實(shí)例
? 這一部分主要是解釋官方網(wǎng)站的一個(gè)例子,為自己繪制地圖標(biāo)注散點(diǎn)做準(zhǔn)備。
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# 官方數(shù)據(jù) 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()?
? ?繪圖的機(jī)制其實(shí)很好理解,就是第一個(gè)圖層是一個(gè)世界地圖的多邊形,第二個(gè)圖層是一些城市的經(jīng)緯度點(diǎn),把這些經(jīng)緯度點(diǎn)映射到世界地圖上就是這個(gè)樣了。
三、中國地圖標(biāo)注實(shí)例
只需要地圖數(shù)據(jù)的shp文件,還有就是經(jīng)緯度,就可以實(shí)現(xiàn)自己的地圖。
china_map=gp.GeoDataFrame.from_file("C:/Users/Administrator/Desktop/map_shp/shipnew/中國行政區(qū)劃2004_shp/dijishi_2004.shp",\encoding = 'gb18030') data2002com = pd.read_excel(r'C:\Users\Administrator\Desktop\經(jīng)緯度數(shù)據(jù)\按公司名稱 \2002.xlsx')# 幾何圖形 geo_ploy = china_map['geometry'] # 地圖點(diǎn) 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')# 地圖點(diǎn)標(biāo)注 geo_point.plot(ax=ax, marker='o', color='black', markersize=0.1)總結(jié)
以上是生活随笔為你收集整理的python 绘制中国地图并利用经纬度标注散点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Turbo码基本框架
- 下一篇: python贝叶斯网络预测天气_基于py