如何在其中绘制带有双Y轴图的四个子图

时间:2018-08-21 17:11:39

标签: python python-3.x matplotlib

我已经成功绘制了四个子图。后来,我希望子图上有两个yy图(一个x轴和两个y轴图)。通过添加新代码进行了一系列试验之后,我做到了。但是,问题是,出现了第五个空地。请检查以下屏幕截图。 enter image description here

下面是“我的代码”的必需部分:

fig, axs = plt.subplots(2, 2, figsize=(14, 10))
fig, add_ax = plt.subplots()
plt.rc('font',family='Times New Roman')        
.
.
# yy axis plot in fourth subplot
add_ax = axs[1][1].twinx()
axs[1][1].set_xlabel('Irradiance (W/m^2)', fontsize=16)
axs[1][1].set_ylabel('Power (W)', fontsize=16)
add_ax.set_ylabel('Module temperature (Deg. C)', fontsize=16)

for i,j in zip(IV_start_index,IV_start_index[1:]):  # This is simple code to access present and next element in a list
    .
    .
    .

    axs[1][1].plot(module_allData_df['Irradiance'].iloc[i],p_mpp,'go')        

    add_ax.plot(module_allData_df['Irradiance'].iloc[i],module_allData_df['Temperature'].iloc[i],'bo')
    plt.suptitle('A NIST Module %s day normalized IV curves '%(module_allData_df['Time'].loc[i].strftime('%Y-%m-%d')),fontsize=18) #         
    plt.savefig('All_day_normalized_IV_plot')

在某些地方,我的代码是错误的。

我已根据下面接受的@Bazingaa答案修改了代码,从而解决了我的问题。下面是最终的数字。

enter image description here

1 个答案:

答案 0 :(得分:1)

从代码中删除以下行(其余内容保持不变):

fig, add_ax = plt.subplots()

add_ax = axs[1][1].twinx()仅够用,并且会在您的底部右侧子图中添加第二个y轴。

相关问题