首页 > 解决方案 > 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?

标签: pythonplotresampling

解决方案


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.


推荐阅读