【问题标题】:Placing a single color bar for two sub-plots in Matplotlib and gridscpec在 Matplotlib 和 gridspec 中为两个子图放置一个颜色条
【发布时间】:2021-10-16 12:18:10
【问题描述】:

我正在尝试使用 gridspec 以网格的形式绘制 6 个图。我想在底部的第 2 列和第 3 列之间放置一个颜色条。

我的代码如下,但它生成了 6 个颜色条。如何更改此代码,以便在两列之间的底部放置一个颜色条?

import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes


plt.figure(figsize=(8, 6))
gs = gridspec.GridSpec(2, 3)
gs.update(wspace= 1)

cmaps = ['RdBu_r', 'viridis', 'viridis']

for i in range(2):
  for j in range(3):
      ax = plt.subplot(gs[i, j])
      image = ax.pcolormesh(np.random.random((20, 20)) * (j + 1),
                            cmap=cmaps[j])
      #image = ax.imshow(im)
      axins = inset_axes(ax, 
               width="10%",  
               height="100%",  
               loc='lower left',
               bbox_to_anchor=(1.05, 0.0, 1, 1),
               bbox_transform=ax.transAxes,
               borderpad=0
               )
               
      cb = plt.colorbar(image, cax=axins)

编辑:如果我不清楚,我很抱歉。我希望将颜色条水平放置在底部,跨越第 2 列和第 3 列(我用红色标记了我想要放置的位置)

【问题讨论】:

    标签: python matplotlib plot subplot colorbar


    【解决方案1】:

    Matplotlib 足够聪明,可以让您选择使用 ax 参数一次为多轴创建颜色条:

    import numpy as np
    import matplotlib.gridspec as gridspec
    import matplotlib.pyplot as plt
    
    plt.figure(figsize=(8, 6))
    gs = gridspec.GridSpec(2, 3)
    gs.update(wspace= 1)
    
    cmaps = ['RdBu_r', 'viridis', 'viridis']
    
    axs = [[],[]]
    for i in range(2):
      for j in range(3):
          ax = plt.subplot(gs[i, j])
          axs[i].append(ax)
          image = ax.pcolormesh(
              np.random.random((20, 20)) * (j + 1),
              cmap=cmaps[j])
    
    # converting to numpy array for easier slicing
    axs = np.array(axs)
    
    cb = plt.colorbar(
        image,
        ax=axs[:,1:], # select all axis from second column
        orientation='horizontal')
    

    颜色条将占据轴的一部分,并且第一列不会在底部对齐。

    如果这让您感到困扰,请手动定义颜色条轴,并在您的 gridspec 中添加一行。要控制颜色条的高度,请修改 grispec 高度比率(height_ratios=[1,1,.1]gridspec's documentation 中有更多内容)。我的尝试:

    import numpy as np
    import matplotlib.gridspec as gridspec
    import matplotlib.pyplot as plt
    
    plt.figure(figsize=(8, 6))
    
    gs = gridspec.GridSpec(
        3, 3,
        height_ratios=[1,1,.1]) 
    gs.update(wspace= 1)
    
    cmaps = ['RdBu_r', 'viridis', 'viridis']
    
    axs = [[],[]]
    for i in range(2):
      for j in range(3):
          ax = plt.subplot(gs[i, j])
          axs[i].append(ax)
          image = ax.pcolormesh(
              np.random.random((20, 20)) * (j + 1),
              cmap=cmaps[j])
    
    # ax is not necessary if cax is given
    cb = plt.colorbar(
        image,
        cax=plt.subplot(gs[-1, 1:]),
        orientation='horizontal')
    

    【讨论】:

    • 如果我不清楚,我很抱歉。我希望颜色条水平放置在底部跨越第 2 列和第 3 列。
    • 我已更新我的答案以产生所需的行为。
    • 非常感谢。这就是我一直在寻找的。有效。 :)
    • @wagnificio 另外,你能告诉我我是否想做plt.legend() 而不是colorbar() 我该怎么做?因为,plt.legend() 没有cax 参数。
    猜你喜欢
    • 2019-11-07
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 2018-09-27
    • 2014-04-03
    • 2018-08-28
    • 1970-01-01
    相关资源
    最近更新 更多