Skip to content

time series plotting gap causes strange jump #16266

Closed as not planned
Closed as not planned
@Fealthas

Description

@Fealthas

Bug report

Gap in time series data causes MPL to jump to a very small datetime that doesnt exist in the dataset.
image

If I don't manually set the axis, it tries to autofit.

ax1.set_xlim( [ min(datana.date.dt.to_pydatetime()) , max(datana.date.dt.to_pydatetime()) ])

Autofitting looks like this:
image

Note that the minimum x datetime in the dataset is in 2019! Not sure where the 1680ish time comes from. I've manually checked what actually gets passed into mpl, a numpy object array of datetime objects, does not have a time value below 2019.

Abbreviated code:

import datetime
import time
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
import pandas as pd

fig1, ax1 = plt.subplots( figsize=(15,5) )

    data = pd.read_csv("temps.dat",header=None, names=['date','temp'])
    data.temp = data.temp/1000
    data.date = pd.to_datetime(data.date)
    data.date = data.date.dt.ceil(freq='s') #strip microseconds
    data['roll'] = data['temp'].rolling(window=722 ).mean()
    data['roll2'] = data['temp'].rolling(window=14220  ).mean()
    datana = data.dropna(subset=['roll'])
    datana2 = data.dropna(subset=['roll2'])

    ax1.cla()
 
    ax1.plot(datana2.date.dt.to_pydatetime()  ,datana2.roll2, lw=3, alpha=.8)
    ax1.plot(datana.date.dt.to_pydatetime()  ,datana.roll, lw=.7)

    ax1.set_xlim( [ min(datana.date.dt.to_pydatetime()) , max(datana.date.dt.to_pydatetime()) ])
fig1.autofmt_xdate()
fig1.savefig("fig1.png",format='png' ,bbox_inches='tight', dpi=500)

temps.zip
Matplotlib version

  • Operating system: debian (raspbian)

  • Matplotlib version: 3.1.2 , in a venv, installed by pip

  • Matplotlib backend (print(matplotlib.get_backend())): agg

  • Python version: 3.7

  • Other libraries: pandas, Pillow

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