保存数字以具有相同大小的独立于 x 刻度、ylabels

问题描述

我正在尝试创建一些用于发布的数字。当我将它们插入乳胶中时,它们排列得不好。本质上是因为 x 刻度和标签不同,它们最终看起来像这样:

enter image description here

用于保存这些数字的代码如下:

size_f = 20
#### Set labels ####
ax.set_ylabel(r'$W_{kin} \ [eV]$',fontsize=size_f)
ax.set_xlabel(r'$Time \  [sec]$',fontsize=size_f)


#### Set fig dimensions ####
plt.rcParams["figure.figsize"] = [10,8]

#### Set tick size ####
ax.tick_params(axis='x',labelsize=size_f)
ax.tick_params(axis='y',labelsize=size_f)


#### Save figure ####
fig.savefig(r'C:\Users\nikos.000\vlahos\png\Dw_UCS.png',format='png',dpi=300,bBox_inches='tight')

如何使盒子具有相同的大小,以便图形更好地排列,独立于轴刻度和标签

解决方法

我强烈建议您使用 matplotlib.pyplot.subplots。您可以在不打乱整体外观的情况下调整每个子图的特征。我添加了代码供您使用您的项目。

import matplotlib.pyplot as plt

x = range(10)
y = range(10)

size_f = 12

labels = ("(a)","(b)","(c)","(d)")

fig,ax = plt.subplots(2,2,figsize=(10,8))
ax[0,0].plot(x,y,'r')
ax[0,0].text(-0.1,1.,labels[0],transform=ax[0,0].transAxes,fontsize=15,fontweight='normal',va='top',ha='right')
ax[0,1].plot(x,'b') 
ax[0,1].text(-0.1,labels[1],1].transAxes,ha='right')
ax[1,'g') 
ax[1,labels[2],transform=ax[1,'k') 
ax[1,labels[3],ha='right')

for row in range(2):
    for col in range(2):
        ax[row,col].set_ylabel(r'$W_{kin} \ [eV]$',fontsize=size_f)
        ax[row,col].set_xlabel(r'$Time \  [sec]$',fontsize=size_f)

fig.subplots_adjust(wspace=0.3)
fig.subplots_adjust(hspace=0.3) 
fig.savefig(r'C:\Users\nikos.000\vlahos\png\Dw_UCS.png',format='png',dpi=300,bbox_inches='tight')

图: enter image description here

由于每个子情节都有共同和独立的特征,因此我没有将所有内容都放在 for-blocks 中。

,

使用 matplotlib.pyplot.subplots 可以在同一图像中整齐地获得多个图。