Python 如何使用双y轴绘图绘制四个子绘图
我已经成功地画出了四个子图。后来,我想在子地块的上绘制双yy图(一个x轴和两个y轴图)。在通过添加新代码进行了一系列试验之后,我终于得到了它。但问题是,出现了第五个空阴谋。请检查下面的屏幕截图。 我的代码的必需部分如下所示:Python 如何使用双y轴绘图绘制四个子绘图,python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,我已经成功地画出了四个子图。后来,我想在子地块的上绘制双yy图(一个x轴和两个y轴图)。在通过添加新代码进行了一系列试验之后,我终于得到了它。但问题是,出现了第五个空阴谋。请检查下面的屏幕截图。 我的代码的必需部分如下所示: fig, axs = plt.subplots(2, 2, figsize=(14, 10)) fig, add_ax = plt.subplots() plt.rc('font',family='Times New Roman') . . # yy ax
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
答案修改了我的代码,它解决了我的问题。下面是最后的数字
从代码中删除以下行(其余所有内容保持不变):
add_ax=axs[1][1].twinx()
单独就足够了,它将在底部右侧子批次中添加第二个y
轴。最初,我这样做了。弹出一个错误,说明未定义add_ax。还有其他猜测吗?如果你只做了fig,axs=plt.subplot(2,2,figsize=(14,10))
和添加ax=axs[1][1]。twinx()
,你仍然会得到一个错误吗?我试过了,它没有出现任何错误。我试过这个:axs[1][1]=add_ax.twinx(),它给出了错误。你做错了。你应该试试另一种方法。在axs[1][1]=add_ax.twinx()
中,您假设add_ax
已经存在,现在正在创建一个新的y轴axs[1][1]
。但实际上,您已经有了axs[1][1]
,并且希望在axs[1][1]
中创建第二个y
轴。因此,您应该编写add_ax=axs[1][1].twinx()
。我会试试看,明天再给你回复。谢谢。我是python新手,非常感谢您为我提供的输入。
fig, add_ax = plt.subplots()