【问题标题】:How would I add a x axis label in plt.subplots?如何在 plt.subplots 中添加 x 轴标签?
【发布时间】:2020-12-05 12:46:16
【问题描述】:

我有一个非常复杂的 plt.subplots 因为我试图绘制 29 个不同的图。一切似乎都在工作,但我想在我的 x 轴上添加一个标签:

plt.figure(figsize=(30,30))
for i in range(int(len(fwf_tot_grnl.sum(dim=('X','Y')))/24)):

    # add a new subplot
    ax = plt.subplot(8, 4, i+1)

    # plot on the subplot
    ax.plot(time_months[i*24: (i+1)*24],fwf_tot_grnl.sum(dim=('X','Y'))[i*24: (i+1)*24])

    # other formating
    ax.set_xticks(np.arange(min(time_months[i*24: (i+1)*24]),max(time_months[i*24: (i+1)*24]),.25))

# want to add x label here!
    ax.set_xticklabels(['{:.2f}'.format(a) for a in np.arange(min(time_months[i*24: (i+1)*24]),max(time_months[i*24: (i+1)*24]),.25)], rotation=15)

    ax.grid()
    ax.set_title('FWF All Basins Monthly Data (2 year increments)')
    plt.tight_layout()

plt.show()

我想在这一行添加一个 xlabel:

ax.set_xticklabels(['{:.2f}'.format(a) for a in np.arange(min(time_months[i*24: (i+1)*24]),max(time_months[i*24: (i+1)*24]),.25)], rotation=15)

我试着做:

ax.set_xticklabels(['{:.2f}'.format(a) for a in np.arange(min(time_months[i*24: (i+1)*24]),max(time_months[i*24: (i+1)*24]),.25), 'Time'], rotation=15)

但我收到此错误:

 File "<ipython-input-48-4e6a31743d8a>", line 13
    ax.set_xticklabels(['{:.2f}'.format(a) for a in np.arange(min(time_months[i*24: (i+1)*24]),max(time_months[i*24: (i+1)*24]),.25),'Time'], rotation=15)
                                                                                                                                    ^
SyntaxError: invalid syntax

如何向我的 plt.subplots 添加一个简单的 x 标签?

【问题讨论】:

    标签: python numpy matplotlib jupyter


    【解决方案1】:
      ax.set_ylabel('FWF (km$^3$/yr)')
      ax.set_xlabel('Time')
    

    我查看的是 set x_ticks 而不是 xlabel!只需要做 set_ylabel/set_xlabel 来定义 x 和 y 轴标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 2018-12-08
      • 2014-06-29
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 2012-04-26
      相关资源
      最近更新 更多