Python可视化-县市按经纬度坐标在地图标记数值

一、数据文件准备

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")

四、运行结果

如图:

Python可视化-县市按经纬度坐标在地图标记数值_第1张图片

你可能感兴趣的:(Python,python数据可视化实例入门)