我正在尝试使用here和here处的代码为两个colorbar
添加一个matshow
。
我的代码现在如下,但问题是colorbar调节右侧图表的大小。我该如何防止这种情况?
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# Generate some data that where each slice has a different range
# (The overall range is from 0 to 2)
data = np.random.random((2,10,10))
data *= np.array([1.5, 2.0])[:,None,None]
# Plot each slice as an independent subplot
fig, axes = plt.subplots(nrows=1, ncols=2)
for dat, ax in zip(data, axes.flat):
# The vmin and vmax arguments specify the color limits
im = ax.imshow(dat, vmin=0, vmax=2)
# Make an axis for the colorbar on the right side
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
fig.colorbar(im, cax=cax)
plt.tight_layout()
plt.show()
答案 0 :(得分:4)
Matplotlib 2 Subplots, 1 Colorbar的答案中有几种方法。最后一个是最简单但对我不起作用(imshow图是相同的大小,但两个比colorbar短)。您还可以在图像下运行颜色栏:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
data = np.random.random((2,10,10))
data *= np.array([1.5, 2.0])[:,None,None]
fig, axes = plt.subplots(nrows=1, ncols=2)
for dat, ax in zip(data, axes.flat):
im = ax.imshow(dat, vmin=0, vmax=2)
fig.colorbar(im, ax=axes.ravel().tolist(), orientation='horizontal')
plt.show()