我试图主要使用here和here的代码为两个colorbar
添加一个matshow
。
我的代码如下,但是问题是颜色条会缓和右侧图的大小。我该如何预防呢?
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 2 Subplots, 1 Colorbar的答案中有两种方法。最后一个最简单,但对我不起作用(imshow图的大小相同,但都比色条短)。您还可以在图像下运行颜色栏:
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()
关于python - 两个子图的单个颜色条可更改其中一个子图的大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29447352/