Cannot plot predicted time series values using matplotlib
Asked Answered
V

1

13

I am trying to plot my actual time series values and predicted values but it gives me this error:

ValueError: view limit minimum -36816.95989583333 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units

I am using statsmodels to fit an arima model to the data.

This is a sample of my data:

datetime             value
2017-01-01 00:00:00  10.18
2017-01-01 00:15:00  10.2
2017-01-01 00:30:00  10.32
2017-01-01 00:45:00  10.16
2017-01-01 01:00:00  9.93
2017-01-01 01:15:00  9.77
2017-01-01 01:30:00  9.47
2017-01-01 01:45:00  9.08

This is my code:

mod = sm.tsa.statespace.SARIMAX(
    subset,
    order=(1, 1, 1),
    seasonal_order=(1, 1, 1, 12),
    enforce_stationarity=False,
    enforce_invertibility=False
)

results = mod.fit()
pred_uc = results.get_forecast(steps=500)
pred_ci = pred_uc.conf_int(alpha = 0.05)

# Plot
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(1, 1, 1)
ax.plot(subset,color = "blue")
ax.plot(pred_uc.predicted_mean, color="black", alpha=0.5, label='SARIMAX')
plt.show()

Any idea how to fix this?

Vergara answered 1/4, 2018 at 7:17 Comment(13)
Please provide a MCVECamorra
@VivekKalyanarangan I removed some unnecessary lines of code .. just edited now.Vergara
Also give a sample of the data you are working onCamorra
@VivekKalyanarangan doneVergara
How do you define subset?Dart
df = pd.read_csv('interpolated-detailed.csv') df.set_index('datetime') subset = df[(df['datetime'] > "2017-01-04") & (df['datetime'] < "2017-01-05")] subset = subset.iloc[:, 0:2] @DartVergara
I tried to reproduce your example but sm.tsa.statespace.SARIMAX doesn't like my subset for some reason: ValueError: Pandas data cast to numpy dtype of object. Check input data with np.asarray(data). Converting with pd.to_datetime didn't helpDart
did you try subset.set_index(subset["datetime"]) before calling sm.tsa.statespace.SARIMAXVergara
sorry meant subset = subset.set_index("datetime") @DartVergara
Ok, now it works, and, actually, I don't get any errors. Did you try printing subset and pred_uc.predicted_mean before plotting to check if the format of datetimes is the same?Dart
@Dart The error has gone now , I don't remember how , but now I have another problem that The ARIMA model is not well fitted with the data.Vergara
Then I suggest creating another post. And this one should be closed as "a problem that can no longer be reproduced"Dart
I just ran into this issue. My problem was that the datetime column actually contained strings and not dates. So I had to convert using pd.to_datetime. My next guess was to use this answer: https://mcmap.net/q/910069/-add-date-tickers-to-a-matplotlib-python-chartLattermost
U
1

It should have been an issue on how you provide the data.

The datetime values must be the index of the values in your data subset variable, and thus, the following works.

I've imported the data as follows right before the code you provided:

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import statsmodels.api as sm

subset = pd.Series(

[

    10.18, 10.2 , 10.32,
    10.16, 9.93, 9.77,
    9.47, 9.08

]

, index=pd.date_range(

    start='2017-01-01T00:00:00.000',

    end='2017-01-01T01:45:00.000',

    freq='15T'

)  )

And I got, I believe, your desired plot (it's cut):

plot_result_cut.

I used these versions of the libraries, in Python 3:

matplotlib.version '3.1.2'

numpy.version '1.17.4'

pandas.version '0.25.3'

statsmodels.version '0.12.0'

Union answered 9/10, 2020 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.