zoukankan      html  css  js  c++  java
  • Python三维绘图--Matplotlib colorbar生成

    在这里插入图片描述
    Matplotlib 使用colorbar来设置色阶条:

    colorbar(**kwargs) 
    colorbar(mappable, **kwargs)
    colorbar(mappable, cax=cax, **kwargs)
    colorbar(mappable, ax=ax, **kwargs)   #需要制定颜色样式图,所要画色阶条的轴
    #ref:https://matplotlib.org/gallery/color/colorbar_basics.html#sphx-glr-gallery-color-colorbar-basics-py
    

    下面是一个例子:

    import numpy as np
    import matplotlib.pyplot as plt
    
    #生成绘图数据
    N = 100
    x, y = np.mgrid[:100, :100]
    Z = np.cos(x*0.05+np.random.rand()) + np.sin(y*0.05+np.random.rand())+2*np.random.rand()-1
    
    # mask out the negative and positive values, respectively
    Zpos = np.ma.masked_less(Z, 0)   #小于零的部分
    Zneg = np.ma.masked_greater(Z, 0)  #大于零的部分
    
    fig, (ax1, ax2, ax3) = plt.subplots(figsize=(13, 3), ncols=3)
    
    pos = ax1.imshow(Zpos, cmap='Reds', interpolation='none')
    fig.colorbar(pos, ax=ax1)  #这里使用colorbar来制定需要画颜色棒的图的轴,以及对应的cmap,与pos对应
    
    neg = ax2.imshow(Zneg, cmap='Blues_r', interpolation='none')
    fig.colorbar(neg, ax=ax2)
    
    pos_neg_clipped = ax3.imshow(Z, cmap='jet', vmin=-2, vmax=2,interpolation='none')  #-2,2的区间
    fig.colorbar(pos_neg_clipped, ax=ax3)
    plt.show()
    
    #ref:
    #    https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure.colorbar
    #    https://matplotlib.org/gallery/color/colorbar_basics.html#sphx-glr-gallery-color-colorbar-basics-py
    

    在这里插入图片描述

  • 相关阅读:
    Computer Vision 基础学习
    PHP遍历文件夹下的文件时遇到中文目录乱码问题
    Note -「模板」矩阵
    Note -「模板」高斯消元
    Solution -「CF113D」Museum
    【更新中】后缀数组学习笔记
    【题解】ABC225F
    【更新中】2021ZR模拟赛要题记录
    【游记】CSP-S-2021
    【题解】#2019 [zr联赛集训day3]史上第四简洁的题面
  • 原文地址:https://www.cnblogs.com/Tom-Ren/p/9897809.html
Copyright © 2011-2022 走看看