How to Generate Time Series Plot in Pandas

Pandas DataFrame.plot() method is used to generate a time series plot or line plot from the DataFrame. In time series data the values are measured at different points in time. Some of the time series are uniformly spaced at a specific frequency, for example, hourly temperature measurements, the daily volume of website visits, yearly counts of population e.t.c.

Time series can also be irregularly spaced, for example, events in a log file, or a history of 911 emergency calls. In this article, I will explain the concept of a time series and how to plot the time series from the given pandas DataFrame.

1. Quick Examples of Time Series Plot

Following are quick examples of how to create a time series plot.


# Below are quick examples 

# Example 1: Create DataFrame 
seattle_temps = data.seattle_temps()

# Example 2: Get the min & max temparatures
df = seattle_temps.groupby('date').agg(['min','max'])

# Example 3: Get the single line plot
df['min'].plot()

# Example 4: create timeseries plot
df.plot(x="date", y="min")
plt.xlabel("Date",  size = 20)
plt.ylabel("Minimum Temperature", size = 20)
plt.title("Minimum temperature of Seattle", size = 25)

# Example 5: Line plot of DataFrame
df.plot()
plt.xlabel("Index", size = 20)
plt.ylabel("Temp", size = 20)
plt.title("Minimum temperature of Seattle", size = 25)

# Example 6: Timeseries plot of DataFrame
df.set_index('date').plot(rot=60)
plt.xlabel("Date", size = 20)
plt.ylabel("Temp", size = 20)
plt.title("Minimum temperature of Seattle", size = 25) 

2. Syntax of Pandas plot()

Following is the syntax of the plot() function which I will be using to create a time series plot.


# Syntax of plot()
DataFrame.plot(*args, **kwargs)

2.1 Parameters of plot() function

Following are the parameters of the plot() function.

data: Series or DataFrame.

x: label or position, default None. Only used if data is a DataFrame.

y: label, position or list of label, positions, default None. It allows plotting of multiple columns. Only used if data is a DataFrame.

Kind: str

The kind of plot to produce:

  • line - line plot (default)
  • bar - vertical bar plot
  • barh - horizontal bar plot
  • hist - histogram
  • box - boxplot
  • kde - Kernel Density Estimation plot
  • density - same as ‘kde’
  • area - area plot
  • pie - pie plot
  • scatter - scatter plot (DataFrame only)
  • hexbin - hexbin plot (DataFrame only)
  • **kwargs: Options to pass to matplotlib plotting method.

2.2 Return Value

It returns matplotlib.axes.Axes or numpy.ndarray of them

3. Usage of Plot() function.

Python Pandas library is mainly focused on data analysis and it is not only a data visualization library but also using this we can create basic plots. When we want to create exploratory data analysis plots, pandas is highly useful and practical. It provides plot() and several other wrapper functions for visualizing our data. Let’s use this pandas plot() function to create a time series plot.

Here I have taken weather data of Seattle city from vega_datasets and using pandas I will plot the time series or line plot of the given dataset.

To access these datasets from Python, you can use the Vega datasets python package.

Let’s import weather data of Seattle city, Here columns are date and temp. The date column is in the form of yyyy-mm-dd.


# Import weather dataset
import pandas as pd
import numpy as np
from vega_datasets import data
import matplotlib.pyplot as plt

# Load seattle temperature data
seattle_temps = data.seattle_temps()
print(seattle_temps.shape)
print(seattle_temps.head())
print(seattle_temps.tail())

Yields below output.


# shape() output
(8759, 2)

# head() output
                 date  temp
0 2010-01-01 00:00:00  39.4
1 2010-01-01 01:00:00  39.2
2 2010-01-01 02:00:00  39.0
3 2010-01-01 03:00:00  38.9
4 2010-01-01 04:00:00  38.8

# tail() output
                    date  temp
8754 2010-12-31 19:00:00  40.7
8755 2010-12-31 20:00:00  40.5
8756 2010-12-31 21:00:00  40.2
8757 2010-12-31 22:00:00  40.0
8758 2010-12-31 23:00:00  39.6

4. Create Sample line Plot

By using Seattle’s weather data, let’s make a simple plot using plot() function directly using the temp column.


# Create a line plot 
seattle_temps['temp'].plot()

Yields below output.

Line plot of temperature

As you can see from the above, we have got a line plot with all the data, here band showing the minimum and maximum temperature for every data. For every hour the temperature data changes over a day. Also, we can observe indices of DataFrame on the x-axis, not the date column.

5. Prepare Data with Time Series

Let’s set the date column as an index so that we can make line plots with a data point for each day. To do so, let’s remove the time part of the datetime column.


# Convert date column as simple date
seattle_temps['date'] = seattle_temps['date'].dt.date
print(seattle_temps.tail())

Yields below output.


            date  temp
8754  2010-12-31  40.7
8755  2010-12-31  40.5
8756  2010-12-31  40.2
8757  2010-12-31  40.0
8758  2010-12-31  39.6

Let’s also get minimum and maximum temperatures for each day using Pandas groupby() function along with pandas agg() function.


# Get the min & max temparatures
df = seattle_temps.groupby('date').agg(['min','max'])
print(df)

Yields below output.


           temp      
             min   max
date                  
2010-01-01  38.6  43.5
2010-01-02  38.8  43.8
2010-01-03  39.0  44.0
2010-01-04  39.2  44.2
2010-01-05  39.3  44.4
         ...   ...
2010-12-27  37.9  42.8
2010-12-28  38.1  43.0
2010-12-29  38.1  43.0
2010-12-30  38.2  43.1
2010-12-31  38.4  43.3

[365 rows x 2 columns]

Using the pd.droplevel() function we can drop the multi-level column index, here I can drop the level 0 index of a given DataFrame to make a flattened Dataframe. Then, reset the index using reset_index() function.


# drop the level 0 column & set the index
df.columns = df.columns.droplevel(0)
df.reset_index(level=0, inplace=True)
print(df.head())

Yields below output.


         date   min   max
0  2010-01-01  38.6  43.5
1  2010-01-02  38.8  43.8
2  2010-01-03  39.0  44.0
3  2010-01-04  39.2  44.2
4  2010-01-05  39.3  44.4

6. Make a Single Line plot

By using the above-created dataframe let’s plot the min temperature across different days.


# Get the single line plot
df['min'].plot()
pandas time series plot
Minimum temperature of Line Plot with Pandas

7. Create Timeseries plot in Pandas

Let’s create timeseries plot with minimum temperature on y-axis and date on x-axis using plot() function directly on the DataFrame. Using Matplotlib.pyplot we can give the labels of the axis and the title of the plot. For example,


# create timeseries plot
df.plot(x="date", y="min")
plt.xlabel("Date",  size = 20)
plt.ylabel("Minimum Temperature", size = 20)
plt.title("Minimum temperature of Seattle", size = 25)
Minimum temperature of Line Plot with Pandas

8. Customize the Timeseries

We can customize the plots using any keyword arguments pass into plot() function. rot keyword allows rotating the markings on the x-axis for horizontal plotting and y-axis for vertical plotting, size keyword allows to set the font size for the labels of axis points and title of the plots, and colormap keyword argument allows to choose different color sets for the plots.

Here, I use the rot keyword into plot() function, it will rotate the marking of the x-axis horizontally.


# Customize the line plot
df.plot(x="date",y="min", rot = 60)
plt.xlabel("Date",size = 20)
plt.ylabel("Minimum temperature", size = 20)
plt.title("Minimum temperature of Seattle", size = 25)
Pandas timeseries plot
Minimum temperature of timeseries Plot with Pandas

9. Make a default Line Plot using DataFrame

Here, I will create a line plot of the given DataFrame using plot() function, it will take default indices on the x-axis and min and max columns on the y-axis. Finally, it will return the double-line plot.


# Line plot of DataFrame
df.plot()
plt.xlabel("Index", size = 20)
plt.ylabel("Temp", size = 20)
plt.title("Minimum temperature of Seattle", size = 25)

pandas time series plot
Line Plot of temperature using Pandas

Now, We can set the date column on the x-axis and make a time-series plot. For, that we need to reset the index of the data frame with our date variable and then, apply the plot() function it will return the time series of the given DataFrame.


# Timeseries plot of DataFrame
df.set_index('date').plot(rot=60)
plt.xlabel("Date", size = 20)
plt.ylabel("Temp", size = 20)
plt.title("Minimum temperature of Seattle", size = 25)
Pandas timeseries plot
Timeseries Plot of temperature using Pandas

10. Conclusion

In this article, I have explained the concept of a time series plot and by using the plot() function how to plot the time series DataFrame. Also explained how we can customize the time series plot and line plots using optional parameters.

Happy learning !!

References

Leave a Reply

You are currently viewing How to Generate Time Series Plot in Pandas