使用matplotlib的示例:调整字体-设置刻度、坐标、colormap和colorbar等

 分类:
 
  

使用matplotlib的示例:调整字体-设置刻度、坐标、colormap和colorbar等

 

[python] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. # -*- coding: utf-8 -*-  
  2. #**********************************************************  
  3. import os  
  4. import numpy as np  
  5. import wlab #pip install wlab  
  6. import matplotlib  
  7. import matplotlib.cm as cm  
  8. import matplotlib.pyplot as plt  
  9. from matplotlib.ticker import MultipleLocator  
  10. from scipy.interpolate import griddata  
  11. matplotlib.rcParams['xtick.direction'] = 'out'  
  12. matplotlib.rcParams['ytick.direction'] = 'out'  
  13. #**********************************************************  
  14. FreqPLUS=['F06925','F10650','F23800','F18700','F36500','F89000']  
  15. #  
  16. FindPath='/d3/MWRT/R20130805/'  
  17. #**********************************************************  
  18. fig = plt.figure(figsize=(8,6), dpi=72, facecolor="white")  
  19. axes = plt.subplot(111)  
  20. axes.cla()#清空坐标轴内的所有内容  
  21. #指定图形的字体  
  22. font = {'family' : 'serif',  
  23.         'color'  : 'darkred',  
  24.         'weight' : 'normal',  
  25.         'size'   : 16,  
  26.         }  
  27. #**********************************************************  
  28. # 查找目录总文件名中保护F06925,EMS和txt字符的文件  
  29. for fp in FreqPLUS:  
  30.     FlagStr=[fp,'EMS','txt']  
  31.     FileList=wlab.GetFileList(FindPath,FlagStr)  
  32.     #  
  33.     LST=[]#地表温度  
  34.     EMS=[]#地表发射率  
  35.     TBH=[]#水平极化亮温  
  36.     TBV=[]#垂直极化亮温  
  37.     #  
  38.     findex=0  
  39.     for fn in FileList:  
  40.         findex=findex+1  
  41.         if (os.path.isfile(fn)):  
  42.             print(str(findex)+'-->'+fn)  
  43.             #fn='/d3/MWRT/R20130805/F06925_EMS60.txt'  
  44.             data=wlab.dlmread(fn)  
  45.             EMS=EMS+list(data[:,1])#地表发射率  
  46.             LST=LST+list(data[:,2])#温度  
  47.             TBH=TBH+list(data[:,8])#水平亮温  
  48.             TBV=TBV+list(data[:,9])#垂直亮温  
  49.     #-----------------------------------------------------------  
  50.     #生成格点数据,利用griddata插值  
  51.     grid_x, grid_y = np.mgrid[275:315:1, 0.60:0.95:0.01]  
  52.     grid_z = griddata((LST,EMS), TBH, (grid_x, grid_y), method='cubic')  
  53.     #将横纵坐标都映射到(0,1)的范围内  
  54.     extent=(0,1,0,1)  
  55.      #指定colormap  
  56.     cmap = matplotlib.cm.jet  
  57.     #设定每个图的colormap和colorbar所表示范围是一样的,即归一化  
  58.     norm = matplotlib.colors.Normalize(vmin=160, vmax=300)  
  59.     #显示图形,此处没有使用contourf #>>>ctf=plt.contourf(grid_x,grid_y,grid_z)  
  60.     gci=plt.imshow(grid_z.T, extent=extent, origin='lower',cmap=cmap, norm=norm)  
  61.     #配置一下坐标刻度等  
  62.     ax=plt.gca()  
  63.     ax.set_xticks(np.linspace(0,1,9))  
  64.     ax.set_xticklabels( ('275', '280', '285', '290', '295',  '300',  '305',  '310', '315'))  
  65.     ax.set_yticks(np.linspace(0,1,8))  
  66.     ax.set_yticklabels( ('0.60', '0.65', '0.70', '0.75', '0.80','0.85','0.90','0.95'))  
  67.     #显示colorbar  
  68.     cbar = plt.colorbar(gci)  
  69.     cbar.set_label('$T_B(K)$',fontdict=font)  
  70.     cbar.set_ticks(np.linspace(160,300,8))  
  71.     cbar.set_ticklabels( ('160', '180', '200', '220', '240',  '260',  '280',  '300'))  
  72.     #设置label  
  73.     ax.set_ylabel('Land Surface Emissivity',fontdict=font)  
  74.     ax.set_xlabel('Land Surface Temperature(K)',fontdict=font) #陆地地表温度LST  
  75.     #设置title  
  76.     titleStr='$T_B$ for Freq = '+str(float(fp[1:-1])*0.01)+'GHz'  
  77.     plt.title(titleStr)  
  78.     figname=fp+'.png'  
  79.     plt.savefig(figname)  
  80.     plt.clf()#清除图形  
  81.   
  82. #plt.show()  
  83. print('ALL -> Finished OK')  
上面的例子中,每个保存的图,都是用同样的colormap,并且每个图的颜色映射值都是一样的,也就是说第一个图中如果200表示蓝色,那么其他图中的200也表示蓝色。

 

示例的图形如下:

 
2
2
posted on 2016-06-07 16:52  qqhfeng16  阅读(29232)  评论(0编辑  收藏  举报