colorbar更改python中子图的大小 [英] colorbar changes the size of subplot in python
本文介绍了colorbar更改python中子图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码生成并排尺寸的图像,我只需要将颜色条添加到该行中的第二个图像.我使用以下代码
将matplotlib.pyplot导入为plt从mpl_toolkits.axes_grid1导入make_axes_locatable导入matplotlib.gridspec作为gridspecdef plot(x):gs1 = gridspec.GridSpec(1、2)gs1.update(wspace = 0.005,hspace = 0.005)plt.subplot(gs1 [0])plt.imshow(x)plt.axis('off')plt.title('dog')ax1 = plt.subplot(gs1 [1])imc = plt.imshow(x,cmap ='hot',插值='nearest')plt.axis('off')plt.title('dog')除法器= make_axes_locatable(ax1)cax = Divider.append_axes("right",size ="5%",pad = 0.05)plt.colorbar(imc,cax = cax)plt.tight_layout()plt.show()
但是,并排图像的大小不相等.我想知道如何解决此问题?
解决方案
您可以使用
I use the following code to generate side-by-size images and I need to add colorbar only to the second image in the row. I use the following code for it
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.gridspec as gridspec
def plotting(x):
gs1 = gridspec.GridSpec(1, 2)
gs1.update(wspace=0.005, hspace=0.005)
plt.subplot(gs1[0])
plt.imshow(x)
plt.axis('off')
plt.title('dog')
ax1 = plt.subplot(gs1[1])
imc = plt.imshow(x, cmap='hot', interpolation='nearest')
plt.axis('off')
plt.title('dog')
divider = make_axes_locatable(ax1)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(imc, cax=cax)
plt.tight_layout()
plt.show()
However it comes out the size of side-by-side images are not equal. I wonder how I could fix this issue?
解决方案
You can use ImageGrid, which was created exactly for this purpose:
from mpl_toolkits.axes_grid1 import ImageGrid
x = np.random.random(size=(10,10))
fig = plt.figure()
grid = ImageGrid(fig, 111,
nrows_ncols = (1,2),
axes_pad = 0.05,
cbar_location = "right",
cbar_mode="single",
cbar_size="5%",
cbar_pad=0.05
)
grid[0].imshow(x)
grid[0].axis('off')
grid[0].set_title('dog')
imc = grid[1].imshow(x, cmap='hot', interpolation='nearest')
grid[1].axis('off')
grid[1].set_title('dog')
plt.colorbar(imc, cax=grid.cbar_axes[0])
这篇关于colorbar更改python中子图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文