我正在测量温度数据,我得到的.csv如下:
Date/Time,to [°C]
06.12.2018 11:56:07,-26.2
06.12.2018 11:56:09,-26.2
06.12.2018 11:56:11,-26.2
06.12.2018 11:56:13,-26.2
06.12.2018 11:56:15,-26.2我想画出在两个不同的时间跨度内进行的两种同类测量的值。那么,我是否需要标准化日期时间,或者如何使它们在折线图中具有可比性?谢谢你的帮助。
发布于 2018-12-11 23:23:16
使用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')

发布于 2018-12-11 23:17:41
您的意思是,您希望在绘制比较值时忽略时间戳?下面是如何在没有时间戳的情况下将两个时间序列绘制在一起。我添加了第二个具有随机温度的列表。
import matplotlib.pyplot as plt
import numpy as np
other_list = [-23,-29,-10 ,0,3]
t = np.arange(len(df))
plt.figure(figsize=(10,7))
plt.plot( t,df['to [°C]'].values, 'r--s',t, other_list, 'b--s')
plt.legend(('FirstDf', 'SecDf'),
loc='center right',title = 'visualization')
plt.show()https://stackoverflow.com/questions/53726696
复制相似问题