更改 matplotlib 中多个子图的 y 轴
Changing y axis on multiple subplots in matplotlib
我对 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 轴?这对我有很大帮助!
谢谢,
您正在尝试索引 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'
.
您似乎枚举了轴上想要的名称列表,因此您可以在每次迭代中使用当前名称,如下所示:
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 还很陌生。我有以下问题:
我想绘制 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 轴?这对我有很大帮助!
谢谢,
您正在尝试索引 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'
.
您似乎枚举了轴上想要的名称列表,因此您可以在每次迭代中使用当前名称,如下所示:
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])