Change language

Seaborn | Regression plots

Seaborn — it is not only a visualization library, but also a provider of embedded datasets. Here we will be working with one such dataset in seaborn called "tips". The tips dataset contains information about people who likely had a meal at a restaurant and whether or not they tip. It also provides information about the gender of people, whether they smoke, day, time, etc.

Let’s take a look at the dataset first before we start with regression plots.

Load dataset

# import library

import seaborn as sns

 
# load dataset

dataset = sns .load_dataset ( ’tips’ )

  
# first five records of the dataset
dataset.head ()

Output

Now let’s start from the regression plots in the sea bay. 
Regression plots in seaborn can be easily implemented using the lmplot () function. lmplot () can be thought of as a function that basically creates a line plot of a model. lmplot () creates a very simple linear regression plot. It creates a scatter plot with a linear fit on top of it. 
Simple linear plot

sns.set_style ( ’ whitegrid ’ )

sns.lmplot (x = ’total_bill’ , y = ’tip’ , data = dataset)

Exit

explanation
The x and y parameters are specified to provide values ​​for the x and y axes. sns.set_style () is used to create a grid in the background instead of the default white background. The data parameter is used to specify the source of information for drawing graphs. 
Line chart with additional parameters

sns.set_style ( ’whitegrid’ )

sns.lmplot (x = ’ total_bill’ , y = ’tip’ , data = dataset , 

hue = ’sex’ , markers = [ ’o’ , ’ v’ ])

< p> Exit

explanation
To better analyze with these plots, we can specify the hue to have a categorical separation in our plot, and also use markers that are taken from matplotlib marker symbols. Since we have two separate categories, we need to specify a list of characters when specifying a bullet. 
Setting the size and color of the scene

sns.set_style ( ’whitegrid’ )

sns.lmplot (x = ’ total_bill’ , y = ’tip’ , data = dataset , hue = ’sex’

markers = [ ’o’ , ’ v’ ], scatter_kws = { ’ s’ : 100 }, 

palette = ’plasma’ )

Exit

explanation
In this example, what Seabron does is that he calls matplotlib parameters indirectly to affect scatter plots. We specify a parameter named scatter_kws. It should be noted that the scatter_kws parameter only resizes the scatter plots, not the regression lines. The regression lines remain intact. We also use the palette option to change the color of the graph. The rest of the things remain the same as described in the first example. 
Display multiple graphs

sns.lmplot (x = ’total_bill’ , y = ’tip’ , data = dataset, 

col = ’sex’ , row = ’ time ’ , hue = ’ smoker’ )

Exit

explanation
In the above code, we draw multiple plots, defining separation using rows and columns. Each row contains graphs of tips versus total score for different times specified in the dataset. Each column contains graphs of advice versus total score for different genders. Further separation is done by specifying a hue parameter based on whether the person smokes. 
Plot size and aspect ratio

sns.lmplot (x = ’total_bill’ , y = ’tip’ , data = dataset, col = ’sex’

row = ’time’ , hue = ’ smoker’ , aspect = 0.6

size = 4 , palette = ’coolwarm’ )

Exit

explanation
Suppose we have a large number of graphs in the output, we you need to set its size and aspect in order to better visualize it. 
aspect: scalar, an optional parameter specifies the aspect ratio of each facet, so "aspect * height" gives the width of each facet in inches.

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically