【问题标题】:Adding one colorbar for hist2d subplots and make them adjacent为 hist2d 子图添加一个颜色条并使它们相邻
【发布时间】:2020-07-18 14:15:09
【问题描述】:

我正在努力调整情节,我一直在努力。 我面临两个问题:

  1. 绘图应该是相邻的,并且 wspace 和 hspace 为 0。我将这两个值都设置为零,但图之间仍然有一些空格。
  2. 我想为所有子图设置一个颜色条(它们的范围都相同)。现在,代码在最后一个子图中添加了一个颜色条,因为我知道它需要 hist2D 的第三个返回值。

到目前为止,这是我的代码:

def plot_panel(pannel_plot):
fig, ax = plt.subplots(3, 2, figsize=(7, 7), gridspec_kw={'hspace': 0.0, 'wspace': 0.0}, sharex=True, sharey=True)
fig.subplots_adjust(wspace=0.0)
ax = ax.flatten()
xmin = 0
ymin = 0
xmax = 0.19
ymax = 0.19
hist2_num = 0
h =[]
for i, j in zip(pannel_plot['x'].values(), pannel_plot['y'].values()):
    h = ax[hist2_num].hist2d(i, j, bins=50, norm=LogNorm(vmin=1, vmax=5000), range=[[xmin, xmax], [ymin, ymax]])
    ax[hist2_num].set_aspect('equal', 'box')
    ax[hist2_num].tick_params(axis='both', top=False, bottom=True, left=True, right=False,
                              labelsize=10, direction='in')
    ax[hist2_num].set_xticks(np.arange(xmin, xmax, 0.07))
    ax[hist2_num].set_yticks(np.arange(ymin, ymax, 0.07))
    hist2_num += 1

fig.colorbar(h[3], orientation='vertical', fraction=.1)
plt.show()

以及相应的结果:

Result

如果我错过了任何提醒,我会很高兴!

【问题讨论】:

    标签: python-3.x matplotlib histogram subplot colorbar


    【解决方案1】:

    您可以使用ImageGrid,它旨在使此类事情变得更容易

    data = np.vstack([
        np.random.multivariate_normal([10, 10], [[3, 2], [2, 3]], size=100000),
        np.random.multivariate_normal([30, 20], [[2, 3], [1, 3]], size=1000)
    ])
    
    
    from mpl_toolkits.axes_grid1 import ImageGrid
    
    fig = plt.figure(figsize=(4, 6))
    grid = ImageGrid(fig, 111,  # similar to subplot(111)
                     nrows_ncols=(3, 2),  # creates 2x2 grid of axes
                     axes_pad=0.1,  # pad between axes in inch.
                     cbar_mode="single",
                     cbar_location="right",
                     cbar_pad=0.1
                    )
    for ax in grid:
        h = ax.hist2d(data[:, 0], data[:, 1], bins=100)
    fig.colorbar(h[3], cax=grid.cbar_axes[0], orientation='vertical')
    

    data = np.vstack([
        np.random.multivariate_normal([10, 10], [[3, 2], [2, 3]], size=100000),
        np.random.multivariate_normal([30, 20], [[2, 3], [1, 3]], size=1000)
    ])
    
    
    from mpl_toolkits.axes_grid1 import ImageGrid
    
    fig = plt.figure(figsize=(4, 6))
    grid = ImageGrid(fig, 111,  # similar to subplot(111)
                     nrows_ncols=(3, 2),  # creates 2x2 grid of axes
                     axes_pad=0.1,  # pad between axes in inch.
                     cbar_mode="single",
                     cbar_location="top",
                     cbar_pad=0.1
                    )
    for ax in grid:
        h = ax.hist2d(data[:, 0], data[:, 1], bins=100)
    fig.colorbar(h[3], cax=grid.cbar_axes[0], orientation='horizontal')
    grid.cbar_axes[0].xaxis.set_ticks_position('top')
    

    【讨论】:

    • 这正是我所需要的。谢谢你。我不知道 ImageGrid。很有帮助!
    猜你喜欢
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    相关资源
    最近更新 更多