I followed a tutorial to do forecasting, who uses Matplotlib , but I don’t like it. I mean, I found more beautiful Plotly, because you can interact with graphics . So, I looked the documentation to convert Matplotlib to Plotly, but I don’t how can I do it and if I can do it, as I don’t have the data using the Matplotlib functions (plot_pacf, plot_acf, autocorrelation_plot and lag_plot) . I would like to ask if someone have any idea how to do it. I checked on Github the code of those functions, but nothing.
Thanks for helps.
I am having the same problem as @zero94. I would like to use plotly to plot acf and pacf. I already tried to convert the plots generated by pandas library to plotly (I followed the instructions in the documentation as well), but I got three different warnings:
- “Blended transforms not yet supported. Zoom behavior may not work as expected.”
- “Bummer! Plotly can currently only draw Line2D objects from matplotlib that are in ‘data’ coordinates!”
- “Dang! That path collection is out of this world. I totally don’t know what to do with it yet! Plotly can only import path collections linked to ‘data’ coordinates”
Is it possible to generate this type of plots by using plotly?
Thanks in advance.
I had the same issue and my solution was as followed:
from statsmodels.tsa.stattools import pacf
import plotly.graph_objects as go
# df['sum'] is my time series where i want the pacf of.
df_pacf = pacf(df['sum'], nlags=300)
fig = go.Figure()
I hope this solution helps someone
All you gotta do is pass the series as a function argument and it will give you ACF plot. And add an extra argument
True if you want the PACF plot.
def create_corr_plot(series, plot_pacf=False):
corr_array = pacf(series.dropna(), alpha=0.05) if plot_pacf else acf(series.dropna(), alpha=0.05)
lower_y = corr_array[:,0] - corr_array
upper_y = corr_array[:,1] - corr_array
fig = go.Figure()
[fig.add_scatter(x=(x,x), y=(0,corr_array[x]), mode='lines',line_color='#3f3f3f')
for x in range(len(corr_array))]
fig.add_scatter(x=np.arange(len(corr_array)), y=corr_array, mode='markers', marker_color='#1f77b4',
fig.add_scatter(x=np.arange(len(corr_array)), y=upper_y, mode='lines', line_color='rgba(255,255,255,0)')
fig.add_scatter(x=np.arange(len(corr_array)), y=lower_y, mode='lines',fillcolor='rgba(32, 146, 230,0.3)',
title='Partial Autocorrelation (PACF)' if plot_pacf else 'Autocorrelation (ACF)'
The function assumes you have imported the necessary functions (not the plot ones, the array ones) from statsmodels library.