英文:
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.figure
function 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的功能。
要获得上面的图形,我已经执行了以下操作:
- 保留了
grille.cbar_axes
的内容。 - 使用
grille.cbar_axes
来放置颜色条。 - 将suptitle的位置交给Matplotlib决定。
- 使用
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()
附录
- 您可以使用适当的关键字参数来更改颜色条的“厚度”和间距,例如,我已经使用了
cbar_size="2%"
和cbar_pad=0.05
。 - 关于空白空间,当标题、图和颜色条被打包时,具有明确定义的纵横比,额外的空白空间根据图的纵横比垂直或水平添加 — 这是当
figsize=(13.5, 5)
时发生的情况。
英文:
You don't trust Matplotlib capabilities.
To obtain the above figure, I have
- left alone the contents of
grille.cbar_axes
- used
grille.cbar_axes
to place the colorbar - left the suptitle's position to be decided by Matplotlib
- 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
- You can change the "thickness" and the spacing of the colorbar using appropriate keyword arguments to
AxesGrid
; e.g., in the following I've usedcbar_size="2%", cbar_pad=0.05
- 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论