如何在一个matplotlib图像集中共享同一个colorbar而不重复创建?

2026-04-15 06:4613阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计486个文字,预计阅读时间需要2分钟。

如何在一个matplotlib图像集中共享同一个colorbar而不重复创建?

本例主要介绍了如何使用matplotlib绘制多个图像并共享一个colorbar。以下是一个简单的实现示例,适合分享给家人:

pythonimport matplotlib.pyplot as pltimport numpy as np

创建数据x=np.linspace(0, 10, 100)y1=np.sin(x)y2=np.cos(x)

如何在一个matplotlib图像集中共享同一个colorbar而不重复创建?

创建图像fig, axs=plt.subplots(2, 1, figsize=(8, 6))

绘制第一个图像axs[0].plot(x, y1, 'r-')axs[0].set_title('正弦波')axs[0].colorbar()

绘制第二个图像axs[1].plot(x, y2, 'b-')axs[1].set_title('余弦波')axs[1].colorbar()

调整布局plt.tight_layout()plt.show()

本文主要介绍了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()

结果为:

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内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计486个文字,预计阅读时间需要2分钟。

如何在一个matplotlib图像集中共享同一个colorbar而不重复创建?

本例主要介绍了如何使用matplotlib绘制多个图像并共享一个colorbar。以下是一个简单的实现示例,适合分享给家人:

pythonimport matplotlib.pyplot as pltimport numpy as np

创建数据x=np.linspace(0, 10, 100)y1=np.sin(x)y2=np.cos(x)

如何在一个matplotlib图像集中共享同一个colorbar而不重复创建?

创建图像fig, axs=plt.subplots(2, 1, figsize=(8, 6))

绘制第一个图像axs[0].plot(x, y1, 'r-')axs[0].set_title('正弦波')axs[0].colorbar()

绘制第二个图像axs[1].plot(x, y2, 'b-')axs[1].set_title('余弦波')axs[1].colorbar()

调整布局plt.tight_layout()plt.show()

本文主要介绍了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()

结果为:

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内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!