本文為原創(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