【问题标题】:Save figures to have the same size independet of x ticks, ylabels保存图形以具有相同的大小,独立于 xticks、标签
【发布时间】:2021-04-21 02:25:20
【问题描述】:

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

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

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

我怎样才能使盒子具有相同的大小,以便数字排列得更好,而与轴刻度和标签无关。

【问题讨论】:

    标签: python matplotlib save png figure


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我强烈建议您使用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, y, 'b') 
      ax[0, 1].text(-0.1, 1., labels[1], transform=ax[0, 1].transAxes, fontsize=15, fontweight='normal', va='top', ha='right')
      ax[1, 0].plot(x, y, 'g') 
      ax[1, 0].text(-0.1, 1., labels[2], transform=ax[1, 0].transAxes, fontsize=15, fontweight='normal', va='top', ha='right')
      ax[1, 1].plot(x, y, 'k') 
      ax[1, 1].text(-0.1, 1., labels[3], transform=ax[1, 1].transAxes, fontsize=15, fontweight='normal', va='top', 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')
      

      图:

      由于每个子图都有共同和独立的特征,我没有将所有内容都放入for-blocks

      【讨论】:

      • 谢谢!太好了!你知道我如何改变标签的大小吗?
      • 您要求标签的字体大小吗?在ax[row, col].set_ylabel(r'$W_{kin} \ [eV]$', fontsize=size_f) 中,只需更改size_f。但是,我想,你想学习不同的东西。你能再解释一下吗?
      • 或者您是在谈论 (a)、(b) 等的标签吗?然后在ax[0, 0].text(-0.1, 1., labels[0], transform=ax[0, 0].transAxes, fontsize=15, fontweight='normal', va='top', ha='right') 中将fontsize 参数从15 增加到18 或更多。确保应用所有子图。 (ax[0, 0], ax[0, 1], ax[1, 0], ax[1, 1])
      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 2012-06-26
      • 2021-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      相关资源
      最近更新 更多