python - 更改 matplotlib 中多个子图的 y 轴

标签 python matplotlib plot axes

我对 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 轴吗?这对我有很大帮助!

谢谢,

最佳答案

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

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

names =["Day_since_start_of_sim", "Min._Temp", "Max._Temp", 
        "Radiation","CO2_(ppm)","DAY"]

for ii, name_MET in enumerate (names):
    ax3 = fig3.add_subplot(2,3,ii+1)
    ax3.plot(drivers[ii],'g-',label='2001')# - unclear what drivers is?
    ax3.set_title(name_MET)
    ax3.set_xlabel('years')
    ax3.set_ylabel(name_MET)
    ax3.set_xticks(incr_plot_years) # - unclear
    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])

关于python - 更改 matplotlib 中多个子图的 y 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44054877/

相关文章:

python - pandas read_excel 同一张纸上的多个表

python - 如何增加表的大小?

python - 循环遍历 2d 子图,就好像它是 1-D

python - Matplotlib 散点图;颜色作为第三个变量的函数

python - pandas 条形图结合线图显示了从 1970 年开始的时间轴

python - 试图操纵数据,将列表分配给更高列表中的第一项,第二项将是有关该列表的信息

python - 在 Python 中使用原始套接字

python - 以编程方式更改 matplotlib 填充数据

python - 极坐标/径向图上的刻度标签填充和刻度标签位置 - matplotlib

Python 类和 ssh 连接不能正常工作