Skip to content

Misaligned X axis when plotting datetime with same frequency but different step base #29719

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

Open
HenrikSpa opened this issue Nov 19, 2019 · 0 comments
Labels

Comments

@HenrikSpa
Copy link

HenrikSpa commented Nov 19, 2019

Code Sample, a copy-pastable example if possible

import pandas as pd
import matplotlib.pyplot as plt

x1 = pd.date_range(start='2018-03-02 12:00:00', end='2019-10-21 12:00:00', freq='1h')
df1 = pd.DataFrame([0]*len(x1), index=x1, columns=['x1'])
x2 = pd.date_range(start='2017-06-04 06:00:01', end='2019-11-10 06:00:01', freq='1h')
df2 = pd.DataFrame([0]*len(x2), index=x2, columns=['x2'])

fig, axes = plt.subplots(nrows=4, ncols=1, sharex=True)
df1.plot(ax=axes[0])
df2.plot(ax=axes[1])
axes[2].plot(df1)
axes[3].plot(df2)

#Autoscale doesn't affect this issue. Just used to make all lines visible from start.
plt.autoscale(enable=True, axis='both', tight=None)

for ax in axes:
    ax.legend().remove()

# printouts
print(str(axes[0].get_xlim()))
print(str(axes[1].get_xlim()))
print(str(axes[2].get_xlim()))
print(str(axes[3].get_xlim()))
plt.show()

image

Problem description

The x-axes are misaligned when plotting two or more subplots and have different timestep base (in the example above, x1 starts with hour:minute:second 00:00:00 while x2 starts with 06:00:01.

UPDATE: Also, plotting df1 using axes[2].plot(df1) instead of df1.plot(ax=axes[0]) works perfectly.

If plt.autoscale is not used, the topmost plot, df1.plot(ax=axes[0]), is not visible unless one pan the axes manually.

The result from get_xlim() from the supplied code returns

(736484.2500115741, 737373.2500115741)
(736484.2500115741, 737373.2500115741)
(736484.2500115741, 737373.2500115741)
(736484.2500115741, 737373.2500115741)

If the df2.plot(ax=axes[1]) is plotted before the other three, the result of the get_xlim() is instead

(422220.0, 436572.0)
(422220.0, 436572.0)
(422220.0, 436572.0)
(422220.0, 436572.0)

This issue might be related to:
#11574, #18571 and #29705.

Expected Output

Plotting the data at the correct x axis coordinates.

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
INSTALLED VERSIONS

commit : None
python : 3.6.8.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-65-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : sv_SE.UTF-8
LOCALE : sv_SE.UTF-8

pandas : 0.25.3
numpy : 1.17.4
pytz : 2019.3
dateutil : 2.8.1
pip : 9.0.1
setuptools : 40.8.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.2.1
html5lib : 0.999999999
pymysql : None
psycopg2 : 2.7.4 (dt dec pq3 ext lo64)
jinja2 : 2.10
IPython : None
pandas_datareader: None
bs4 : 4.6.0
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.2.1
matplotlib : 3.0.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.2.0
sqlalchemy : None
tables : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None

@jbrockmendel jbrockmendel added the Visualization plotting label Dec 11, 2019
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
@mroeschke mroeschke added the Bug label Jul 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants