0% found this document useful (0 votes)
14 views20 pages

Day 15

Uploaded by

Vatsal Upadhyay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views20 pages

Day 15

Uploaded by

Vatsal Upadhyay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

seaborn-plots

October 11, 2023

1 Seaborn Different Types of plots

2 Creating Different Types of Plots


Relational Plot - Scatter Plot - Line Plot
Categorical Plots:
• Bar Plot
• Count Plot
• Box Plot
• Violinplot
• Stripplot
• Swarmplot
• Factorplot
Distribution Plot:
• Histogram
• Distplot
• Jointplot
• Pairplot
• Rugplot
• KDE Plot
Regression Plots: - lmplot - Regplot - Matrix Plots - Heatmap - Clustermap

3 Relational Plot
• Relplot()
This function provides us the access to some other different axes-level functions which shows the
relationships between two variables with semantic mappings of subsets.
It is plotted using the relplot() method.

[10]: import seaborn as sns


import matplotlib.pyplot as plt

data = sns.load_dataset("iris")
sns.relplot(x='sepal_width', y='species', data=data)

1
plt.show()

• Scatter Plot
The scatter plot is a mainstay of statistical visualization.
It depicts the joint distribution of two variables using a cloud of points,
It is plotted using the scatterplot() method.

[12]: import seaborn as sns


import matplotlib.pyplot as plt

data = sns.load_dataset("iris")
sns.scatterplot(x='sepal_length', y='sepal_width', data=data)
plt.show()

2
• Line Plot
It is plotted using the lineplot() method.

[14]: import seaborn as sns


import matplotlib.pyplot as plt

data = sns.load_dataset("iris")
sns.lineplot(x='sepal_length', y='species', data=data)

plt.show()

3
4 Categorical Plots
Categorical Plots are used where we have to visualize relationship between two numerical values.
• Bar Plot
• Count Plot
• Box Plot
• Violinplot
• Stripplot
• Swarmplot
• Factorplot
• Bar Plot
A barplot is basically used to aggregate the categorical data according to some methods and by
default its the mean.
It can also be understood as a visualization of the group by action.
To use this plot we choose a categorical column for the x axis and a numerical column for the y
axis
It can be created using the barplot() method.

4
[15]: import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset("iris")

sns.barplot(x='species', y='sepal_length', data=data)


plt.show()

• Count Plot
A countplot basically counts the categories and returns a count of their occurrences.
It is one of the most simple plots provided by the seaborn library.
It can be created using the countplot() method.

[16]: import seaborn as sns


import matplotlib.pyplot as plt

data = sns.load_dataset("iris")
sns.countplot(x='species', data=data)
plt.show()

5
Box Plot
A boxplot is sometimes known as the box and whisker plot.
It shows the distribution of the quantitative data that represents the comparisons between variables.
the dots indicating the presence of outliers.
It is created using the boxplot() method.

[17]: import seaborn as sns


import matplotlib.pyplot as plt

data = sns.load_dataset("iris")
sns.boxplot(x='species', y='sepal_width', data=data)
plt.show()

6
• Violinplot
It is similar to the boxplot except that it provides a higher, more advanced visualization and uses
the kernel density estimation to give a better description about the data distribution.
It is created using the violinplot() method.

[18]: import seaborn as sns


import matplotlib.pyplot as plt

data = sns.load_dataset("iris")
sns.violinplot(x='species', y='sepal_width', data=data)
plt.show()

7
• Stripplot
It basically creates a scatter plot based on the category.
It is created using the stripplot() method.

[19]: import seaborn as sns


import matplotlib.pyplot as plt

data = sns.load_dataset("iris")
sns.stripplot(x='species', y='sepal_width', data=data)
plt.show()

8
• Swarmplot
Swarmplot is very similar to the stripplot except the fact that the points are adjusted so that they
do not overlap.
it is plotted using the swarmplot() method.

[20]: import seaborn as sns


import matplotlib.pyplot as plt

data = sns.load_dataset("iris")
sns.swarmplot(x='species', y='sepal_width', data=data)
plt.show()

9
• Factorplot
Factorplot is the most general of all these plots and provides a parameter called kind to choose the
kind of plot we want thus saving us from the trouble of writing these plots separately.
The kind parameter can be bar, violin, swarm etc. It is plotted using the factorplot() method.

5 Distribution Plot:
• Histogram
• Distplot
• Jointplot
• Pairplot
• Rugplot
• KDE Plot
• Histogram
A histogram is basically used to represent data provided in a form of some groups.
It is accurate method for the graphical representation of numerical data distribution.
It can be plotted using the histplot() function.

10
[27]: import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset("iris")
sns.histplot(x='species', y='sepal_width', data=data)
plt.show()

