如何在 matplotlib python 中使用颜色条更正子图图像大小?

发布于 2025-01-10 08:23:25 字数 1728 浏览 0 评论 0原文

我想用 python 制作一个 3x2 子图图像。对于第三行的图像,我添加了一个颜色条。但与顶行相比,图像尺寸变小。无论如何,是否可以将图像大小固定为与前两行相同,同时在第三行中有颜色条?

这是我的 python 代码,

#Imports
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib.image as image
import matplotlib.colors
from matplotlib.colors import ListedColormap
#data

bird = image.imread('Desktop/bird.jpeg')   

fig, (ax1, ax2, ax3) = plt.subplots(3,2,figsize=(5,5)) 
ax1[0].imshow(bird)
ax1[0].set_ylabel('Row 1', size=8)
ax1[0].set_yticks([]) #display no ticks 
ax1[0].set_xticks([])   

ax1[1].imshow(bird)
ax1[1].set_yticks([]) 
ax1[1].set_xticks([])   

ax2[0].imshow(bird)
ax2[0].set_yticks([]) 
ax2[0].set_xticks([])
ax2[0].set_ylabel('Row 2', size=8)

ax2[1].imshow(bird)
ax2[1].set_yticks([]) 
ax2[1].set_xticks([])  

#Generating Color Map
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["Red","Green","Blue"])

# Right Image
bird_3 = ax3[1].imshow(bird, cmap = cmap)
ax3[1].set_yticks([]) 
ax3[1].set_xticks([])
cbar_int = fig.colorbar(bird_3,orientation='horizontal', ax=ax3[1]) 
cbar_int.set_label('CBar', size=8, rotation=0)
cbar_int.ax.tick_params(labelsize=8) 
bird_3.set_clim(vmin=-1, vmax=1) 

# Left Image
bird_4 = ax3[0].imshow(bird, cmap = cmap)
ax3[0].set_yticks([]) 
ax3[0].set_xticks([])
ax3[0].set_ylabel('Row 3', size=8)
cbar_int = fig.colorbar(bird_4,orientation='horizontal', ax=ax3[0]) 
cbar_int.set_label('CBar', size=8, rotation=0)
cbar_int.ax.tick_params(labelsize=8) 
bird_3.set_clim(vmin=-1, vmax=1)    

plt.show()

我用它得到了以下结果。您会看到,与第 1 行和第 2 行相比,第 3 行图像较小。

在此处输入图像描述

I want to make a 3x2 subplot image in python. With the images in third row I have added a colorbar. But it the image size gets small as compared to the top rows. Is there anyway to fix the image size the same as of top two rows while having a colorbar in the third row?

Here's my python code

#Imports
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib.image as image
import matplotlib.colors
from matplotlib.colors import ListedColormap
#data

bird = image.imread('Desktop/bird.jpeg')   

fig, (ax1, ax2, ax3) = plt.subplots(3,2,figsize=(5,5)) 
ax1[0].imshow(bird)
ax1[0].set_ylabel('Row 1', size=8)
ax1[0].set_yticks([]) #display no ticks 
ax1[0].set_xticks([])   

ax1[1].imshow(bird)
ax1[1].set_yticks([]) 
ax1[1].set_xticks([])   

ax2[0].imshow(bird)
ax2[0].set_yticks([]) 
ax2[0].set_xticks([])
ax2[0].set_ylabel('Row 2', size=8)

ax2[1].imshow(bird)
ax2[1].set_yticks([]) 
ax2[1].set_xticks([])  

#Generating Color Map
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["Red","Green","Blue"])

# Right Image
bird_3 = ax3[1].imshow(bird, cmap = cmap)
ax3[1].set_yticks([]) 
ax3[1].set_xticks([])
cbar_int = fig.colorbar(bird_3,orientation='horizontal', ax=ax3[1]) 
cbar_int.set_label('CBar', size=8, rotation=0)
cbar_int.ax.tick_params(labelsize=8) 
bird_3.set_clim(vmin=-1, vmax=1) 

# Left Image
bird_4 = ax3[0].imshow(bird, cmap = cmap)
ax3[0].set_yticks([]) 
ax3[0].set_xticks([])
ax3[0].set_ylabel('Row 3', size=8)
cbar_int = fig.colorbar(bird_4,orientation='horizontal', ax=ax3[0]) 
cbar_int.set_label('CBar', size=8, rotation=0)
cbar_int.ax.tick_params(labelsize=8) 
bird_3.set_clim(vmin=-1, vmax=1)    

plt.show()

The following results I get with it. You see row 3 images are small compared to row 1 and 2.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无言温柔 2025-01-17 08:23:25

Matplotlib 从主机轴中窃取空间。但是,您可以指定多个轴来窃取空间。所以上面你可以很容易地做到:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

fig, axs = plt.subplots(3, 2)
for ax in axs.flat:
    pc = ax.imshow(np.random.randn(20,40))

fig.colorbar(pc, ax=axs[:, 1], orientation='horizontal')
fig.colorbar(pc, ax=axs[:, 0], orientation='horizontal')
plt.show()

并且从每列的所有三个轴中窃取空间。

您还可以指定 constrained_layout=True 以获得更好的布局。

请注意,使用 imshow 时,轴具有固定的纵横比,因此总会出现空白问题。

Matplotlib steals space from the host axes. However, you can specify more than one axes to steal space from. So above you can easily do:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

fig, axs = plt.subplots(3, 2)
for ax in axs.flat:
    pc = ax.imshow(np.random.randn(20,40))

fig.colorbar(pc, ax=axs[:, 1], orientation='horizontal')
fig.colorbar(pc, ax=axs[:, 0], orientation='horizontal')
plt.show()

and space is stolen from all three axes in each column.

You can also specify constrained_layout=True for slightly better layout.

Note that with imshow the axes have a fixed aspect ratio, so there is always going to be issues with white space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文