Skip to content
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

Open
juseg opened this issue Nov 11, 2015 · 6 comments
Open

Misaligned shared axes between timeseries plots of different frequency. #11574

juseg opened this issue Nov 11, 2015 · 6 comments
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions

Comments

@juseg
Copy link

juseg commented Nov 11, 2015

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:

"""Shared axes between timeseries plots of different frequency."""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
pd.show_versions()

# prepare a random time series
t = pd.date_range('1/1/2000', periods=1000)
x = np.random.randn(1000)
ts = pd.Series(x, index=t).cumsum()

# initialize figure
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True)

# plot original and resampled timeseries
ts.plot(ax=ax1)                 # works as expected
ts.resample('30D').plot(ax=ax2) # works as expected
ts.resample('1M').plot(ax=ax3)  # seem to mess up x axis

# show
plt.show()

After the third series is drawn, the first two appear shifted out of the frame to wrong x-coordinates.

Version info:

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.3.final.0
python-bits: 64
OS: Linux
OS-release: 3.2.0-4-amd64
machine: x86_64
processor: 
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.17.0+159.gd78266e
nose: 1.3.7
pip: 7.1.2
setuptools: 14.3.1
Cython: 0.22
numpy: 1.10.1
scipy: 0.16.0
statsmodels: None
IPython: 0.13.1
sphinx: 1.3.1
patsy: None
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: 0.7.4
apiclient: None
sqlalchemy: 0.7.8
pymysql: None
psycopg2: 2.4.5 (dt dec mx pq3 ext)

Thanks!

@Stigmaru
Copy link

Stigmaru commented Jul 6, 2018

@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.

@juseg
Copy link
Author

juseg commented Jul 9, 2018

@AeroWRX I updated the script above for changes in the resample API (cf v0.18.0 changelog) and tested against pandas 0.23.1.

ts.resample('30D').mean().plot(ax=ax2) # works as expected
ts.resample('1M').mean().plot(ax=ax3) # seem to work now

The resulting figure has aligned x-axes. I think the issue has been solved. I am don't know about Matlab.

INSTALLED VERSIONS
------------------
python: 2.7.15.final.0
pandas: 0.23.1
numpy: 1.14.5
matplotlib: 2.2.2

@juseg
Copy link
Author

juseg commented Jan 28, 2020

The above code seems to work in the more recent pandas versions. On the other hand combining pandas ts.plot() with matplotlib ax.plot() methods produces wrong results as for the above referenced other issues.

@mroeschke mroeschke added Bug good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug Testing pandas testing functions or related to the test suite Visualization plotting labels May 22, 2020
jgehrcke added a commit to jgehrcke/ci-analysis that referenced this issue Dec 7, 2020
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
@astrojuanlu
Copy link

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:

Screenshot 2022-01-30 at 22-22-35 Session 05 p… (7) - JupyterLab

Result:

pandas_misalignment_bug

@madhuramkumar
Copy link

Has this issue been fixed? If not I would love to contribute to it.

@palbha
Copy link

palbha commented Feb 21, 2025

@rhshadrach - I could see the issue still exist in pandas =2.2.3
Let me know if this needs work ?

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

No branches or pull requests

6 participants