Skip to content

Unexpected plot() matplotlib time shift #18571

@Grismar

Description

@Grismar

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions