Open In App

8 Types of Plots for Time Series Analysis using Python

Last Updated : 06 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Time series data

Time series data is a collection of observations chronologically arranged at regular time intervals. Each observation corresponds to a specific time point, and the data can be recorded at various frequencies (e.g., daily, monthly, yearly). This type of data is very essential in many fields, including finance, economics, climate science, and others as it helps to grasp underlying patterns, spot trends, and spot seasonal fluctuations by analyzing time series data.

What is Time Series Analysis?

Techniques for evaluating time-series data to determine relevant statistics and other data properties are referred to as time series analysis. Any time series with a recurrent pattern, including those in the financial markets, the weather, and social media statistics, can be subjected to it. The primary goal of time series analysis is to investigate key ideas concerning market trends and economic cycles.

Visualizations are vital in the process of obtaining insightful information from time series data and enable us to comprehend complex relationships and make intelligent decisions.

This article covers several types of plots that will help you with time series analysis using Python, with detailed examples using a freely accessible dataset.

Dataset description

Dataset: Sunspots dataset

What are Sunspots?

Sunspots are areas of the Sun’s surface, known as the photosphere, where the magnetic field is concentrated and results in reduced surface temperature compared to its surroundings. The complex magnetic activity of the Sun is what generates these black areas.

Sunspots have been observed and documented for many years, some of the earliest records being from ancient civilizations. Understanding solar behavior, space weather, and their possible impacts on the climate and communication systems of Earth is greatly aided by their research. Sunspots are still being studied by scientists to learn more about the dynamics of the Sun’s magnetic field and how it affects our solar system.

Dataset variables: The dataset consists of 2 columns – “Month” and “Sunspots” from 1749 to 1983. It basically describes the number of sunspots that were seen on the sun each month was recorded in this dataset.

Let’s begin coding now!

Importing necessary libraries

Python3




import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from statsmodels.graphics.tsaplots import plot_acf
from statsmodels.graphics.tsaplots import plot_pacf


Importing the dataset

Python3




# Load the Monthly Sunspots dataset
data = pd.read_csv(url, parse_dates=['Month'], index_col='Month')
print(data)


Output:

            Sunspots
Month
1749-01-01 58.0
1749-02-01 62.6
1749-03-01 70.0
1749-04-01 55.7
1749-05-01 85.0
... ...
1983-08-01 71.8
1983-09-01 50.3
1983-10-01 55.8
1983-11-01 33.3
1983-12-01 33.4
[2820 rows x 1 columns]

Now, we can start understanding various types of plot along with their implementation in Python.

Types of Plots

1. Time Plot

One of the most basic representations of time series data is the time plot, sometimes called a time series plot. The x-axis is time, and the y-axis is the relevant variable, and it shows data points in chronological order.

The temporal plot is utilized in this research to show the monthly fluctuation in sunspot numbers across the complete dataset period from 1749 to 1983. We can learn more about the overall trend, seasonal trends, and any potential outliers or abnormalities in the sunspot data by looking at the time map.

Here is a code example of a time plot

Python3




# Time plot
plt.figure(figsize=(7, 5))
plt.plot(data.index, data['Sunspots'], marker='o', linestyle='-', markersize=5)
plt.xlabel('Date')
plt.ylabel('Number of Sunspots')
plt.title('Monthly Sunspots Time Plot')
plt.grid(True)
plt.show()


Output:

Monthly Sunspots Time Plots - Geeksforgeeks

Over the full dataset period, the time plot displays the monthly variance in the number of sunspots. Here, the highest number of sunspots occurred around 1960, while the lowest around 1800.

2. Line Plot

A simple visualization that links data points with straight lines is known as a line plot. A continuous view of the time series data is provided, emphasizing the trend and changes in the variable over time. It is mainly used to track the long-term patterns in the data.

The line plot is employed in our study to display the long-term trends in the number of sunspots seen on the sun. This enables us to determine the general trend of sunspot activity and whether the number of sunspots is rising, falling, or staying the same over time.

