首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python /R中绘制两个不同时间戳中的两个变量

在Python /R中绘制两个不同时间戳中的两个变量
EN

Stack Overflow用户
提问于 2021-02-10 21:25:59
回答 1查看 25关注 0票数 1

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

EN

回答 1

Stack Overflow用户

发布于 2021-06-04 08:52:10

代码语言:javascript
复制
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()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66137783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档