python - 对 matplotlib 中的不同子图使用相同的颜色条

标签 python matplotlib subplot colorbar

我正在使用以下过程在子图中绘制不同的图形。

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

这就是我得到的:

enter image description here

但是,我希望所有子图都具有相同的颜色条,例如在图的右侧。

最佳答案

看下面的例子:

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

enter image description here

关于python - 对 matplotlib 中的不同子图使用相同的颜色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40980921/

相关文章:

python - 运行 python 脚本时出错 - 语法无效

python - Numpy 子数组的外部添加

python - 如何创建一个输出 matplotlib 图形的函数?

python - 如何创建具有裁剪背景的渐变文本

matlab - 轴标签是不可见的?

python - 如何从项目管道访问scrapy设置

python - 将控制台中的所有内容重定向到日志文件,包括'input'和'os.system'函数

python - 如何绘制 3 个变量的隐函数

python - Python 子图中的作用域类似于 MATLAB 的 stackedplot()

layout - 密谋:仅在特定子图中堆积条形图