Python: How to Add a Trend Line to a Line Chart/Graph
In this article, you will learn how to add a trend line to the line chart/line graph using Python Matplotlib.
Join the DZone community and get the full member experience.Join For Free
In this article, you will learn how to add a trend line to the line chart/line graph using Python Matplotlib. As a data scientist, it proves to be helpful to learn the concepts and related Python code, which can be used to draw or add the trend line to the line charts as it helps understand the trend and make decisions.
In this post, we will consider an example of IPL average batting scores of Virat Kohli, Chris Gayle, MS Dhoni, and Rohit Sharma of the last 10 years and assess the trend related to their overall performance using trend lines. What's the main reason we want to understand the trend line? The primary goal is to assess who could the larger money be put in order to acquire him for the team? The batsman who has the largest upward trend line with the highest slope is the one I would like to put my money on. Having said that, the batting scores mean and variance/standard deviation also comes into the picture in taking the final decision on who to put larger money on.
Here are their batting average scores in IPL seasons from the last 10 years from 2010 - 2019:
Before getting ahead and understanding the code related to the line chart and related trendlines, let's understand why the trend line is needed.
Why/When Use a Trend Line With a Line Chart?
There are scenarios when a numerical entity changes with time, which is represented using a line chart or line plot or line graph. However, it becomes difficult to find the trend given the complexity of the line chart. This is where the trend line comes into the picture. Let's plot the line charts of batting average scores for individual batsmen listed in this post.
You may note that it becomes difficult to find out about the performance of batsmen across different seasons. Thus, if one would like to make a decision on who to put larger money on to acquire for the next season, IPL 2020 (currently going on), it would just get difficult.
This is where adding a trend line to all of the line charts will make difference.
How to Draw a Trend Line for Line Chart/Graph Using Python?
First and foremost, let's represent the IPL batting average scores data across different seasons from 2010-2019 shown in table 1 in form of numpy array.
Here is the Python code that is used to draw the trend lines for line charts/line graphs in order to assess the overall performance of these batsmen in the last 10 years in terms of increasing or decreasing scoring trends.
Here is how the trend line plot would look for all the players listed in this post.
The Python code that does the magic of drawing/adding the trend line to the line chart/line graph is the following. Pay attention to some of the following in the code given below:
- Two plots have been created — One is a Line chart/line plot/line graph, and the other is a trend line.
- Plotting code that represents line chart is ax[0, 1].plot(X, virat_kohli)
- Plotting code which represents a trend line is the following. Numpy ployfit method is used to fit the trend line which then returns the coefficients.
Here is the full Python code for adding a trend line to the line chart.
Line Chart and Trend Line Analysis
Given the trend line in figure 2, it can be concluded that MS Dhoni batting averages have been increasing as it has an upward trend line. That said, Virat Kohli's batting average also has an upward trend line but the slope is lesser than that of MS Dhoni. Thus, if it comes to making a decision on who to put your money on, I would bet my money on MS Dhoni.
Here is the summary of what you learned about adding a trend line to a line graph or a line chart using Python:
- Matplotlib can be used to draw line chart and trend line
- Matplotlib plot function is used to draw the line chart and trend line
- Numpy ployfit method is used to fit the polynomial which returns coefficients which are later used to draw the trend line.
Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.