python 如何改变物体的位置

pn9klfpd  于 2023-06-04  发布在  Python
关注(0)|答案(1)|浏览(148)

正如你在下面的图片中看到的,我的整个图(无论是热图、标题还是颜色条)在图/窗口的顶部都是“收紧”的,底部有一大片空白

    • 是否有办法完全降低它并减少底部的空白空间?**

我已经尝试使用fig.tight_layout()并更改plt.figure函数的figsize参数的值,但底部的空间保持不变(按比例)...
下面是一段代码:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid


def graphs():
    fig = plt.figure(figsize=(13.5, 6.8))
    Grid = AxesGrid(fig, 111, nrows_ncols=(1,3), axes_pad=0.2, share_all=False, label_mode='L', cbar_location="bottom", cbar_mode="single")

    Matrices = [np.random.randint(-100,10,(10,10)) for i in range(3)]
    M, m = np.max(Matrices), np.min(Matrices)

    for values, ax in zip(Matrices, Grid):

        ax.tick_params(top = True, bottom = False, labeltop = True, labelbottom = False)
        ax.xaxis.set_label_position('top')

        heatmap = ax.imshow(values, vmin=m, vmax=M, cmap='plasma')
        ax.set_xticks(np.arange(0,10), labels=list(np.arange(1,11)), fontsize=6)
        ax.set_xlabel('Arrival', fontweight='bold', fontsize=8)
        plt.setp(ax.get_xticklabels(), rotation=35, ha="center", rotation_mode=None)

        for l in range(len(values)):
            for c in range(len(values[l])):
                text = ax.text(c, l, values[l,c], ha="center", va="center", color='k', fontsize=6)

        ax.set_title('Gaps', fontweight='bold', fontsize=9)

    Grid[0].set_yticks(np.arange(0,10), labels=list(np.arange(1,11)), fontsize=6)
    Grid[0].set_ylabel('Depart', fontweight='bold', fontsize=8)

    for cax in Grid.cbar_axes:
        cax.remove()

    cbar = Grid[0].figure.colorbar(heatmap, ax = Grid, fraction=0.2, aspect=50, location='bottom', label='Gaps')
    plt.suptitle('Visualisation of different gaps', fontsize=15, fontweight='bold', y=0.97)
    plt.show()
bgibtngc

bgibtngc1#

你不信任Matplotlib的功能。

为了获得上述数字,我
1.不考虑grille.cbar_axes的内容
1.使用grille.cbar_axes放置颜色条
1.我把suptitle的位置留给Matplotlib来决定
1.使用plt.tight_layout()将所有空格保留到最小

def graphs():
    fig = plt.figure(figsize=(13.5, 6.8), dpi=60) # low dpi to fit the window on my screen
    grille = AxesGrid(fig, 111, nrows_ncols=(1,3),
                      axes_pad=0.2, share_all=False, label_mode='L',
                      cbar_location="bottom", cbar_mode="single")

    Matrices = [np.random.randint(-100,10,(10,10)) for i in range(3)]
    M, m = np.max(Matrices), np.min(Matrices)

    for values, ax in zip(Matrices, grille):

        ax.tick_params(top = True, bottom = False, labeltop = True, labelbottom = False)
        ax.xaxis.set_label_position('top')

        heatmap = ax.imshow(values, vmin=m, vmax=M, cmap='plasma')
        ax.set_xticks(np.arange(0,10), labels=list(np.arange(1,11)), fontsize=6)/
        ax.set_xlabel('Arrival', fontweight='bold', fontsize=8)
        plt.setp(ax.get_xticklabels(), rotation=35, ha="center", rotation_mode=None)

        for l in range(len(values)):
            for c in range(len(values[l])):
                text = ax.text(c, l, values[l,c], ha="center", va="center", color='k', fontsize=6)

        ax.set_title('Gaps', fontweight='bold', fontsize=9)

    grille[0].set_yticks(np.arange(0,10), labels=list(np.arange(1,11)), fontsize=6)
    grille[0].set_ylabel('Depart', fontweight='bold', fontsize=8)

    cbar = plt.colorbar(heatmap, cax=grille.cbar_axes[0], orientation='horizontal', label='Gaps')
    plt.suptitle('Visualisation of different gaps', fontsize=15, fontweight='bold')
    plt.tight_layout()
    plt.show()
graphs()

附录

1.您可以使用适当的关键字参数将颜色条的“厚度”和间距更改为AxesGrid;例如,在下文中,我使用了cbar_size="2%", cbar_pad=0.05

1.重新白色,标题,情节和colorbar的整体时, Package 有一个明确的长宽比,额外的空白是垂直或水平添加根据纵横比的数字-这里是发生什么时,figsize=(13.5, 5)

相关问题