How to Create a Seaborn Regplot in Python
We will learn to plot ‘regplot’ in Python from the Seaborn library. This is useful when you need graphs to analyze data. It is important as you can come to some important conclusions from the visualization.
seaborn is a built-in data visualization package built on top of matplotlib. Data visualization converts raw data into graphical representation which can be easily analyzed. There are several data visualization libraries in Python. A few examples are seaborn, pandas, and ggplot2. seaborn can take a large dataset and represent it in graphical form. It is used for plotting continuous or categorical data.
REGPLOT- regplot stands for regression plot and it shows the linear relationship between two continuous variables.
Generating a seaborn regplot example in Python
First, let us begin by importing the seaborn and matplotlib libraries. Here pyplot is the sub-module of the library matplotlib.
Make sure to have all the modules installed if you haven’t already.
import seaborn as sns import matplotlib.pyplot as plt
Subsequently, we load the data using the sns.load_dataset()
function. In this example, we are using the already existing dataset “flights“.
# loading dataset data_set = sns.load_dataset("flights")
Optionally, we can set the style of the plot using the sns.set()
function.
# Setting the style sns.set(style="whitegrid")
In the next step, sns.regplot()
is used to create a scatter plot using the regression line. In our example, the x-axis denotes years, while the y-axis represents the number of passengers boarding flights.
The documentation for the regplot function is provided here- seaborn-regplot-documentation
# draw regplot sns.regplot(x = "year", y = "passengers", data = data_set, ci=95, )
We then label the x-axis and y-axis for easy understanding and to improve comprehension of the represented data.
# lableing and giving a title plt.title("Regression plot of flights") plt.xlabel("Year") plt.ylabel("no. of Passengers")
Lastly, we visualize.
# representation plt.show()
Output-
This is the output for our example code. You can experiment with different data sets!
Leave a Reply