【问题标题】:Changing y axis on multiple subplots in matplotlib在 matplotlib 中的多个子图上更改 y 轴
【发布时间】:2017-05-18 17:59:21
【问题描述】:

我对 python 和 matplotlib 还很陌生。我有以下问题:

我想绘制超过 2000 年的六个气候驱动因素。由于每个图代表一个不同的驱动程序,我想给每个 y 轴一个不同的标签,但无法索引每个子图。请看以下代码及后续错误信息:

###Plot MET drivers
fig3 = plt.figure(figsize=(20, 14))

for ii, name_MET in enumerate (["Day_since_start_of_sim", "Min._Temp", "Max._Temp", "Radiation","CO2_(ppm)","DOY"]):
        ax3 = fig3.add_subplot(2,3,ii+1)
        ax3.plot(drivers,'g-',label='2001')
        ax3.set_title(name_MET)
        ax3.set_xlabel('years')
        ax3[1].set_ylabel('Day_since_start_of_simulation')
        ax3[2].set_ylabel('Degrees C')
        ax3[3].set_ylabel('Degrees C')
        ax3[4].set_ylabel('MJ m-2 d-1')
        ax3[5].set_ylabel('ppm')
        ax3[6].set_ylabel('Days')
        ax3.set_xticks(incr_plot_years)
        ax3.set_xticklabels((incr_plot_years/365).astype('S'), rotation = 45)
        ax3.set_xlim(0,ax3.get_xlim()[1])
        ax3.set_ylim(0,ax3.get_ylim()[1])

错误信息:

    287     ax3.set_title(name_MET)
    288     ax3.set_xlabel('years')
--> 289     ax3[1].set_ylabel('Day_since_start_of_simulation')
    290     ax3[2].set_ylabel('Degrees C')
    291     ax3[3].set_ylabel('Degrees C')

TypeError: 'AxesSubplot' object does not support indexing

谁能帮助我如何分别命名每个 y 轴?对我有很大帮助!

谢谢,

【问题讨论】:

  • 您的问题是否得到充分解决?如果是这样,您可以通过使用复选框标记正确答案来帮助其他用户。否则,这些答案缺少什么?

标签: python matplotlib plot axes


【解决方案1】:

您似乎枚举了要在轴上使用的名称列表,因此您可以在每次迭代中使用当前名称,如下所示:

fig3 = plt.figure(figsize=(20, 14))

名称=[“Day_since_start_of_sim”,“Min._Temp”,“Max._Temp”,
        "辐射","CO2_(ppm)","DAY"]

对于 ii,枚举中的 name_MET(名称):
    ax3 = fig3.add_subplot(2,3,ii+1)
    ax3.plot(drivers[ii],'g-',label='2001')# - 不清楚什么是驱动程序?
    ax3.set_title(name_MET)
    ax3.set_xlabel('年')
    ax3.set_ylabel(name_MET)
    ax3.set_xticks(incr_plot_years) # - 不清楚
    ax3.set_xticklabels((incr_plot_years/365).astype('S'), 旋转 = 45)
    ax3.set_xlim(0,ax3.get_xlim()[1])
    ax3.set_ylim(0,ax3.get_ylim()[1])

【讨论】:

    【解决方案2】:

    您试图索引到ax3,但ax3 代表一个子图,而不是整个图。相反,您应该在相应的循环迭代上标记每个 y 轴。试试这个:

    names_MET = ["Day_since_start_of_sim", "Min._Temp", "Max._Temp", "Radiation","CO2_(ppm)","DOY"]
    ylabels = ['Day_since_start_of_simulation', 'Degrees C', 'Degrees C', 'MJ m-2 d-1', 'ppm', 'Days']
    
    for ii, (name_MET, ylabel) in enumerate(zip(names_MET, ylabels)):
        ax3 = fig3.add_subplot(2,3,ii+1)
        ax3.plot(drivers,'g-',label='2001')
        ax3.set_title(name_MET)
        ax3.set_xlabel('years')
        ax3.set_ylabel(ylabel)
        ax3.set_xticks(incr_plot_years)
        ax3.set_xticklabels((incr_plot_years/365).astype('S'), rotation = 45)
        ax3.set_xlim(0,ax3.get_xlim()[1])
        ax3.set_ylim(0,ax3.get_ylim()[1])
    

    这里的重要好处是您可以为 y 轴提供自定义标签,而不必使用您在 names_MET 中定义的值。例如,单位 'Degrees C' 而不是简单的 'Min._Temp'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      相关资源
      最近更新 更多