对matplotlib中的不同子图使用相同的颜色栏 [英] Use the same colorbar for different subplots in matplotlib

查看:98
本文介绍了对matplotlib中的不同子图使用相同的颜色栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照以下步骤在子图中绘制不同的图形.

I am plotting different figure in subplots using the following procedure.

fig = figure(figsize=(10,11))
subplots_adjust(wspace=0.5,hspace=0.2)
iplot = 330
for i in range(9):
    iplot += 1
    ax = fig.add_subplot(iplot)
    ## Comparison Mreal - Real
    tmp = REAL[REAL.days==days[i]]
    tmp = tmp.score
    tmp = np.array(tmp)
    tmp = tmp.reshape(len(xv), len(br))
    im = plt.imshow(tmp, interpolation='nearest', cmap='gnuplot', vmin = 0, vmax = 1,  extent=[0.05,0.5,1,0.05],
              aspect=0.5)
    xtmp = [0.05, 0.2, 0.3, 0.4, 0.5]
    plt.xticks(xtmp)
    ytmp = [0.05, 0.2, 0.4, 0.6, 0.8, 1.0]
    plt.yticks(ytmp)
    ax.grid(False)
divider = make_axes_locatable(plt.gca())
cax = divider.append_axes("right", "5%", pad="3%")
plt.colorbar(im, cax=cax)
plt.tight_layout()

这就是我得到的:

不过,我希望所有子图具有相同的颜色条,以图右侧为例.

However I would like to have the same colorbar for all the subplots, for istance on the right side of the figure.

推荐答案

看下面的例子:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=3, ncols=3)
for ax in axes.flat:
    im = ax.imshow(np.random.random((6,6)), interpolation='nearest', cmap='gnuplot',
     vmin=0, vmax=1, extent=[0.05,0.5,1,0.05],aspect=0.5)

fig.subplots_adjust(right=0.8)
# put colorbar at desire position
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(im, cax=cbar_ax)

plt.show()

这篇关于对matplotlib中的不同子图使用相同的颜色栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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