python - Plot the resampled function over the original one
问题描述
I'm writing a code in which I have to resample the data column of a dataframe and I want to plot the resampled function over the original function line. Now, i correctly resampled the function (5T, 5 minutes) and I can print the new value correctly. When I try to plot the single function is perfect, but when I try to subplot them I can't have the x-axis with my timestamps from 'Date', that is similar between both of the functions with a shift of some minutes, and the values are not overlapped but separated
I already created everything, used the subplot() and the twinx. Here's my code
originalFuncForSingleID = originalFunc[(originalFunc['ID'])==IDVal]
originalFuncForSingleIDResampled = originalFuncForSingleID.set_index('Date').resample('5T').mean().reset_index()
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(x=originalFuncForSingleID['Date'], y=originalFuncForSingleID['Value'], use_index=True)
ax2.plot(x=originalFuncForSingleIDResampled['Date'], y=originalFuncForSingleIDResampled['Value'], use_index=True)
ax1.set_xlabel('Date')
ax1.set_ylabel('Value original', color='g')
ax2.set_ylabel('Value resampled', color='b')
plt.rcParams['figure.figsize'] = 12, 5
plt.show()
My result should be the original function line with an overlapped function (the resampled) that shows the changes and the newly created function. How can I do that? Where am I wrong?
解决方案
Turns out I solved it myself without the multiple axs. Here's the code:
f, ax = plt.subplots(1)
plt.title('Title')
originalFuncForSingleID.plot(kind='line', x='Date', y='Value', color='brown', label='Originasl', ax=ax)
originalFuncForSingleIDResampled.plot(kind ='line', x='Date', y='Value', color='green', label='Resampled', ax=ax)
plt.legend()
plt.show()
Using this I had an overlapped function over the original one, showing the differences with a different color.
推荐阅读
- python - 创建一个包含相似词的新列
- phpmyadmin - 我不小心取消了 phpmyadmin 中 root 的所有权限
- ocaml - 找到没有重复的对列表的并集
- reactjs - 使用 Redux 和组件状态是个好主意吗?
- android - Android 在 Zebra 扫描仪上获取序列号
- npm - 有没有办法停止纱线最新版本检查?
- html - res ['casesbyState'] 出错,无法解决
- php - 添加带有披露的标签时无法使用复选框输入
- c++ - 切换以对所有情况执行相同的功能以及其中一种情况的单个打印语句
- dapper - Dapper 与 Oracle 给出 ORA-00936:缺少表达式错误