WeirdGeek

Data Science | Machine Learning | Automation

  • Data Analytics
  • Python
  • Data Science
  • Google Apps Script
  • Machine Learning
  • Artificial Intelligence
  • SQL Server

24/11/2018 By WeirdGeek Leave a Comment

Creating Time Series with Line Charts using Python’s Matplotlib library

In this post, we will see how we can create Time Series with Line Charts using Python’s Matplotlib library. Basically, in Data Visualization, Time series charts are one of the important ways to analyse data over a time. In general, any chart that shows a trend over a time is a Time series chart and usually its a line chart that we use to see time series data.

When should you use Line chart/ Time series?

To understand this, let’s consider an example where you have been appointed as a Sales Manager in an online retail store and you want to understand the behaviour of the customer and types of customers from whom your company is getting sales revenue and you have sales figures of your retail company of last one year. You started analysing a few things like:

  • The week trend like on which day you have more sales in comparison to other days
  • and, On that day, particular duration of hours your website is getting more customers order.

For all sort of thing, your first choice is a line chart. Now let’s see how you can create Time Series with Line Charts using Python’s Matplotlib library.

Creating Time Series with Line Charts using Python’s Matplotlib library:

Suppose we want to find the GDP per capita  of Japan and China and compare their GDP per capita growth over time.

Let’s start by importing important libraries that will help us to implement our task.

  • Pandas library in this task will help us to import our ‘countries.csv’ file.
  • And the second and most important library which helps us to visualize our data is Matplotlib. It will help us to plot multiple bar graph.

Here’s how you can import:

import pandas as pd
import matplotlib.pyplot as plt

Now we want to import our file countries.csv which will help us in answering the above questions. If you want to download and use the CSV file, you can download it from here and follow with us. We will going to use pandas .read_csv() function as shown below:

data = pd.read_csv('countries.csv')
data.head()

Then we will select only data of Japan and China from out Countries.csv file by using below code:

Japan= data[data.country == 'Japan']
China = data[data.country == 'China']

To create a plot of Japan and China’s GDP per Capita, we can use the code below:

plt.plot(Japan.year, Japan.gdpPerCapita)
plt.plot(China.year, China.gdpPerCapita)
plt.legend(['Japan', 'China'])
plt.xlabel('Year')
plt.ylabel('GDP Per Capita')
plt.title('GDP Per Capita of china and Japan')
plt.show()

Creating Time Series with Line Charts

But by looking at the above chart it’s quite hard to tell which country has grown faster in the relative term because their absolute values are so different. What if instead of comparing the absolute value, we want to compare growth?

To do this first let’s set the GDP Per Capita for the first year to 100 and then compare how it grew for Japan and China with respect to that year.

So what we will do is, divide each year’s GDP per capita by the first year’s GDP per capita. To select the first year GDP per capita we will use .iloc[] for both Japan and China as shown below:

Japan.lifeExpectancy.iloc[0]
Output-
3216.956347
China.lifeExpectancy.iloc[0]
Output-
400.44861099999997

Then divide the GDP per capita by first-year’s GDP per capita (scalar value) for respective countries i.e. for Japan and China and multiply it with 100.

Japan_growth = Japan.gdpPerCapita / Japan.gdpPerCapita.iloc[0] * 100
China_growth = China.gdpPerCapita / China.gdpPerCapita.iloc[0] * 100

To create the plot we can do the same that we did before as shown below:

plt.plot(Japan.year, Japan_growth)
plt.plot(China.year, China_growth)
plt.legend(['Japan', 'China'])
plt.xlabel('Year')
plt.ylabel('GDP Per Capita')
plt.title('GDP Per Capita growth (first year = 100)')
plt.show()

Creating Time Series with Line Charts

 

Now, by looking at the above plot we can conclude that up to 1958 both countries have almost same GDP per capita but later Japan’s grows much faster than China upthe to year 2000 and afterwards China’s growth is more than Japan’s GDP growth. So this is how you can create Time Series with Line Charts.

Hope you like our post. If you want to learn how to plot different types of charts using the Python’s Matplotlib library check here. To learn more about Matplotlib package, you can go through the official documentation here.

Related posts:

  1. Plot multiple bar graph using Python’s Plotly library
  2. Plotting multiple bar graph using Python’s Matplotlib library
  3. Plotting stacked bar graph using Python’s Matplotlib library
  4. Plotting stacked histogram using Python’s Matplotlib library

Filed Under: Data Analytics, Python Tagged With: Line Chart, Matplotlib, Time Series

Leave a Reply Cancel reply

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

Subscribe to my Blog !!

Enter your email below to subscribe my blog and get the latest post right in your inbox.

  • Home
  • Terms
  • Privacy
  • Contact Us

Copyright © 2025 · WeirdGeek · All trademarks mentioned are the property of their respective owners.