python-3.x - 为 hist2d 子图添加一个颜色条并使它们相邻

标签 python-3.x matplotlib histogram subplot colorbar

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

  • 这些图应该是相邻的,并且有 0 个 wspace 和 hspace。我将两个值都设置为零,但图之间仍然有一些空格。
  • 我想为所有子图使用一个颜色条(它们的范围都相同)。现在,代码向最后一个子图添加了一个颜色条,因为我知道它需要 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

    对于我失踪的任何提示,我会很高兴!

    最佳答案

    您可以使用 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')
    

    enter image description here

    或者
    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')
    

    enter image description here

    关于python-3.x - 为 hist2d 子图添加一个颜色条并使它们相邻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61062216/

    相关文章:

    python - 从时间序列中插值单个值

    python - 如何在背景 OpenCV 和 Python 上调整和转换蒙版图像的大小

    python - 计算字母出现次数 Python

    Python .extend 不必要地拆分字符

    python - 根据每个列表的子集从列表列表中删除重复项

    Python Pandas fillna()

    Python 网络托管 : Numpy, Matplotlib,科学计算

    python - 如何使用直方图识别文本区域?

    java - 骰子直方图

    python - GoTo(基本)程序