Visualize Linear Relationship using Python

A linear relationship is a statistical term that is nothing but the relationship between two variables. A linear relationship shows how well two variables x and y are related to each other. As a data science professional, you should know how to visualize a linear relationship as it will show the relationship between two numerical features of a dataset. So if you want to learn how to visualize a linear relationship, this article is for you. In this article, I will take you through a tutorial on how to visualize a linear relationship using Python.

Visualize a Linear Relationship using Python

When the value of variable increases or decreases with the increase or decrease in the value of another variable, then it is nothing but a linear relationship. When we visualize a linear relationship, it shows whether the relationship between the two features is linear or not.

You can use any data visualization library in Python to visualize a linear relationship. I prefer to use plotly as it provides interactive results. But as so many Python programmers use matplotlib for data visualization, I will show you how to visualize a linear relationship with Python using plotly and matplotlib.

Visualizing Linear Relationships using Python

So let’s import a dataset and all the necessary Python libraries for this task:

import pandas as pd
import numpy as np
import plotly.express as px
import matplotlib.pyplot as plt
import seaborn as sns

data = pd.read_csv("https://raw.githubusercontent.com/amankharwal/Website-data/master/Instagram.csv", encoding = 'latin1')
data = data.dropna()
print(data.head())
   Impressions  From Home  From Hashtags  From Explore  From Other  Saves  \
0       3920.0     2586.0         1028.0         619.0        56.0   98.0   
1       5394.0     2727.0         1838.0        1174.0        78.0  194.0   
2       4021.0     2085.0         1188.0           0.0       533.0   41.0   
3       4528.0     2700.0          621.0         932.0        73.0  172.0   
4       2518.0     1704.0          255.0         279.0        37.0   96.0   

   Comments  Shares  Likes  Profile Visits  Follows  \
0       9.0     5.0  162.0            35.0      2.0   
1       7.0    14.0  224.0            48.0     10.0   
2      11.0     1.0  131.0            62.0     12.0   
3      10.0     7.0  213.0            23.0      8.0   
4       5.0     4.0  123.0             8.0      0.0   

                                             Caption  \
0  Here are some of the most important data visua...   
1  Here are some of the best data science project...   
2  Learn how to train a machine learning model an...   
3  Here’s how you can write a Python program to d...   
4  Plotting annotations while visualizing your da...   

                                            Hashtags  
0  #finance #money #business #investing #investme...  
1  #healthcare #health #covid #data #datascience ...  
2  #data #datascience #dataanalysis #dataanalytic...  
3  #python #pythonprogramming #pythonprojects #py...  
4  #datavisualization #datascience #data #dataana... 

Here’s how to visualize linear relationships by using the plotly library in Python:

figure = px.scatter(data_frame = data, 
                    x="Impressions",
                    y="Likes", 
                    size="Likes", 
                    trendline="ols", 
                    title = "Relationship Between Likes and Impressions")
figure.show()
Visualize Linear Relationship using Python: plotly

To visualize linear relationships using matplotlib, you have to use seaborn.regplot method. So here’s how to plot linear relationships by using the matplotlib library in Python:

plt.figure(figsize=(10, 8))
plt.style.use('fivethirtyeight')
plt.title("Relationship Bewtween Likes & Impressions")
sns.regplot(x="Impressions", y="Likes", data=data)
plt.show()
Linear Relationship using Python: Matplotlib

Summary

So this is how to visualize linear relationships using the Python programming language. When the value of variable increases or decreases with the increase or decrease in the value of another variable, then it is nothing but a linear relationship. I hope you liked this article on visualizing linear relationships using Python. Feel free to ask valuable questions in the comments section below.

Default image
Aman Kharwal

Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder

Articles: 1289

2 Comments

Leave a Reply