咔叽游戏

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 468|回复: 0

[python] matplotlib 多个图像共用一个colorbar的实现示例

[复制链接]
  • TA的每日心情
    无聊
    2019-5-27 08:20
  • 签到天数: 4 天

    [LV.2]圆转纯熟

    发表于 2020-10-4 14:31:57 | 显示全部楼层 |阅读模式
    本文主要介绍了matplotlib 多个图像共用一个colorbar的实现示例,分享给大家,具体如下:
    1. # -*- coding: utf-8 -*-
    2. """
    3. Created on Sat Sep 5 18:05:11 2020
    4. @author: 15025
    5. draw three figures with one common colorbar
    6. """
    7. import numpy as np
    8. import matplotlib.pyplot as plt
    9. from mpl_toolkits.axes_grid1 import ImageGrid
    10. class Visualazation:
    11.   def mainProgram(self):
    12.     # Set up figure and image grid
    13.     fig = plt.figure(figsize=(8, 4))
    14.     grid = ImageGrid(fig, 111,
    15.              nrows_ncols=(1,3),
    16.              axes_pad=0.15,
    17.              share_all=True,
    18.              cbar_location="right",
    19.              cbar_mode="single",
    20.              cbar_size="7%",
    21.              cbar_pad=0.15,
    22.              )
    23.     # Add data to image grid
    24.     for ax in grid:
    25.       im = ax.imshow(np.random.random((10,10)), vmin=0, vmax=1)
    26.     # Colorbar
    27.     ax.cax.colorbar(im)
    28.     ax.cax.toggle_label(True)
    29.     plt.show()
    30. if __name__ == "__main__":
    31.   main = Visualazation()
    32.   main.mainProgram()
    复制代码
    结果为:

    matplotlib 多个图像共用一个colorbar的实现示例-1.png

    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%。可以自行调整以上数值进行尝试。
    到此这篇关于matplotlib 多个图像共用一个colorbar的实现示例的文章就介绍到这了,更多相关matplotlib 共用colorbar内容请搜索咔叽论坛以前的文章或继续浏览下面的相关文章希望大家以后多多支持咔叽论坛!

    原文地址:https://www.jb51.net/article/195331.htm

    QQ|免责声明|小黑屋|手机版|Archiver|咔叽游戏

    GMT+8, 2024-3-29 09:33

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

    快速回复 返回顶部 返回列表