Search code examples
pythonmatplotlibplotautocorrelation

How to make a plot show everytime after a run through a for loop and not let the lines stack up


I am trying to show 4 autocorrelation plots, 1 for each column with values of my dataframe. I'm using a for loop to make it show 4 times. But the result is that the autocorrelation plot only shows one time with 4 lines instead of showing up 4 times with each plot only 1 line. My code is as followed: (Ignore the libraries that are being imported but not used)

from dateutil.parser import parse
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
plt.rcParams.update({'figure.figsize': (5, 3), 'figure.dpi': 120}
from statsmodels.tsa.seasonal import seasonal_decompose
from dateutil.parser import parse
from scipy import signal
from pandas.plotting import autocorrelation_plot

# Import Data
ser = pd.read_csv('Time series analysis dataset Weekly.csv', parse_dates=['Date'], index_col='Date')
ser = ser[ser.YearOfDate != 2020]

ValueNames = ['Europe', 'Total', 'International', 'Distributor']
ColorsList = ['Blue','Green','Orange','Purple','Brown']

# Draw Plot
plt.rcParams.update({'figure.figsize':(7,4), 'figure.dpi':120})
for ValueRound in range(0,len(ValueNames)):
   autocorrelation_plot(ser[f'{ValueNames[ValueRound]}'].tolist())

The result I get by running the script is

enter image description here

Because of the for loop, 4 lines have been plotted, 1 for each value name. The desired result is to have 4 seperate plots with each 1 line. In other scripts where I use a normal plot I used plt.plot.show() within the for loop. This results in 4 plots with only 1 line. How van I do this for an autocorrelation plot?

Im following this guide for my time series analysis https://www.machinelearningplus.com/time-series/time-series-analysis-python/, i've based my script on the script in chapter 14: How to test for seasonality.

Here is another picture with the desired plot. To get this plot I've only looped threw the for loop 1 time, resulting in only ONE line for the value name Europe. See

enter image description here


Solution

  • Resolved the problem by adding plt.show() at the end of the for loop instead of plt.plot.show()