-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Misaligned shared axes between timeseries plots of different frequency. #11574
Comments
@juseg Julien did you ever figure this misalignment problem out? I am running into same problem plotting two signals at different frequencies. MATLAB can do it using Linkaxes property, but I don't believe Python's sharex property is exactly the same thing. |
@AeroWRX I updated the script above for changes in the resample API (cf v0.18.0 changelog) and tested against pandas 0.23.1.
The resulting figure has aligned x-axes. I think the issue has been solved. I am don't know about Matlab.
|
The above code seems to work in the more recent pandas versions. On the other hand combining pandas |
There's a lot of magic going on between how the datetime64 values actually encode datetime in plots. Sharing an axis across (sub)plots is brittle w.r.t. these differences. Work around this, here: make it so that individual timestamps have a non-zero value for seconds, by simply adding one second, shifting the whole data set by one second to the left. That prevents, I guess, an optimization to hit in which would see that individual timestamps hit the full hour or integer multiples of 30 or 15 minutes. Also see pandas-dev/pandas#15874 pandas-dev/pandas#15071 pandas-dev/pandas#31074 pandas-dev/pandas#29705 pandas-dev/pandas#29719 pandas-dev/pandas#18571 pandas-dev/pandas#11574 pandas-dev/pandas#22586
I am observing that plotting series with different frequencies and data densities produces misaligned plots and I think it might be related by this bug, but several similar ones have been reported (#29705, #11574, #18571) so I'm not 100 % sure. To reproduce: import pandas as pd
dates = ["2019-01-31", "2019-02-28", "2019-03-31"]
values = [1000.0, 1100.0, 1200.0]
s_monthly = pd.Series(values, index=pd.DatetimeIndex(dates, freq="M"))
s_none = s_monthly.asfreq(None).interpolate()
ax = s_monthly.plot(marker="o")
s_none.plot(marker="o", ax=ax)
ax.figure.savefig("pandas_misalignment_bug.png") Data: Result: |
Has this issue been fixed? If not I would love to contribute to it. |
@rhshadrach - I could see the issue still exist in pandas =2.2.3 |
Observed in pandas 0.17.0 and the current development version (d78266e).
It seems to me that pandas fail to align timeseries plots with different frequency unit (or no frequency) when they are drawn on different axes with one shared axis. The issue does not occur if the two timeseries are plotted on the same axes.
Example:
After the third series is drawn, the first two appear shifted out of the frame to wrong x-coordinates.
Version info:
Thanks!
The text was updated successfully, but these errors were encountered: