【问题标题】:Single colorbar for two subplots changes the size of one of the subplots两个子图的单个颜色条更改其中一个子图的大小
【发布时间】:2015-04-04 14:06:45
【问题描述】:

我正在尝试为两个matshows 添加一个colorbar,主要使用herehere 处的代码。

我的代码现在如下,但问题是颜色条调节了右侧绘图的大小。我怎样才能防止这种情况发生?

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

【问题讨论】:

标签: python matplotlib


【解决方案1】:

Matplotlib 2 Subplots, 1 Colorbar 的答案中有几种方法。最后一个是最简单的,但对我不起作用(imshow 图的大小相同,但 both 比颜色条短)。您还可以在图像下运行颜色栏:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 2021-08-14
    相关资源
    最近更新 更多