let’s plot it using python now

Python3




# Line plot
import matplotlib.pyplot as plt
plt.figure(figsize=(7, 5))
plt.plot(data)
plt.xlabel('Date')
plt.ylabel('Number of Sunspots')
plt.title('Monthly Sunspots Line Plot')
plt.grid(True)
plt.show()


Output:

Monthly Sunspots Line Plot-Geeksforgeeks

Now, time plot and line plot look almost the same, however it is important to note that a line plot is a general term for a plot that displays the relationship between two continuous variables, without any specific emphasis on time.

On the other hand, a time plot is a specialized form of a line plot that focuses on visualizing how a variable changes over time, with time being the x-axis variable. Time plots are particularly useful when dealing with time series data, where observations are ordered chronologically and recorded at regular time intervals.

3. Seasonal Plot

The seasonal plot breaks down time series data into seasonal components to illustrate patterns that reoccur over predetermined time intervals, such as annual or monthly cycles. It enables us to recognize recurring trends in sunspot activity, such as variations in activity throughout the year.

Here is a code example of a seasonal plot

Python3




# Seasonal plot
plt.figure(figsize=(7, 5))
sns.lineplot(x=data.index.month, y=data['Sunspots'], ci=None)
plt.xlabel('Month')
plt.ylabel('Number of Sunspots')
plt.title('Seasonal Plot')
plt.xticks(range(1, 13), labels=[
           'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
plt.grid(True)
plt.show()


Output:

Seasonal plots-Geeksforgeeks

From this seasonal plot, we can learn more about the seasonality of solar activity which reveals the months that tend to have higher or lower sunspot activity.

4. Histogram and Density Plot

A density plot is defined as a graphical representation which is used to visualize the distribution of data and estimate the probability density function (PDF) of a continuous random variable. It is basically a smoothed version of a histogram, providing a continuous curve that represents the underlying data distribution. These plots are suitable when dealing with large datasets or when a smooth, continuous representation of the data is needed to shed light on how values are distributed throughout the time series.

Here is a code example of a histogram and density plot

Python3




# Histogram and Density Plot
plt.figure(figsize=(7, 5))
sns.histplot(data['Sunspots'], kde=True)
plt.xlabel('Number of Sunspots')
plt.ylabel('Frequency')
plt.title('Histogram and Density Plot')
plt.grid(True)
plt.show()


Output:

Histogram and Density Plot-Geeksforgeeks

The density plot and histogram show the frequency distribution of the number of sunspots, which aids in our comprehension of the form of the sunspot distribution and its central tendency.

5. Autocorrelation Plot

Autocorrelation plot, also known as a correlogram is defined as a time series analysis tool used to display the autocorrelation of a time series with itself at various lags. The link between a data point and its prior observations at various time lags is measured by autocorrelation. It basically represents the correlation between a time series and its own lagged values. The plots are mainly used for identifying the seasonal lags in the data

Let’s plot an autocorrelation plot using python now

Python3




# Autocorrelation Plot
plt.figure(figsize=(7,5))
plot_acf(data['Sunspots'], lags=50)
plt.xlabel('Lags')
plt.ylabel('Autocorrelation')
plt.title('Autocorrelation Plot')
plt.grid(True)
plt.show()


Output:

Autocorrelation plot=Geeksforgeeks

The autocorrelation plot displays the correlation at different lags, which is useful for understanding the seasonal patterns in sunspot activity. A substantial autocorrelation at particular delays suggests that sunspot activity may follow a yearly pattern.

6. Partial Autocorrelation Function (PACF) Plot

The Partial Autocorrelation Function (PACF) plot is a graphical tool used in time series analysis to determine the autoregressive (AR) order of a time series. While accounting for the effects of all intermediate delays, it displays the direct influence of each lag on the time series’ current value. Basically, it examines the correlation between a data point and its prior observations without taking into account the effect of the intervening time steps.

Here is a code example of a partial autocorrelation function (PACF) plot

Python3




# PACF plot
plt.figure(figsize=(7, 5))
plot_pacf(data['Sunspots'], lags=50)
plt.xlabel('Lags')
plt.ylabel('Partial Autocorrelation')
plt.title('Partial Autocorrelation Function (PACF) Plot')
plt.grid(True)
plt.show()


Output:

Partial autocorrelation function (PACF) plot- Geeksforgeeks

In our approach, the number of lag observations with a substantial influence on the current sunspot count is determined using the PACF plot. The PACF aids in choosing the AR model’s forecasting order for sunspot activity.

7. Polar Plot

A polar plot is a data visualization plot in which the data points are arranged in a circular pattern. The angle around the circle and the radial distance from the center are used to represent various variables or data properties. These are mainly used for visualizing the seasonal patterns in a data set.

Here is a code example of a polar plot

Python3




# Extracting the month and year from the index of the above dataset "Monthly Sunspots"
data['Month_Num'] = data.index.month
  
# Grouping the data by month, calculating the average number of sunspots for each month
monthly_average = data.groupby('Month_Num')['Sunspots'].mean()
  
# Polar Plot theta (angle) and radii (length) settings
theta = np.linspace(0, 2 * np.pi, len(monthly_average))
radii = monthly_average.values
  
# Polar Plot
plt.figure(figsize=(7, 5))
plt.polar(theta, radii)
plt.title('Polar Plot of Monthly Average Sunspots')
plt.xticks(theta, ['Jan', 'Feb', 'Mar', 'Apr', 'May',
                   'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
  
# Set y-axis limit to accommodate the data
plt.ylim(0, radii.max() + 10)
plt.show()


Output:

Polar Plot of Monthly Average Sunspots-Geeksforgeeks

The polar plot in this particular case displays the average number of sunspots every month. The radial distance from the center reflects the average number of sunspots for each month, while the angular location within the circle represents the months (e.g., January, February, March, etc.). It reveals any seasonal tendencies present in the data and allows us to see cyclic patterns in the annual cycle of sunspot activity.

The monthly average sunspot view is captivatingly displayed in the polar plot, which also reveals cyclical trends throughout the year.

8. Moving Average Plot

In time series analysis, a Moving average plot is a popular data visualization approach for spotting trends and patterns in the data. Its main objective is to eliminate short-term fluctuations and reveal long-term trends in the data by determining the average of a specified window of succeeding data points.

The data points within the window are averaged, and the resulting values are shown on a graph to generate a moving average plot. It is simpler to spot and compare trends and seasonal patterns when the Moving Average line is positioned over the original data graph.

Here is a code example of a moving average plot

Python3




# Moving Average Plot
plt.figure(figsize=(7, 5))
values = data['Sunspots']
  
# 7-day moving average
rolling_mean = values.rolling(window=7).mean()
plt.plot(values, label='Original')
plt.plot(rolling_mean, label='7-day Moving Average', color='red')
plt.xlabel('Date')
plt.ylabel('Value')
plt.title('Moving Average Plot')
plt.legend()
plt.grid(True)
plt.show()


Output:

Moving average plot-Geeksforgeeks

We are plotting a 7-day moving average here. We know that a moving average is a technique for smoothing data by determining the average of a predetermined window of subsequent data points. The number of data points utilized for averaging depends on the window size, commonly referred to as the moving average period. This method involves averaging the initial sunspot data over a period of seven days.

Conclusion:

By utilizing these various plots and data visualization techniques, we can gain a comprehensive understanding of the “Monthly Sunspots” dataset, identify patterns, and extract valuable insights about solar activity over several centuries.

As a result of this research, you now possess the knowledge necessary to apply these techniques to time series datasets in a variety of sectors and industries. Time series analysis provides vital tools for acquiring insightful insights and making data-driven decisions in any field requiring sequential data, including economics, finance, climate studies, and other areas. By adopting these methods, we have the ability to utilize time-dependent data and tap into its predictive potential to address real-world issues.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads