Fork me on github

Python-气象-大气科学-可视化绘图系列(三)—— 地图上自动标注省会名称(demo调整中)(代码+示例)

本文为原创文章

本文链接: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                           }
 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')

posted @ 2020-03-31 18:03  斩羚_HermioneX  阅读(1615)  评论(1编辑  收藏  举报