Skip to content

Unexpected plot() matplotlib time shift #18571

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
Grismar opened this issue Nov 30, 2017 · 1 comment
Open

Unexpected plot() matplotlib time shift #18571

Grismar opened this issue Nov 30, 2017 · 1 comment
Labels
Bug Datetime Datetime data dtype Visualization plotting

Comments

@Grismar
Copy link

Grismar commented Nov 30, 2017

Code Sample

    from datetime import datetime
    import pandas as pd
    from matplotlib import pyplot as plt

    f = '%Y-%m-%d %H:%M:%S'

    data1 = [1, 2, 3]
    data2 = [4, 3, 2]
    ts1 = [datetime.strptime('2000-01-01 10:00:00', f), datetime.strptime('2000-01-02 10:00:00', f), datetime.strptime('2000-01-03 10:00:00', f)]
    ts2 = [datetime.strptime('2000-01-01 09:00:00', f), datetime.strptime('2000-01-02 10:00:00', f), datetime.strptime('2000-01-03 11:00:00', f)]

    a1 = pd.DataFrame(data1, ts1, ['a'])
    a2 = pd.DataFrame(data2, ts1, ['b'])

    b1 = pd.DataFrame(data1, ts1, ['a'])
    b2 = pd.DataFrame(data2, ts2, ['b'])

    fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(15, 5))

    a1.plot(ax=axs[0], color='r')
    a2.plot(ax=axs[0].twinx())

    b1.plot(ax=axs[1], color='r')
    b2.plot(ax=axs[1].twinx())

    axs[2].plot(ts1, data1, color='r')
    axs[2].twinx().plot(ts2, data2)

    fig.tight_layout()
    fig.savefig('../output/plottest.png', format='png')

Problem description

When making minor changes to a time series (adding a second for example), the entire series is shifted to seemingly arbitrary positions. In the example code, one of the time series is shifted to around 2053.

Note that the only difference between the data for the first and the second plot is the time series. ts1 is used on the b data series in the first and ts2 is used on the b data series in the second.

And the only difference between the time series is the hour of the first and the last day. If I change either to 10 (as in ts1), the problem disappears. Adding a single second to the first timestamp also 'fixes' the problem.

This is a problem because it appears that using the twinx, for unclear reasons, fails with specific values for a datetime x-axis.

output example

See also: my question on stackoverflow

Expected Output

The expected output would be plots that share the x-axis and show the plots at the correct time index.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.21.0
pytest: None
pip: 9.0.1
setuptools: 38.2.3
Cython: None
numpy: 1.13.3
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@TomAugspurger
Copy link
Contributor

Not sure offhand what's going on. IIRC, pandas stores some freq state on the axes object, perhaps that's causing issues with twinx?

I'd try to set some breakpoints / print statements in pandas.plotting._converter and see if anything jumps out to you as looking strange.

@TomAugspurger TomAugspurger added this to the Next Major Release milestone Nov 30, 2017
@TomAugspurger TomAugspurger added Datetime Datetime data dtype Visualization plotting labels Nov 30, 2017
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 Jun 12, 2021
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Visualization plotting
Projects
None yet
Development

No branches or pull requests

3 participants