Python Time Series Analysis – Line, Histogram, Density Plotting

1. Time Series Analysis in Python

In this Python tutorial, we will learn about Python Time Series Analysis. Moreover, we will see how to plot the Python Time Series in different forms like the line graph, Python histogram, density plot, autocorrelation plot, and lag plot.

So, let’s begin the Python Time Series Analysis.

Python Time Series

Python Time Series Analysis – Line, Histogram, Density Plotting

2. What is Time Series in Python?

Consider a sequence of points of data. Suppose we look at the rate of Dollar($) to Indian Rupee. We can link each point of data with a timestamp. Let’s try plotting for this rate over a period of one week. Let’s use Python pandas for this.

a. Data to Use in Python Time Series Analysis

We save the following data in a CSV file
Date, Rate
18-07-2018, 68.625
19-07-2018, 68.9453
20-07-2018, 68.745
21-07-2018, 68.747
22-07-2018, 68.7415
23-07-2018, 68.9449
24-07-2018, 68.9486
We save this as dollartorupee.csv.
Do you know about Python NumPy?

b. Plotting a Python Line Chart/Graph

Let’s use this data to plot a simple line graph with this.

>>> from pandas import Series
>>> from matplotlib import pyplot
>>> series=Series.from_csv('dollartorupee.csv',header=0)
>>> series.plot()

<matplotlib.axes._subplots.AxesSubplot object at 0x031948D0>

>>> pyplot.show()
Python Time Series Analysis

Plotting a Line Graph in Time Series Python Analysis

We can use a line style with this-
Learn more about Python SciPy

>>> series.plot(style='k.')

<matplotlib.axes._subplots.AxesSubplot object at 0x033969B0>

>>> pyplot.show()
Python Time Series Analysis

Plotting a Line Graph

3. Plotting a Python Histogram

Now to plot a Python histogram, we can try the hist() method.

>>> series.hist()

<matplotlib.axes._subplots.AxesSubplot object at 0x05D683D0>

>>> pyplot.show()
Python Time Series Analysis

Plotting a Python Histogram in Time Series Python Analysis

4. Plotting a Density Plot in Python Time Series

What if we want to find out the density of the rate values for the entire week?

>>> series.plot(kind='kde')

<matplotlib.axes._subplots.AxesSubplot object at 0x03194C70>
Let’s discuss Python Interpreter

>>> pyplot.show()
Python Time Series Analysis

Plotting a Density Plot in Time Series Python Analysis

5. Autocorrelation Plot in Python Time Series

This gives us how the elements of the series correlate to each other.

>>> from pandas.tools.plotting import autocorrelation_plot
>>> autocorrelation_plot(series)

<matplotlib.axes._subplots.AxesSubplot object at 0x08C5CA90>

>>> pyplot.show()
Python Time Series Analysis

Autocorrelation Plot in Time Series Python Analysis

Python Interview Questions

6. Plotting a Lag Plot in Python Time Series

Such a plot tells us whether a time series is random. If you can identify a structure in the plot, the data isn’t random.
Do you know about Python Matplotlib

>>> from pandas.tools.plotting import lag_plot
>>> lag_plot(series)

<matplotlib.axes._subplots.AxesSubplot object at 0x08EF4FF0>

>>> pyplot.show()
Python Time Series Analysis

Plotting a Lag Plot in Time Series in Python Analysis

So, this was all in Time Series Analysis in Python. Hope you like our explanation.

7. Conclusion

Hence, in this Python Time Series tutorial, we discussed what is Time Series, Time Series Analysis in Python and plotting in Python Time Series Analysis. With this, we conclude our tutorial on time series. Now you know how to plot it in different forms. Got any questions? Leave them in the comments below.
See also – 
Python Django 
For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.