# -*- 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()
結果為:

ImageGrid()函式引數說明:nrows_ncols=(1,3)表示創建一個1行3列的畫布,share_all=True表示所畫的影像公用x坐標軸和y坐標軸,cbar_location="right"表示colorbar位于影像的右側,當然也可以位于上方,下方和左側,cbar_mode="single"表示三個影像公用一個colorbar,cbar_size="7%"表示colorbar的尺寸,默認值為5%,cbar_pad=0.15表示影像與colorbar之間的填充間距,默認值為5%,可以自行調整以上數值進行嘗試,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/1521.html
標籤:其他
