xlabels 仅出现在两个子图中的一个中

xlabels only appear formatted in one of two subplots

我想用 subplot 函数和两个不同的 x 轴(日期时间格式)绘制 2 个图形。

我做了以下代码:

fig=plt.figure()
fig.set_size_inches(10,10)
plt.subplot(211)
plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S'))
plt.gca().xaxis.set_major_locator(dates.MinuteLocator())
plt.plot(WaveLab_df['Time'],WaveLab_df['Active_Power_Starboard_W'])
plt.plot(WaveLab_df['Time'],WaveLab_df['Active_Power_Portside_W'])
plt.gcf().autofmt_xdate()
plt.subplot(212)
plt.plot(df_tempS['DateTime'],df_tempS['Power[W]'])

我得到:

事实是,在第二个情节中,我希望有相同类型的 x 标签(小时-分钟)。我想要每个图的 x 轴。

我尝试了不同的组合,但没有成功。

有没有人作为想法?

谢谢!

您打开第二个子图后,我会尝试再次调用此行。

plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S'))

您需要在两个 子图中使用格式化程序和定位器。

fig=plt.figure()
fig.set_size_inches(10,10)

plt.subplot(211)
plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S'))
plt.gca().xaxis.set_major_locator(dates.MinuteLocator())
plt.plot(WaveLab_df['Time'],WaveLab_df['Active_Power_Starboard_W'])
plt.plot(WaveLab_df['Time'],WaveLab_df['Active_Power_Portside_W'])
plt.gcf().autofmt_xdate()

plt.subplot(212)
plt.plot(df_tempS['DateTime'],df_tempS['Power[W]'])
plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S'))
plt.gca().xaxis.set_major_locator(dates.MinuteLocator())
plt.gcf().autofmt_xdate()