How to graphically represent the relation of two variables

1 min

One of the best ways to check the relation between two variables is to make a scatter plot with those two variables.

Using Matplotlib, plotting a scatter plot is quite straightforward.

Here is the example

# For our DataFrame
import pandas as pd

# In order to plot
import matplotlib.pyplot as plt

# We get our sample data from github
df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')

# We setup our figure
fig, axes = plt.subplots(1,1, figsize=(5,5))

# We plot the scatter
axes.scatter(data=df,
            x="petal_length",
            y="sepal_length")
            
# We plot the grid            
axes.grid()

# We add better labels
axes.set_xlabel("Petal Length")
axes.set_ylabel("Sepal Length")

# We set the title
axes.set_title("Graphical relation between petal length and sepal length")

# We tidy things up
plt.tight_layout()

# We plot our data
plt.show()
How to graphically represent the relation between two variables

Here is the result

Here is how to graphically represent the relation of two variables

You can check for correlation

One good thing about graphical representation is that you will be able to tell whether those two variables correlate or not.

Here are the different possible correlation types

The different correlation types, source: https://byjus.com/

More on plots

If you want to know more about how to add labels, plot different types of plots, etc... check out the other articles I wrote on the topic, just here :

Matplotlib - The Python You Need
We gathered the only Python essentials that you will probably ever need.