Python-氣象-大氣科學(xué)-可視化繪圖系列(三)—— 地圖上自動(dòng)標(biāo)注省會(huì)名稱(demo調(diào)整中)(代碼+示例)

本文為原創(chuàng)文章:http://www.itdecent.cn/p/b1e8086b2e6b

# -*- coding: utf-8 -*-


'''
Author: HermionX

source:https://github.com/HermioneX
'''

import numpy as np
import xarray as xr
import os
import datetime
from mpl_toolkits.basemap import Basemap
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import matplotlib.patches as mpatches
import matplotlib.colors as colors
plt.rcParams['font.sans-serif']=['simhei']
plt.rcParams['axes.unicode_minus'] = False
defaultencoding = 'utf-8'


def plot1pic(src_fn,out_fn,start_str,cnt_str):
print(src_fn)
ds = xr.open_dataset(src_fn)
t = ds['value']*100
t.data[t.data<20] = 0
lons = ds.lon.data
lats = ds.lat.data
temp = xr.DataArray(t.data.T, coords=[lats,lons], dims=['latitude','longitude'])

fig, ax = plt.subplots(figsize = (18,16))

m = Basemap(projection='cyl',resolution='l',llcrnrlon=lons.min(),llcrnrlat=lats.min(),
urcrnrlon=lons.max(),urcrnrlat=lats.max(),lon_0=120.,lat_0=90)
Lon,Lat = np.meshgrid(lons[:],lats[:])
X,Y = m(Lon,Lat)
shp_info3 = m.readshapefile("./CHN_adm_shp/CHN_adm1",'states',drawbounds=True,linewidth = 0.4,zorder=10)

short_state_names = {u"湖北":"Hubei",
u"北京":"Beijing",
u"上海":"Shanghai",
u"天津":"Tianjin",
u"重慶":"Chongqing",
u"香港":"Xianggang",
}

printed_names = []

for shapedict, state in zip(m.states_info, m.states):
if shapedict['NAME_1'] not in short_state_names.values(): continue
short_name = short_state_names.keys()[short_state_names.values().index(shapedict['NAME_1'])]
if short_name in printed_names: continue
x, y = np.median(np.array(state),axis=0)
print(x,y)
plt.text(x, y, short_name, ha="center")
printed_names += [short_name]


cm = plt.cm.get_cmap('rainbow')
print(np.min(t.data),np.max(t.data))
cs=m.contourf(X,Y,t.data.T,cmap=cm)

l = 0.95
b = 0.23
w = 0.02
h = 1 - 2*b

plt.title( start_str +' ' +cnt_str,fontdict={'weight':'normal','size': 20},loc ='left')
rect = [l,b,w,h]
cbar_ax = fig.add_axes(rect)
cbar = plt.colorbar(cs,orientation='vertical',cax =cbar_ax)
cbar.ax.tick_params(labelsize = 16)
font = {'family' : 'simhei',
'color' : 'black',
'weight' : 'normal',
'size' : 16,
}
cbar.set_label(u'(%)' ,fontdict=font) #設(shè)置colorbar的標(biāo)簽字體及其大小
plt.savefig(out_fn,bbox_inches = 'tight')
---------------------
著作權(quán)歸作者所有。
商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
作者:斬羚_HermionX
image
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容