Line graph also known as line chart or line plot is used to track changes over a period of time. It’s ideal for clearly shows the trend especially when the changes are smaller. Line graphs are usually used for time series analysis. One axis usually x-axis is used to show time intervals and the other axis usually y-axis is used to show the continuous values. We can have one line or multiple lines in one graph each comparing the changes of individual measure over time. Matplotlib provides us with functions to create line graphs. In this post we will see when to use line graphs and how to create line graphs in Matplotlib. Download the dataset for this post from here.

matplotlib-logo

Line Graph Use-Cases

Line chart is one of the most common and basic type of visualization used for visualizing continuous values against continuous variable such as time, distance, speed etc. It’s best suited for visualizing data with chronological trends. It can be used to compare the change between two or more groups.

How to Use Line Graph

  1. Your data should has a chronological variable such as time, distance, etc.
  2. Select a good interval that will clearly show all data points.
  3. Use as fewer lines as possible when necessary to avoid cluttering the visualization.
  4. Ensure that the line/s cover almost 90% of the canvas.
  5. Show data points in the line.
  6. Interpolate the gaps in the line where necessary unless you want to detect missing data.
  7. When measuring two different metrics in the same graph select appropriate scales for dual axis.

Line Graph in Matplotlib

Load Data

                    

import matplotlib.pyplot as plt
import pandas as pd

plt.style.use(‘ggplot’) # Other sytles to use; fivethirtyeight

ex_rate_df=pd.read_csv('Exchange_Rates.csv')
ex_rate_df.head()

matplotlib-load-data

Plot Single Line Graph

Let’s Plot the Exchange Rate for FRA Currency

                    

fra_x=ex_rate_df[ex_rate_df['LOCATION']=='FRA']['TIME'].astype(str)
fra_y=ex_rate_df[ex_rate_df['LOCATION']=='FRA']['Value']

plt.style.use('ggplot') # Other sytles to use; fivethirtyeight
plt.figure(figsize=(20,10)) # Set figure size
plt.rcParams.update({'font.size': 22}) # Set axes size
plt.plot(fra_x,fra_y,color='orange',marker='*') # Plot the chart
plt.title('Exchange rates',fontsize=24)
plt.xticks(rotation=45)
plt.xlabel('Year',fontsize=24)
plt.ylabel('Rate',fontsize=24)
plt.legend(['FRA'], loc='upper right')
plt.show()

matplotlib-single-line-graph

 

Multiple Lines in a Graph

Let’s Add more Plots For the Exchange Rate for FRA, BGR and USD Baseline Currency

                    

fra_x=ex_rate_df[ex_rate_df['LOCATION']=='FRA']['TIME'].astype(str)
fra_y=ex_rate_df[ex_rate_df['LOCATION']=='FRA']['Value']
gbr_x=ex_rate_df[ex_rate_df['LOCATION']=='GBR']['TIME'].astype(str)
gbr_y=ex_rate_df[ex_rate_df['LOCATION']=='GBR']['Value']
usd_x=ex_rate_df[ex_rate_df['LOCATION']=='USA']['TIME'].astype(str)
usd_y=ex_rate_df[ex_rate_df['LOCATION']=='USA']['Value']

plt.figure(figsize=(20,10)) # Set figure size
plt.rcParams.update({'font.size': 22}) # Set axes size
plt.plot(fra_x,fra_y,color='orange',marker='*',label='FRA') # Plot the chart for FRA
plt.plot(gbr_x,gbr_y,color='skyblue',marker='*',label='GBR') # Plot the chart for GBR
plt.plot(usd_x,usd_y,color='red',marker='*',label='USD') # Plot the chart for USD
plt.title('Exchange rates',fontsize=24)
plt.xticks(rotation=45)
plt.xlabel('Year',fontsize=24)
plt.ylabel('Rate',fontsize=24)
plt.legend(['FRA', 'GBR','USD','RUS'], loc='lower right')
plt.show()

matplotlib-multiple-lines-graph

For complete code check jupyter notebook here

Conclusion

In this post we have looked at what’s line chart, when and to use it. Line charts are commonly used for visualizing time series data. Matplotlib gives us much flexibility for creating line chart. In the next post we will learn about Bar charts when and how to use them. To learn about Matplotlib and its capability check our previous post here.

Line Graph in Matplotlib

Post navigation


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x