1. 程式人生 > 程式設計 >matplotlib 多個影象共用一個colorbar的實現示例

matplotlib 多個影象共用一個colorbar的實現示例

本文主要介紹了matplotlib 多個影象共用一個colorbar的實現示例,分享給大家,具體如下:

# -*- coding: utf-8 -*-
"""
Created on Sat Sep 5 18:05:11 2020
@author: 15025
draw three figures with one common colorbar
"""

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid


class Visualazation:
  def mainProgram(self):
    # Set up figure and image grid
    fig = plt.figure(figsize=(8,4))
    
    grid = ImageGrid(fig,111,nrows_ncols=(1,3),axes_pad=0.15,share_all=True,cbar_location="right",cbar_mode="single",cbar_size="7%",cbar_pad=0.15,)
    
    # Add data to image grid
    for ax in grid:
      im = ax.imshow(np.random.random((10,10)),vmin=0,vmax=1)
    
    # Colorbar
    ax.cax.colorbar(im)
    ax.cax.toggle_label(True)
    
    plt.show()
    

if __name__ == "__main__":
  main = Visualazation()
  main.mainProgram()

結果為:

matplotlib 多個影象共用一個colorbar的實現示例

ImageGrid()函式引數說明:nrows_ncols=(1,3)表示建立一個13列的畫布。share_all=True表示所畫的影象公用x座標軸和y座標軸。cbar_location="right"表示colorbar位於影象的右側,當然也可以位於上方,下方和左側。cbar_mode="single"表示三個影象公用一個colorbarcbar_size="7%"表示colorbar的尺寸,預設值為5%cbar_pad=0.15表示影象與colorbar之間的填充間距,預設值為5%。可以自行調整以上數值進行嘗試。

到此這篇關於matplotlib 多個影象共用一個colorbar的實現示例的文章就介紹到這了,更多相關matplotlib 共用colorbar內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!