我在两个不同的时间戳中有两个变量..要求是沿X轴和2 Y轴双轴的公共时间(同步)。我添加了我们需求的示例

发布于 2021-06-04 08:52:10
import pandas as pd
import matplotlib.pyplot as plt
#Sample datasets:
df1=pd.DataFrame(); df1['time']=pd.to_datetime(['2020-1-1 02:00','2020-1-1 03:00','2020-1-1 04:00']); df1['value']=[3,6,12]
df2=pd.DataFrame(); df2['time']=pd.to_datetime(['2020-2-1 12:00','2020-2-1 13:00','2020-2-1 14:00']); df2['value']=[1,5,3]
#synchronize time using the first time element in each dataset and convert it to seconds
df1['time_sync']=(df1['time']-df1.time[0]).astype('timedelta64[s]')
df2['time_sync']=(df2['time']-df2.time[0]).astype('timedelta64[s]')
#plot over two y axises:
fig,ax = plt.subplots()
ax.plot(df1['time_sync'],df1['value'],label='set1')
ax.set_ylim(0,13);ax.set_ylabel('set1_ylabel')
ax2=plt.twinx()
ax2.plot(df2['time_sync'],df2['value'],label='set2',c='r')
ax2.set_ylim(0,6);ax2.set_ylabel('set2_ylabel')
ax.legend()
ax2.legend()

https://stackoverflow.com/questions/66137783
复制相似问题