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

15 篇文章 0 订阅
14 篇文章 1 订阅

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

# -*- coding: utf-8 -*-
#**********************************************************
import os
import numpy as np
import wlab #pip install wlab
import matplotlib
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
from scipy.interpolate import griddata
matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'
#**********************************************************
FreqPLUS=['F06925','F10650','F23800','F18700','F36500','F89000']
#
FindPath='/d3/MWRT/R20130805/'
#**********************************************************
fig = plt.figure(figsize=(8,6), dpi=72, facecolor="white")
axes = plt.subplot(111)
axes.cla()#清空坐标轴内的所有内容
#指定图形的字体
font = {'family' : 'serif',
        'color'  : 'darkred',
        'weight' : 'normal',
        'size'   : 16,
        }
#**********************************************************
# 查找目录总文件名中保护F06925,EMS和txt字符的文件
for fp in FreqPLUS:
    FlagStr=[fp,'EMS','txt']
    FileList=wlab.GetFileList(FindPath,FlagStr)
    #
    LST=[]#地表温度
    EMS=[]#地表发射率
    TBH=[]#水平极化亮温
    TBV=[]#垂直极化亮温
    #
    findex=0
    for fn in FileList:
        findex=findex+1
        if (os.path.isfile(fn)):
            print(str(findex)+'-->'+fn)
            #fn='/d3/MWRT/R20130805/F06925_EMS60.txt'
            data=wlab.dlmread(fn)
            EMS=EMS+list(data[:,1])#地表发射率
            LST=LST+list(data[:,2])#温度
            TBH=TBH+list(data[:,8])#水平亮温
            TBV=TBV+list(data[:,9])#垂直亮温
    #-----------------------------------------------------------
    #生成格点数据,利用griddata插值
    grid_x, grid_y = np.mgrid[275:315:1, 0.60:0.95:0.01]
    grid_z = griddata((LST,EMS), TBH, (grid_x, grid_y), method='cubic')
    #将横纵坐标都映射到(0,1)的范围内
    extent=(0,1,0,1)
     #指定colormap
    cmap = matplotlib.cm.jet
    #设定每个图的colormap和colorbar所表示范围是一样的,即归一化
    norm = matplotlib.colors.Normalize(vmin=160, vmax=300)
    #显示图形,此处没有使用contourf #>>>ctf=plt.contourf(grid_x,grid_y,grid_z)
    gci=plt.imshow(grid_z.T, extent=extent, origin='lower',cmap=cmap, norm=norm)
    #配置一下坐标刻度等
    ax=plt.gca()
    ax.set_xticks(np.linspace(0,1,9))
    ax.set_xticklabels( ('275', '280', '285', '290', '295',  '300',  '305',  '310', '315'))
    ax.set_yticks(np.linspace(0,1,8))
    ax.set_yticklabels( ('0.60', '0.65', '0.70', '0.75', '0.80','0.85','0.90','0.95'))
    #显示colorbar
    cbar = plt.colorbar(gci)
    cbar.set_label('$T_B(K)$',fontdict=font)
    cbar.set_ticks(np.linspace(160,300,8))
    cbar.set_ticklabels( ('160', '180', '200', '220', '240',  '260',  '280',  '300'))
    #设置label
    ax.set_ylabel('Land Surface Emissivity',fontdict=font)
    ax.set_xlabel('Land Surface Temperature(K)',fontdict=font) #陆地地表温度LST
    #设置title
    titleStr='$T_B$ for Freq = '+str(float(fp[1:-1])*0.01)+'GHz'
    plt.title(titleStr)
    figname=fp+'.png'
    plt.savefig(figname)
    plt.clf()#清除图形

#plt.show()
print('ALL -> Finished OK')
上面的例子中,每个保存的图,都是用同样的colormap,并且每个图的颜色映射值都是一样的,也就是说第一个图中如果200表示蓝色,那么其他图中的200也表示蓝色。

示例的图形如下:


  • 15
    点赞
  • 129
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
### 回答1: 在Python中使用Matplotlib工具画图,需要先导入Matplotlib库。然后可以使用`plot`函数绘制图形,最后使用`show`函数显示图形。 下面是一个简单的例子: ``` import matplotlib.pyplot as plt # 绘制一个简单的折线图 x = [1, 2, 3, 4, 5] y = [2, 4, 1, 5, 3] plt.plot(x, y) # 设置坐标轴上的刻度字体加粗 plt.xticks(fontweight='bold') plt.yticks(fontweight='bold') plt.show() ``` 在这个例子中,我们首先导入Matplotlib库并给它起了一个别名`plt`,然后使用`plot`函数绘制了一个折线图,最后使用了`xticks`和`yticks`函数分别设置了x轴和y轴上的刻度字体加粗,最后使用`show`函数显示图形。 ### 回答2: 在Python中,使用matplotlib工具来画图非常简单。首先,要先安装matplotlib库,可以使用pip命令来安装: ```python pip install matplotlib ``` 接下来,导入matplotlib库,并调用相应的函数来绘制图形。以下是一个示例代码,用于绘制一个简单的散点图: ```python import matplotlib.pyplot as plt # 假设有一些数据 x = [1, 2, 3, 4, 5] y = [10, 20, 15, 25, 30] # 绘制散点图 plt.scatter(x, y) # 设置图形的坐标轴 plt.xlabel('X轴') plt.ylabel('Y轴') # 设置坐标刻度字体加粗 plt.xticks(fontweight='bold') plt.yticks(fontweight='bold') # 显示图形 plt.show() ``` 在这个示例中,我们使用`plt.scatter()`函数绘制了一个散点图。然后,使用`plt.xlabel()`和`plt.ylabel()`函数设置坐标轴的标签。最后,使用`plt.xticks()`和`plt.yticks()`函数设置坐标刻度字体加粗。 通过运行这段代码,我们可以得到一个散点图,并且坐标轴上的刻度字体会被加粗显示。 ### 回答3: 在Python中使用matplotlib库进行图像绘制非常方便。首先,我们需要安装并导入matplotlib库: ```python import matplotlib.pyplot as plt ``` 接下来,我们可以使用`plt.plot()`函数来画图。例如,我们可以绘制一个简单的折线图: ```python x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.plot(x, y) plt.show() ``` 上述代码中,`x`和`y`分别表示折线图的横轴和纵轴数据。调用`plt.plot()`函数将这些数据绘制成折线图,然后调用`plt.show()`函数显示图像。 要设置坐标上的刻度字体加粗,我们可以通过设置`plt.rcParams`属性来修改matplotlib的默认参数。下面的代码演示了如何将刻度字体加粗显示: ```python import matplotlib.pyplot as plt plt.rcParams['xtick.minor.size'] = 4 plt.rcParams['ytick.minor.size'] = 4 plt.rcParams['xtick.major.width'] = 2 plt.rcParams['ytick.major.width'] = 2 plt.rcParams['xtick.labelsize'] = 'large' plt.rcParams['ytick.labelsize'] = 'large' plt.rcParams['font.weight'] = 'bold' x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.plot(x, y) plt.show() ``` 在上述代码中,我们通过修改`plt.rcParams`的属性值来设置刻度线的长度、宽度以及刻度标签的字体大小和字体粗细。以上代码将坐标上的刻度字体设置为加粗显示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值