如何更改物体的位置。

huangapple go评论61阅读模式
英文:

How to change the positions of the objects

问题

如您在下面的图像中所看到的,我的整个绘图(无论是热图、标题还是色条)都“挤在”图/窗口的顶部,并且底部有很大的空白空间。

如何更改物体的位置。

有没有办法将它完全下移,并减小底部的空白空间?

我已经尝试使用了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('不同间隙的可视化', fontsize=15, fontweight='bold', y=0.97)
    plt.show()
英文:

As you can see on the image below, my entire plot (whether it's the heatmaps, the title or the colorbar) is “tighten” in the top of the figure/window and there is a large blank space in the bottom

如何更改物体的位置。

Is there a way to lower it entirely AND to reduce the blank space in the bottom?

I've already tried to use the fig.tight_layout() and to change the values of the figsize argument of the plt.figurefunction but the space in the bottom stays the same (proportionally)...

Here is the piece of code:

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

答案1

得分: 2

您不信任Matplotlib的功能。

要获得上面的图形,我已经执行了以下操作:

  1. 保留了grille.cbar_axes的内容。
  2. 使用grille.cbar_axes来放置颜色条。
  3. 将suptitle的位置交给Matplotlib决定。
  4. 使用plt.tight_layout()将所有留白减至最小。
def graphs():
    fig = plt.figure(figsize=(13.5, 6.8), dpi=60) # 低dpi以适应我的屏幕窗口
    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. 您可以使用适当的关键字参数来更改颜色条的“厚度”和间距,例如,我已经使用了cbar_size="2%"cbar_pad=0.05
  2. 关于空白空间,当标题、图和颜色条被打包时,具有明确定义的纵横比,额外的空白空间根据图的纵横比垂直或水平添加 — 这是当figsize=(13.5, 5)时发生的情况。
英文:

You don't trust Matplotlib capabilities.

如何更改物体的位置。

To obtain the above figure, I have

  1. left alone the contents of grille.cbar_axes
  2. used grille.cbar_axes to place the colorbar
  3. left the suptitle's position to be decided by Matplotlib
  4. used plt.tight_layout() to have all the spaces left to a minimum
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()

Addendum

  1. You can change the "thickness" and the spacing of the colorbar using appropriate keyword arguments to AxesGrid; e.g., in the following I've used cbar_size="2%", cbar_pad=0.05
    如何更改物体的位置。
  2. Re white space, the whole of titles, plots and colorbar when packed has a well defined aspect ratio, additional white space is added vertically or horizontally according to the aspect ratio of the figure — here it is what happens when figsize=(13.5, 5)
    如何更改物体的位置。

huangapple
  • 本文由 发表于 2023年6月2日 05:13:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定