• Distplot
Distplot is used basically for univariant set of observations and visualizes it through a histogram
i.e. only one observation and hence we choose one particular column of the dataset.
It is potted using the distplot() method.

[32]: import seaborn as sns


import matplotlib.pyplot as plt

data = sns.load_dataset("iris")
sns.distplot(data['sepal_width'])
plt.show()

C:\Users\ABDULLAH KHAN\AppData\Local\Temp\ipykernel_4944\4199604369.py:5:

11
UserWarning:

`distplot` is a deprecated function and will be removed in seaborn v0.14.0.

Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).

For a guide to updating your code to use the new functions, please see
https://gist.github.com/mwaskom/de44147ed2974457ad6372750bbe5751

sns.distplot(data['sepal_width'])

• Jointplot
Jointplot is used to draw a plot of two variables with bivariate and univariate graphs.
It basically combines two different plots.
It is plotted using the jointplot() method.

[33]: import seaborn as sns


import matplotlib.pyplot as plt

12
data = sns.load_dataset("iris")

sns.jointplot(x='species', y='sepal_width', data=data)


plt.show()

• Pairplot
Pairplot represents pairwise relation across the entire dataframe and supports an additional argu-
ment called hue for categorical separation.
What it does basically is create a jointplot between every possible numerical column and takes a
while if the dataframe is really huge.
It is plotted using the pairplot() method.

13
[34]: import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset("iris")

sns.pairplot(data=data, hue='species')
plt.show()

• Rugplot
Rugplot plots datapoints in an array as sticks on an axis.
Just like a distplot it takes a single column.
Instead of drawing a histogram it creates dashes all across the plot.
If you compare it with the joinplot you can see that what a jointplot does is that it counts the
dashes and shows it as bins.

14
It is plotted using the rugplot() method.

[35]: import seaborn as sns


import matplotlib.pyplot as plt

data = sns.load_dataset("iris")

sns.rugplot(data=data)
plt.show()

• KDE Plot
KDE Plot described as Kernel Density Estimate.
it is used for visualizing the Probability Density of a continuous variable.
It depicts the probability density at different values in a continuous variable.
We can also plot a single graph for multiple samples which helps in more efficient data visualization.
[36]: import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset("iris")

15
sns.kdeplot(x='sepal_length', y='sepal_width', data=data)
plt.show()

6 Regression Plots:
Regression plots as the name suggests creates a regression line between two parameters and helps
to visualize their linear relationships.
• lmplot
• Regplot
• Matrix Plots
• Heatmap
• Clustermap
• implot
lmplot() method can be understood as a function that basically creates a linear model plot.
It creates a scatter plot with a linear fit on top of it.

16
[39]: import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset("tips")
sns.lmplot(x='total_bill', y='tip', data=data)
plt.show()

• Regplot
regplot() method is also similar to lmplot which creates linear regression model.
Note: The difference between both the function is that regplot accepts the x, y variables in different
format including NumPy arrays, Pandas objects, whereas, the lmplot only accepts the value as
strings.
[40]: import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset("tips")

17
sns.regplot(x='total_bill', y='tip', data=data)
plt.show()

Matrix Plots
A matrix plot means plotting matrix data where color coded diagrams shows rows data, column
data and values.
It can shown using the heatmap and clustermap.
• Heatmap
Heatmap is defined as a graphical representation of data using colors to visualize the value of the
matrix.
In this, to represent more common values or higher activities brighter colors basically reddish colors
are used and to represent less common or activity values, darker colors are preferred.
it can be plotted using the heatmap() function.

[41]: import seaborn as sns


import matplotlib.pyplot as plt

data = sns.load_dataset("tips")

18
sns.heatmap(data.corr())
plt.show()

C:\Users\ABDULLAH KHAN\AppData\Local\Temp\ipykernel_4944\2866499940.py:6:
FutureWarning: The default value of numeric_only in DataFrame.corr is
deprecated. In a future version, it will default to False. Select only valid
columns or specify the value of numeric_only to silence this warning.
sns.heatmap(data.corr())

• Clustermap
The clustermap() function of seaborn plots the hierarchically-clustered heatmap of the given matrix
dataset.
Clustering simply means grouping data based on relationship among the variables in the data.
[44]: import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset("tips")
plt.figure(figsize=(5,5))
sns.clustermap(data.corr())
plt.show()

19
C:\Users\ABDULLAH KHAN\AppData\Local\Temp\ipykernel_4944\2539552102.py:6:
FutureWarning: The default value of numeric_only in DataFrame.corr is
deprecated. In a future version, it will default to False. Select only valid
columns or specify the value of numeric_only to silence this warning.
sns.clustermap(data.corr())
<Figure size 500x500 with 0 Axes>

20

You might also like