使用ax.twiny 创建独立轴
这是一种方法。假设您有以下数据框:
df1 = pd.DataFrame({'date1':['06.12.2018 11:56:07', '06.12.2018 11:56:09', '06.12.2018 11:56:11', '06.12.2018 11:56:13' ],
'temp': [20.7,13,7,30]})
df2 = pd.DataFrame({'date2':['06.12.2018 21:56:07', '06.12.2018 21:56:09', '06.12.2018 21:56:11', '06.12.2018 21:56:13' ],
'temp': [3,-2,-5,3]})
df1.date1 = pd.to_datetime(df1.date1)
df2.date2 = pd.to_datetime(df2.date2)
您可以使用ax.twiny() 创建一个额外的 x 轴,这将创建一个共享 y 轴的双轴。这样您就可以在同一个图中拥有两个序列,并且不会丢失date 信息:
fig = plt.figure(figsize=(15,8))
ax = fig.add_subplot(111)
df1.plot(x='date1', y = 'temp', ax=ax, label='df1', c='red')
ax2 = ax.twiny()
df2.plot(x='date2', y = 'temp', ax=ax2, label = 'df2', c='blue')