INTRODUCTION TO SEABORN
Seaborn
Seaborn is a library that uses Matplotlib underneath to plot graphs. It is used to visualize random distributions. It is built on top of Matplotlib and provides beautiful default styles and color palettes to make statistical plots more attractive.
For Installation-
pip install seaborn
Creating Plots using Seaborn
import matplotlib.pyplot as plt
import seaborn as snssns.distplot([0, 1, 2, 3, 4, 5])
plt.show()
Seaborn | Style, And Color
The ways of styling themes are as follows:
- white
- dark
- white grid
- dark grid
- ticks
import
seaborn as sns
import
matplotlib.pyplot as plt
# load the tips dataset present by default in seaborn
tips =
sns.load_dataset('tips')
sns.set_style('white')
# make a countplot
sns.countplot(x ='sex', data =
tips)
Seaborn | Distribution Plots
- joinplot
- distplot
- pairplot
- rugplot
import
seaborn as sns
import
matplotlib.pyplot as plt %
matplotlib inline
# to ignore the warnings
from
warnings import
filterwarnings
# load the dataset
df =
sns.load_dataset('tips')
sns.set_style('whitegrid')
sns.distplot(df['total_bill'], kde =
False, color ='red', bins =
30)
sns.jointplot(x ='total_bill', y ='tip', data =
df)
sns.jointplot(x ='total_bill', y ='tip', data =
df, kind ='kde')
sns.pairplot(df, hue ="sex", palette ='coolwarm')
sns.rugplot(df['total_bill'])
Seaborn | Categorical Plots
Plots are basically used for visualizing the relationship between variables. Those variables can be either be completely numerical or a category like a group, class or division.
stripplot()
stripplot() is used when one of the variable under study is categorical. It represents the data in sorted order along any one of the axis.
Example
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset('iris')
sb.stripplot(x = "species", y = "petal_length", data = df)
plt.show()
Swarmplot()
Another option which can be used as an alternate to ‘Jitter’ is function swarmplot(). This function positions each point of scatter plot on the categorical axis and thereby avoids overlapping points −
Example
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset('iris')
sb.swarmplot(x = "species", y = "petal_length", data = df)
plt.show()
Matrix plots in Seaborn
Example 1: Heatmaps
Heatmap is a way to show some sort of matrix plot. To use a heatmap the data should be in a matrix form.
# import the necessary libraries
import seaborn as sns
import matplotlib.pyplot as plt % matplotlib inline# load the tips dataset
dataset = sns.load_dataset(‘tips’)# correlation between the different parameters
tc = dataset.corr()# plot a heatmap of the correlated data
sns.heatmap(tc)
Example 2: Cluster maps
Cluster maps use hierarchical clustering. It performs the clustering based on the similarity of the rows and columns.
# import the necessary libraries
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt % matplotlib inline# load the flights dataset
fd = sns.load_dataset(‘flights’)# make a dataframe of the data
df = pd.pivot_table(values =’passengers’, index =’month’,
columns =’year’, data = fd)# make a clustermap from the dataset
sns.clustermap(df, cmap =’plasma’)
Grid
FacetGrid object takes a dataframe as input and the names of the variables that will form the row, column, or hue dimensions of the grid.
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset('tips')
g = sb.FacetGrid(df, col = "time")
g.map(plt.hist, "tip")
plt.show()
Regression Plots
The regression plots in seaborn are primarily intended to add a visual guide that helps to emphasize patterns in a dataset during exploratory data analyses.
# import the library
import seaborn as sns# load the dataset
dataset = sns.load_dataset(‘tips’)
sns.set_style('whitegrid')
sns.lmplot(x ='total_bill', y ='tip', data =
dataset)