Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot plot predicted time series values using matplotlib

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?

like image 355
Joey12 Avatar asked Apr 01 '18 07:04

Joey12


People also ask

How do I plot time series data in Matplotlib?

In X-axis we should have a variable of DateTime. In Y-axis we can have the variable which we want to analyze with respect to time. plt. plot() method is used to plot the graph in matplotlib.

What is Matplotlib Pyplot as PLT?

matplotlib.pyplot is a collection of functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.


1 Answers

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'

like image 99
Javier González Berenguel Avatar answered Oct 02 '22 17:10

Javier González Berenguel