1.pyproj
2.geos
3.matplotlib
4.basemap (base on geos)
https://www.lfd.uci.edu/~gohlke/pythonlibs/#basemap
pip install basemap-1.2.0-cp37-cp37m-win64.whl
5.下载 shapefile of china
https://gadm.org/download_country_v3.html
中国需下载 gadm36_CHN_shp、gadm36_HKG_shp、gadm36_TWN_shp
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
import pandas as pd
plt.figure(figsize=(16,8))
m = Basemap(llcrnrlon=80, llcrnrlat=14, urcrnrlon=138, urcrnrlat=51, projection='lcc', lat_1=33, lat_2=45, lon_0=100
,resolution='c'
,area_thresh=10000000
) # 兰勃特投影
# The resolution options are c (crude, the default), l (low), i (intermediate), h (high), f (full) or None
#Fill the globe with a blue color1
m.drawmapboundary(fill_color= 'aqua')
#Fill the continents with the land color
#m.fillcontinents(color='coral',lake_color='aqua')
m.fillcontinents(color='gray',lake_color="#7777ff")
m.drawcountries(linewidth=1,color='gray') # 隐藏国境线
#m.drawcoastlines()
m.readshapefile('gadm36_CHN_1', 'states', drawbounds=True)
m.readshapefile('gadm36_TWN_0', 'taiwan', drawbounds=True)
m.readshapefile('gadm36_HKG_0', 'hongkong', drawbounds=True)
df = pd.read_excel('各地销售数据.xlsx' , encoding='gbk')
#print(df)
df['地区'] = df.地区.str[:2]
df.set_index('地区', inplace=True)
statenames=[]
colors={}
cmap = plt.cm.YlOrRd
vmax = 10
vmin = 4
for shapedict in m.states_info:
statename = shapedict['NL_NAME_1']
p = statename.split('|')
if len(p) > 1:
s = p[1]
else:
s = p[0]
s = s[:2]
if s == '黑龍':
s = '黑龙'
statenames.append(s)
pop = df['rate'][s]
lons = df['lon'][s]
lats = df['lat'][s]
x, y = m(lons, lats)
#plt.text(x, y, pop, fontsize=12, fontweight='bold',
# ha='left', va='bottom', color='k')
plt.text(x, y,'%.1f' % pop, fontsize=10,
ha='center', va='center', color='brown')
if pop >=7 :
colors[s]= "#FF0000"
elif pop >=6 and pop <7 :
colors[s] = "#FF7F00"
elif pop >=5 and pop <6:
colors[s] = "#FF7256"
elif pop >=4 and pop <5:
colors[s] = "#B3EE3A"
elif pop >=3 and pop <4 :
colors[s] ="#9ACD32"
elif pop >=2 and pop <3:
colors[s]='#6495ED'
elif pop >=1 and pop <2:
colors[s] = "#0000CD"
elif pop >0 and pop <1:
colors[s] = "#4B0082" #'red'
else:
colors[s] = "red"
ax1 = plt.gca()
for nshape, seg in enumerate(m.states):
#color = rgb2hex(colors[statenames[nshape]])
color=colors[statenames[nshape]]
poly = Polygon(seg, facecolor=color, edgecolor=color)
ax1.add_patch(poly)
for nshape, seg in enumerate(m.taiwan):
poly = Polygon(seg, facecolor="#8e2424") #'brown'
ax1.add_patch(poly)
for nshape, seg in enumerate(m.hongkong):
poly = Polygon(seg, facecolor='brown')
ax1.add_patch(poly)
#----------------------------------------------------------------------------------
plt.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
plt.savefig('5月各个地区销售总额分布.png',ppi = '500')
plt.title('5月各个地区销售总额分布')
plt.show()