本文为原创文章
本文链接:https://www.cnblogs.com/zhanling/p/12606990.html
1 # -*- coding: utf-8 -*- 2 3 4 ‘‘‘ 5 Author: HermionX 6 7 source:https://github.com/HermioneX 8 ‘‘‘ 9 10 import numpy as np 11 import xarray as xr 12 import os 13 import datetime 14 from mpl_toolkits.basemap import Basemap 15 import matplotlib 16 matplotlib.use("Agg") 17 import matplotlib.pyplot as plt 18 from matplotlib.patches import Polygon 19 import matplotlib.patches as mpatches 20 import matplotlib.colors as colors 21 plt.rcParams[‘font.sans-serif‘]=[‘simhei‘] 22 plt.rcParams[‘axes.unicode_minus‘] = False 23 defaultencoding = ‘utf-8‘ 24 25 26 def plot1pic(src_fn,out_fn,start_str,cnt_str): 27 print(src_fn) 28 ds = xr.open_dataset(src_fn) 29 t = ds[‘value‘]*100 30 t.data[t.data<20] = 0 31 lons = ds.lon.data 32 lats = ds.lat.data 33 temp = xr.DataArray(t.data.T, coords=[lats,lons], dims=[‘latitude‘,‘longitude‘]) 34 35 fig, ax = plt.subplots(figsize = (18,16)) 36 37 m = Basemap(projection=‘cyl‘,resolution=‘l‘,llcrnrlon=lons.min(),llcrnrlat=lats.min(), 38 urcrnrlon=lons.max(),urcrnrlat=lats.max(),lon_0=120.,lat_0=90) 39 Lon,Lat = np.meshgrid(lons[:],lats[:]) 40 X,Y = m(Lon,Lat) 41 shp_info3 = m.readshapefile("./CHN_adm_shp/CHN_adm1",‘states‘,drawbounds=True,linewidth = 0.4,zorder=10) 42 43 short_state_names = {u"湖北":"Hubei", 44 u"北京":"Beijing", 45 u"上海":"Shanghai", 46 u"天津":"Tianjin", 47 u"重庆":"Chongqing", 48 u"香港":"Xianggang", 49 u"澳门":"Aomen", 50 u"安徽":"Anhui", 51 u"福建":"Fujian", 52 u"广东":"Guangdong", 53 u"广西":"Guangxi", 54 u"贵州":"Guizhou", 55 u"甘肃":"Gansu", 56 u"海南":"Hainan", 57 u"河北":"Hebei", 58 u"河南":"Henan", 59 u"黑龙江":"HeiLongjiang", 60 u"湖北":"Hubei", 61 u"湖南":"Hunan", 62 u"吉林":"Jilin", 63 u"江苏":"Jiangsu", 64 u"江西":"Jiangxi", 65 u"辽宁":"Liaoning", 66 u"内蒙古":"Neimenggu", 67 u"宁夏":"Ningxia", 68 u"青海":"Qinghai", 69 u"陕西":"Shanxi", 70 u"山西":"Shanxi", 71 u"山东":"Shandong", 72 u"四川":"Sichuan", 73 u"台湾":"Taiwan", 74 u"西藏":"Xizang", 75 u"新疆":"Xinjiang", 76 u"云南":"Yunnan", 77 u"浙江":"Zhejiang",} 78 79 printed_names = [] 80 81 for shapedict, state in zip(m.states_info, m.states): 82 if shapedict[‘NAME_1‘] not in short_state_names.values(): continue 83 short_name = short_state_names.keys()[short_state_names.values().index(shapedict[‘NAME_1‘])] 84 if short_name in printed_names: continue 85 x, y = np.median(np.array(state),axis=0) 86 print(x,y) 87 plt.text(x, y, short_name, ha="center") 88 printed_names += [short_name] 89 90 91 cm = plt.cm.get_cmap(‘rainbow‘) 92 print(np.min(t.data),np.max(t.data)) 93 cs=m.contourf(X,Y,t.data.T,cmap=cm) 94 95 l = 0.95 96 b = 0.23 97 w = 0.02 98 h = 1 - 2*b 99 100 plt.title( start_str +‘ ‘ +cnt_str,fontdict={‘weight‘:‘normal‘,‘size‘: 20},loc =‘left‘) 101 rect = [l,b,w,h] 102 cbar_ax = fig.add_axes(rect) 103 cbar = plt.colorbar(cs,orientation=‘vertical‘,cax =cbar_ax) 104 cbar.ax.tick_params(labelsize = 16) 105 font = {‘family‘ : ‘simhei‘, 106 ‘color‘ : ‘black‘, 107 ‘weight‘ : ‘normal‘, 108 ‘size‘ : 16, 109 } 110 cbar.set_label(u‘(%)‘ ,fontdict=font) #设置colorbar的标签字体及其大小 111 plt.savefig(out_fn,bbox_inches = ‘tight‘)
原文地址:https://www.cnblogs.com/zhanling/p/12606990.html
时间: 2024-10-31 12:57:23