Python視覺化-縣市按經緯度座標在地圖示記數值

kewlgrl發表於2018-04-24

一、資料檔案準備

1、Info.csv

name,val,lat,lon
南京  ,4.23,32.04,118.78
徐州  ,4.13,34.26,117.2
睢寧  ,4.13,33.89,117.94
沛縣  ,4.1,34.73,116.93
豐縣  ,4.1,34.79,116.57
邳縣  ,4.1,34.3,117.97
銅山  ,4.1,34.26,117.2
新沂  ,4.1,34.38,118.33
淮安  ,4.2,33.5,119.15
楚州,4.2,33.5,119.13
淮陰  ,4.1,33.62,119.02
漣水  ,4.1,33.77,119.26
新興,4.05,33.46,120.09
步鳳,4.05,33.34,120.32
鹽城  ,4.1,33.38,120.13
阜寧  ,4.05,33.78,119.79
濱海  ,4.05,34.01,119.84
東臺  ,4.05,32.84,120.31
鹽都,4.05,33.33,120.15
建湖  ,4.05,33.46,119.77
射陽  ,4.1,33.77,120.26
大豐  ,4.1,33.19,120.45
宿遷  ,4.17,33.96,118.3
泗洪  ,4.17,33.46,118.23
沭陽  ,4.1,34.12,118.79
宿城,4.1,33.97,118.25
宿豫,4.1,33.95,118.32
泗洪  ,4.1,33.46,118.23
泰州  ,4.2,32.49,119.9
揚州  ,4.37,32.39,119.42
南通  ,4.15,32.01,120.86
如皋  ,4.15,32.39,120.56
海門  ,4.15,31.89,121.15
啟東  ,4.15,31.8,121.66
海安  ,4.2,32.57,120.45
海安  ,4.2,32.57,120.45
通州,4.23,32.08,121.07
連雲港  ,4.1,34.59,119.16
灌雲  ,4.1,34.3,119.23
東海  ,4.1,34.54,118.75
贛榆  ,4.1,34.83,119.11
灌南  ,4.1,34.09,119.36
鎮江  ,4.4,32.2,119.44
無錫  ,4.29,31.59,120.29
蘇州  ,4.29,31.32,120.62
常州  ,4.29,31.79,119.95
崑山  ,4.29,31.39,120.95
第一列是城市名稱,第二列是數值,第三四列是城市對應的真實緯度和經度。

2、CHN_adm/CHN_adm3

需要下載CHN_adm這個壓縮檔案,使用其畫出地圖,可以自行下載或者:

在這下載:https://download.csdn.net/download/mikasa3/10371748

二、匯入模組包

可參考Windows下安裝Python、matplotlib包 及相關
https://blog.csdn.net/mikasa3/article/details/78942650 

1、numpy

2、pandas

3、matplotlib

4、Basemap

三、完整程式碼

如下:

# coding=utf-8
import csv
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
def DrawPointMap(file_name):
    fig = plt.figure()
    ax1 = fig.add_axes([0.1,0.1,0.8,0.8])#[left,bottom,width,height]
    map = Basemap(projection='mill',lat_0=36,lon_0=122,\
                 llcrnrlat=30.5 ,urcrnrlat=35.3,llcrnrlon=116.2,urcrnrlon=121.99,\
			     ax=ax1,rsphere=6371200.,resolution='h',area_thresh=1000000)
    shp_info = map.readshapefile('CHN_adm/CHN_adm3','states',drawbounds=False)
    for info, shp in zip(map.states_info, map.states):
        proid = info['NAME_1']
        if proid == 'Jiangsu':
            poly = Polygon(shp,facecolor='w',edgecolor='k', lw=1.0, alpha=0.1)#注意設定透明度alpha,否則點會被地圖覆蓋
            ax1.add_patch(poly)		
    parallels = np.arange(30.6,35.3,2) 
    map.drawparallels(parallels,labels=[1,0,0,0],fontsize=10) #parallels
    meridians = np.arange(116.3,122,2)
    map.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10) #meridians
    posi = pd.read_csv(file_name)
    lat = np.array(posi["lat"][0:48])#獲取經緯度座標,一共有48個資料                        
    lon = np.array(posi["lon"][0:48])                        
    val = np.array(posi["val"][0:48],dtype=float)#獲取數值
    size = (val-np.min(val)+0.05)*800#對點的數值作離散化,使得大小的顯示明顯
    x,y = map(lon,lat)
    map.scatter(x, y, s=size, color = 'r') #要標記的點的座標、大小及顏色
    for i in range(0,47):
       plt.text(x[i]+5000,y[i]+5000,str(val[i]))
       #plt.text(lat[i],lon[i],str(val[i]), family='serif', style='italic', ha='right', wrap=True)
    #plt.annotate(s=3.33,xy=(x,y),xytext=None, xycoords='data',textcoords='offset points', arrowprops=None,fontsize=16)
    map.drawmapboundary()  #邊界線
    #map.fillcontinents()  
    map.drawstates()        
    #map.drawcoastlines()  #海岸線 
    map.drawcountries()     
    map.drawcounties()     
    plt.title('Jiangsu in CHINA')#標題
    plt.savefig('Jiangsu.png', dpi=100, bbox_inches='tight')#檔案命名為Jiangsu.png儲存
    plt.show()
if __name__=='__main__':
    DrawPointMap("Info.csv")

四、執行結果

如圖:


相關文章