两个子图的单个颜色条会更改其中一个子图的大小 [英] Single colorbar for two subplots changes the size of one of the subplots

查看:57
本文介绍了两个子图的单个颜色条会更改其中一个子图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为两个 matshow 添加单个 colorbar ,主要使用.最后一个最简单,但对我不起作用(imshow图的大小相同,但短于颜色条).您还可以在图像下运行颜色条:

将 numpy 导入为 np导入matplotlib.pyplot作为plt从 mpl_toolkits.axes_grid1 导入 make_axes_locatable数据= np.random.random((2,10,10))数据 *= np.array([1.5, 2.0])[:,None,None]无花果,轴= plt.subplots(nrows = 1,ncols = 2)对于dat,zip(数据,axes.flat)中的ax:im = ax.imshow(dat,vmin = 0,vmax = 2)fig.colorbar(im, ax=axes.ravel().tolist(),orientation='horizo​​ntal')plt.show()

I am trying to add a single colorbar for two matshows using mainly the code at here and here.

My code is the following now, but the problem is that the colorbar moderates the size of the plot on the right. How can I prevent that?

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

解决方案

There are a couple approaches in the answers to Matplotlib 2 Subplots, 1 Colorbar. The last is simplest but doesn't work for me (the imshow plots are the same size, but both shorter than the colorbar). You could also run the colorbar under the images:

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

这篇关于两个子图的单个颜色条会更改其中一个子图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