Python

How to Use Matplotlib Trend Line

A trend line is a line created at or just below pivotal peaks or low points to illustrate the current valuation direction. For technical analysts, a trend line is an effective component. Analysts can identify trend line representations to determine trend direction and subsequent bouncing tendencies. During the time interval under consideration, analysts choose any two points on a graph and connect them to form a line graph.

When we accept a smaller point in a trend, it operates as a support line. And when we select higher points, it serves as a resistance line. As a result, it will be used to figure out these two spots on a graph. Let’s discuss the method of adding a trend line to the graph by the use of Matplotlib in Python.

Use Matplotlib to Create a Trend Line in a Scatter Graph:

We will utilize the polyfit() and poly1d() functions to acquire the trend line values in Matplotlib to construct a trend line in a scatter graph. The following code is a sketch of inserting a trend line into a scatter graph with groups:

import matplotlib.pyplot as plt

import numpy as np

plt.rcParams["figure.figsize"] = [8.50, 2.50]

plt.rcParams["figure.autolayout"] = True

a = np.random.rand(200)

b = np.random.rand(200)

fig, ax = plt.subplots()

_ = ax.scatter(a, b, c=a, cmap='rainbow')

d = np.polyfit(a, b, 1)

p = np.poly1d(d)

plt.plot(a, p(a), "m:*")

plt.show()

Here, we include the NumPy and matplotlib.pyplot libraries. Matplotlib.pyplot is a graphing package used to draw visualizations in Python. We may utilize it on applications and different graphical user interfaces. The NumPy library provides a large number of numeric data types that we can utilize to declare arrays.

In the next line, we adjust the size of the figure by calling the function plt.rcParams(). The figure.figsize is passed as a parameter to this function. We set the value “true” to adjust the spacing between the subplots. Now, we take two variables. And then, we make data sets of the x-axis and y-axis. The data points of the x-axis are stored in the “a” variable, and the data points of the y-axis are stored in the “b” variable. This can be completed by the use of the NumPy library. We make a new object of the figure. And the plot is created by applying the plt.subplots() function.

In addition, the scatter() function is applied. This function comprises four parameters. The color scheme of the graph is also specified by providing “cmap” as an argument for this function. Now, we plot data sets of the x-axis and y-axis. Here, we adjust the trend line of data sets using polyfit() and poly1d() functions. We utilize the plot() function to draw the trend line.

Here, we set the line style, color of the line, and marker of the trend line. In the end, we will be showing the following graph with the help of the plt.show() function:

Add Graphing Connectors:

Whenever we observe a scatter graph, we may want to identify the overall direction the dataset is heading in some situations. Although if we obtain a clear representation of the subgroups, the overall direction of the information available will not be evident. We insert a trend line to the outcome in this scenario. In this step, we observe how we add connectors to the graph.

import matplotlib.pyplot as plt

import numpy as np

import pylab as plb

a1 = 25 * np.random.rand(60)

a2 = 25 * np.random.rand(60) + 25

a3 = 20 * np.random.rand(20)

x = np.concatenate((a1, a2, a3))

b1 = 25 * np.random.rand(50)

b2 = 25 * np.random.rand(60) + 25

b3 = 20 * np.random.rand(20)

y = np.concatenate((a1, b2, b3))

plt.scatter(x, y, s=[200], marker='o')

z = np.polyfit(x, y, 2)

p = np.poly1d(z)

plb.plot(x, p(x), 'r-.')

plt.show()



At the start of the program, we import three libraries. These include NumPy, matplotlib.pyplot, and matplotlib.pylab. Matplotlib is a Python library that allows users to create dynamic and innovative graphic representations. Matplotlib generates high-quality graphs with the ability to change the visual elements and style.

The pylab package integrates the pyplot and the NumPy libraries into a particular source domain. Now, we take three variables for creating the data sets of the x-axis, which is accomplished by using the random() function of the NumPy library.

First, we stored the data points in the “a1” variable. And then, data is stored in “a2” and “a3” variables, respectively. Now, we create a new variable that stores all the data sets of the x-axis. It utilizes the concatenate() function of the NumPy library.

Similarly, we store data sets of the y-axis in the other three variables. We create the data sets of the y-axis by using the random() method. Further, we concatenate all these data sets in a new variable. Here, we will draw a scatter graph, so we employ the plt.scatter() method. This function holds four different parameters. We pass data sets of the x-axis and y-axis in this function. And we also specify the symbol of the marker which we want to be drawn in a scatter graph by using the “marker” parameter.

We provide the data to the NumPy polyfit() method, which provides an array of parameters, “p”. Here, it optimizes the finite difference error. Hence, a trend line could be created. Regression analysis is a statistical technique for determining a line that is included within the range of the instructive variable x. And it represents the correlation among two variables, in the case of the x-axis and y-axis. The intensity of the polynomial congruence is indicated by the third polyfit() argument.

Polyfit() returns an array, passed to the poly1d() function, and it determines the original y-axis data sets. We draw a trend line on the scatter graph by utilizing the plot() function. We can adjust the style and color of the trend line. Last, we employ the plt.show() method to represent the graph.

Conclusion:

In this article, we talked about Matplotlib trend lines with various examples. We also discussed how to create a trend line in a scatter graph by the use of polyfit() and poly1d() functions. In the end, we illustrate correlations in the groups of data. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content