Plotting a line plot (lmplot) in Python using the Seaborn library

This tutorial will teach you how to plot one of the most basic plots. To do this, we will be using Seaborn and matplotlib.

Importing the libraries

In this step, we import the required libraries i.e., Seaborn and matplotlib.pyplot using the code below:-

import matplotlib.pyplot as plt
import seaborn as sns

Alias is created using the ‘as‘ keyword. Using alias while using libraries is a good practice because it helps in writing clean and well-written code.

I will be using the load_dataset() function for obtaining the dataset. This function fetches the data from an online source; so make sure you are connected to the internet while running the code.

I will be using the ‘planets‘ dataset in this example. The ‘planets‘ dataset stores information about different planets. However, you can choose any dataset of your choice.

#Importing

import seaborn as sns
import matplotlib.pyplot as plt

#Loading the dataet
dataset = sns.load_dataset("planets")

sns.lmplot(x="distance",y="year",data=dataset)

plt.show()

First, we import the required libraries.

load_dataset() function: Load the ‘planets’ dataset into the variable, ‘dataset‘.

lmplot() function: Plot the graph from the loaded dataset. We take x-axis to be ‘distance‘ and y-axis to be ‘year‘.

Finally, we use the show() function of the matplotlib.pyplot module to show the graph.

 

Output

line plot (lmplot) in Python using the Seaborn library

Leave a Reply

Your email address will not be published. Required fields are marked *