【发布时间】:2015-04-04 14:06:45
【问题描述】:
我正在尝试为两个matshows 添加一个colorbar,主要使用here 和here 处的代码。
我的代码现在如下,但问题是颜色条调节了右侧绘图的大小。我怎样才能防止这种情况发生?
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()
【问题讨论】:
-
仅供我自己参考。这可能会有所帮助:matplotlib.org/1.4.3/examples/axes_grid/demo_axes_grid2.html
-
这能回答你的问题吗? Matplotlib 2 Subplots, 1 Colorbar
标签: python matplotlib