# 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.

· Big Data Zone · Tutorial
Save
3.46K Views

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.

Java

x
17

1
`import numpy as np`
2
`#`
3
`# Chris Gayle`
4
`#`
5
`chris_gayle = np.array([32.44, 67.55, 61.08, 59.00, 21.77, 40.91, 22.70, 22.22, 40.88, 40.83])`
6
`#`
7
`# Rohit Sharma`
8
`#`
9
`rohit_sharma = np.array([28.85, 33.81, 30.92, 38.42, 30.00, 34.42, 44.45, 23.78, 23.83, 28.92])`
10
`#`
11
`# MS Dhoni`
12
`#`
13
`ms_dhoni = np.array([31.88, 43.55, 29.83, 41.90, 74.20, 31.00, 40.57, 26.36, 75.83, 83.20])`
14
`#`
15
`# Virat Kohli`
16
`#`
17
`virat_kohli = np.array([27.90, 46.41, 28.00, 45.28, 27.61, 45.90, 81.08, 30.80, 48.18, 33.14])`

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.

Java

`xxxxxxxxxx`
1
44

1
`import matplotlib.pyplot as plt`
2
`import numpy as np`
3
`#`
4
`# Number of years represented as Numpy Array`
5
`#`
6
`X = np.array([2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019])`
7
`fig, ax = plt.subplots(2, 2, figsize=(9, 7), sharex=True, sharey=True)`
8
`#`
9
`# Chris Gayle`
10
`#`
11
`z = np.polyfit(X, chris_gayle, 1)`
12
`p = np.poly1d(z)`
13
`ax[1, 0].plot(X,p(X),"r--")`
14
`ax[1, 0].plot(X, chris_gayle)`
15
`ax[1, 0].set_title('Chris Gayle', fontsize=14)`
16
`#`
17
`# Rohit Sharma`
18
`#`
19
`z = np.polyfit(X, rohit_sharma, 1)`
20
`p = np.poly1d(z)`
21
`ax[0, 0].plot(X,p(X),"r--")`
22
`ax[0, 0].plot(X, rohit_sharma )`
23
`ax[0, 0].set_title('Rohit Sharma', fontsize=14)`
24
`#`
25
`# MS Dhoni`
26
`#`
27
`z = np.polyfit(X, ms_dhoni, 1)`
28
`p = np.poly1d(z)`
29
`ax[1, 1].plot(X,p(X),"r--")`
30
`ax[1, 1].plot(X, ms_dhoni)`
31
`ax[1, 1].set_title('MS Dhoni', fontsize=14)`
32
`#`
33
`# Virat Kohli`
34
`#`
35
`z = np.polyfit(X, virat_kohli, 1)`
36
`p = np.poly1d(z)`
37
`ax[0, 1].plot(X,p(X),"r--")`
38
`ax[0, 1].plot(X, virat_kohli)`
39
`ax[0, 1].set_title('Virat Kohli', fontsize=14)`
40
`#`
41
`# Draw the plot`
42
`#`
43
`fig.text(0.5, 0.04, 'Years', ha='center', fontsize=18)`
44
`fig.text(0.04, 0.5, 'Average Scores in IPL Seasons', va='center', rotation='vertical', fontsize=18)`

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.
Java

`xxxxxxxxxx`
1

1
`z = np.polyfit(X, virat_kohli, 1) // Polynomial fit`
2
`p = np.poly1d(z)`
3
`ax[0, 1].plot(X,p(X),"r--")`

Here is the full Python code for adding a trend line to the line chart.

Java

`xxxxxxxxxx`
1

1
`virat_kohli = np.array([27.90, 46.41, 28.00, 45.28, 27.61, 45.90, 81.08, 30.80, 48.18, 33.14])`
2

3
`z = np.polyfit(X, virat_kohli, 1) // Polynomial fit`
4
`p = np.poly1d(z)`
5

6
`ax[0, 1].plot(X,p(X),"r--")`
7
`ax[0, 1].plot(X, virat_kohli)`
8

9
`ax[0, 1].set_title('Virat Kohli', fontsize=14)`

### 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.

## Conclusions

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.
Topics:
big data, tutorial, python, trend line, line chart

Published at DZone with permission of Ajitesh Kumar, DZone MVB.

Opinions expressed by DZone contributors are their own.