Python Seaborn Tutorial

Written by : Trilochan Tarai


Seaborn library

Seaborn is another python visualization library based on matplotlib. It provides high level interface for drawing attractive statistical graphics.


Functionalities

Allows comparison between multiple variables.

Supports multi-plot grids.

Available univariate and bivariate visualizations.

Availability of different color pallets.

Estimates and plots linear regression automatically.


Seaborn fixes two shortcomings of Matplotlib

Matplotlib settings are difficult to figure out. Seaborn comes with numerous customized themes and high level interfaces.

Matplotlib does not serve well when it comes to dealing with data frame , while Seaborn functions actually work on data frames.

Commands to install seaborn

pip install seaborn

Or

conda install seaborn

Installing dependencies

Pandas, numpy, scipy, matplotlib, and Statsmodels(SM):Statistics in python.


Python seaborn functions

Visualizing statistical relationships : Process of understanding relationships between variables of a dataset.

Plotting with categorical data : Main variable is further devided into discrete groups.

Visualizing the distribution of dataset : Understanding with data sets with context to being univariate or bivariate.

Let’s see in detail practically. Here we use Jupyter Notebook as our IDE.

import seaborn as sns
import matplotlib.pyplot as plt

#To get the current version of seaborn
print(sns.__version__)
Output: 0.11.0

df=sns.load_dataset("fmri")
df.head()

Output:

sns.lineplot(x="timepoint",y="signal",data=df)
plt.show()


sns.lineplot(x="timepoint",y="signal",data=df,hue="event")
plt.show()


sns.lineplot(x="timepoint",y="signal",data=df,hue="event",style="event",markers=True)
plt.show()


import pandas as pd
df1=pd.read_csv("iris.csv")
df1.head()

Output:

sns.scatterplot(x="sepal_length",y="petal_length",data=df1,hue="species",style="species")
plt.show()


sns.swarmplot(x="species",y="petal_length",data=df1)
plt.show()

C:\ProgramData\Anaconda3\lib\site-packages\seaborn\categorical.py:1296: User Warning: 14.0% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
warnings.warn(msg, UserWarning)

df2=sns.load_dataset("flights")
print(df2.head())

sns.relplot(x="passengers",y="month",data=df2)
plt.show()

df3=sns.load_dataset("flights")
sns.relplot(x="passengers",y="month",hue="year",data=df3)

Output:
<seaborn.axisgrid.FacetGrid at 0x258c0fb37c0>

b=sns.load_dataset("tips")
b.head()

Output:

sns.relplot(x="total_bill",y="tip",hue="smoker",data=b)

Output:
<seaborn.axisgrid.FacetGrid at 0x258c0d62910>

sns.relplot(x="time",y="tip",kind="line",data=b)

Output:
<seaborn.axisgrid.FacetGrid at 0x258c101d9d0>

sns.catplot(x="day",y="total_bill",data=b)
plt.show()

sns.catplot(x="day",y="total_bill",kind="boxen",data=b)

Output:
<seaborn.axisgrid.FacetGrid at 0x258c103df10>

sns.catplot(x="day",y="total_bill",kind="violin",data=b)

Output:
<seaborn.axisgrid.FacetGrid at 0x258c0e36a30>

df2=sns.load_dataset("tips")
sns.boxplot(x="day",y="total_bill",data=df2)
plt.show()

a=sns.load_dataset("iris")
b=sns.FacetGrid(a,col="species")
b.map(plt.hist,"sepal_length")
plt.show()

a=sns.load_dataset("flights")
b=sns.PairGrid(a)
b.map(plt.scatter)

Output:
<seaborn.axisgrid.PairGrid at 0x258c110fc70>

#ColorPalette
c=sns.color_palette()
sns.palplot(c)

sns.distplot([0, 1, 2, 3, 4, 5])
plt.show()
C:\ProgramData\Anaconda3\lib\site-packages\seaborn\distributions.py:2551: Fu tureWarning: `distplot` is a deprecated function and will be removed in a fu ture version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
warnings.warn(msg, FutureWarning)

sns.distplot([0, 1, 2, 3, 4, 5], hist=False)
plt.show()
C:\ProgramData\Anaconda3\lib\site-packages\seaborn\distributions.py:2551: Fu tureWarning: `distplot` is a deprecated function and will be removed in a fu ture version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `kdeplot` (an axes-level function for kernel density plots).
warnings.warn(msg, FutureWarning)

from numpy import random
x = random.binomial(n=10, p=0.5, size=10)
print(x)
Output:
[6 8 3 6 5 5 1 6 5 4]

sns.distplot(random.binomial(n=10, p=0.5, size=1000), hist=True)
plt.show()


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext