Python colorbar和多个子批次的问题

Python colorbar和多个子批次的问题,python,matplotlib,colorbar,Python,Matplotlib,Colorbar,我试图用一个对应的色条将两幅图像相邻地绘制出来。 我的代码是 plt.figure(1) plt.subplot (121) plt.title('1') plt.imshow(matrix_lg, interpolation='bilinear', cmap=plt.cm.jet, vmin=np.log10(minVal), vmax = np.log10(maxVal)) plt.subplot(122) plt.title('2') plt.imshow(matrix_lg, inter

我试图用一个对应的色条将两幅图像相邻地绘制出来。 我的代码是

plt.figure(1)
plt.subplot (121)
plt.title('1')
plt.imshow(matrix_lg, interpolation='bilinear', cmap=plt.cm.jet, vmin=np.log10(minVal), vmax = np.log10(maxVal))
plt.subplot(122)
plt.title('2')
plt.imshow(matrix_lg, interpolation='bilinear', cmap=plt.cm.jet, vmin=np.log10(minVal), vmax = np.log10(maxVal))
plt.colorbar()
Python现在将颜色条附加到第二个子图上,并因此对其进行收缩。但我希望两个图的大小相同。 如何分离子批次的颜色栏


您可以为colorbar创建一个单独的
实例

import matplotlib.pyplot as plt
import numpy as np

plt.figure(1)
# Create room on the right
plt.gcf().subplots_adjust(right=0.8)

plt.subplot (121)
plt.title('1')
plt.imshow(np.random.rand(10,10), interpolation='bilinear', cmap=plt.cm.jet)
plt.subplot(122)
plt.title('2')
plt.imshow(np.random.rand(10,10), interpolation='bilinear', cmap=plt.cm.jet)

# Make a new Axes instance
cbar_ax = plt.gcf().add_axes([0.85, 0.15, 0.05, 0.7])
plt.colorbar(cax=cbar_ax)

plt.show()

编辑:

通过更改
add_axes
命令,可以将颜色条的高度更改为更像打印的高度。它以一个矩形作为参数
[左,下,宽,高]
,因此只需更改
底部
高度
,以满足您的需要