gpt4 book ai didi

python - 两个子图的单个颜色条更改其中一个子图的大小

转载 作者:太空狗 更新时间:2023-10-30 01:14:36 25 4
gpt4 key购买 nike

我正在尝试为两个 matshow 添加一个 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()

enter image description here

最佳答案

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()

enter image description here

关于python - 两个子图的单个颜色条更改其中一个子图的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29447352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com