Day 14
Day 14
October 9, 2023
1 Python Seaborn
Seaborn is a library mostly used for statistical plotting in Python.
It is built on top of Matplotlib and provides beautiful default styles and color palettes to make
statistical plots more attractive.
[1]: import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
1
2 Using Seaborn with Matplotlib
[3]: import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset("iris")
2
3 Setting the xlim and ylim
[8]: import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset("iris")
plt.xlim(5)
plt.show()
3
4 Customizing Seaborn Plots
• Changing Figure Aesthetic
• Removal of Spines
• Changing the figure Size
• Scaling the plots
• Setting the Style Temporarily
• Changing Figure Aesthetic
set_style() method is used to set the aesthetic of the plot.
It means it affects things like the color of the axes, whether the grid is active or not, or other
aesthetic elements.
There are five themes available in Seaborn.
darkgrid, whitegrid, dark, white, ticks
[11]: import seaborn as sns
import matplotlib.pyplot as plt
4
data = sns.load_dataset("iris")
• Removal of Spines
Spines are the lines noting the data boundaries and connecting the axis tick marks.
It can be removed using the despine() method.
data = sns.load_dataset("iris")
sns.lineplot(x="sepal_length", y="sepal_width", data=data)
5
sns.despine()
plt.show()
data = sns.load_dataset("iris")
plt.figure(figsize = (3, 4))
sns.lineplot(x="sepal_length", y="sepal_width", data=data)
plt.show()
6
• Scaling the plots
It can be done using the set_context() method. It allows us to override default parameters.
The base context is “notebook”, “paper”, “talk”, and “poster”. font_scale sets the font size.
[15]: import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset("iris")
sns.lineplot(x="sepal_length", y="sepal_width", data=data)
plt.show()
7
• Setting the Style Temporarily
axes_style() method is used to set the style temporarily.
It is used along with the with statement.
[16]: import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset("iris")
def plot():
sns.lineplot(x="sepal_length", y="sepal_width", data=data)
with sns.axes_style('darkgrid'):
plt.subplot(211)
plot()
plt.subplot(212)
plot()
8
5 Color Palette
• Colormaps are used to visualize plots effectively and easily.
• One might use different sorts of colormaps for different kinds of plots.
• color_palette() method is used to give colors to the plot.
• Another function palplot() is used to deal with the color palettes and plots the color palette
as a horizontal array.
Diverging Color Palette
Sequential Color Palette
Setting the default Color Palette
[18]: import seaborn as sns
import matplotlib.pyplot as plt
palette = sns.color_palette()
sns.palplot(palette)
plt.show()
9
Diverging Color Palette
[20]: import seaborn as sns
import matplotlib.pyplot as plt
palette = sns.color_palette('PiYG', 9)
sns.palplot(palette)
plt.show()
sns.palplot(palette)
plt.show()
10
[22]: import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset("iris")
def plot():
sns.lineplot(x="sepal_length", y="sepal_width", data=data)
sns.set_palette('vlag')
plt.subplot(211)
plot()
sns.set_palette('Accent')
plt.subplot(212)
plot()
plt.show()
11
6 Multiple plots with Seaborn
Using Matplotlib
• Using add_axes() method
• Using subplot() method
• Using subplot2grid() method
Using Seaborn
• Using FacetGrid() method
• Using PairGrid() method
• Using add_axes() method
data = sns.load_dataset("iris")
def graph():
sns.lineplot(x="sepal_length", y="sepal_width", data=data)
plt.show()
12
• Using subplot() method
data = sns.load_dataset("iris")
def graph():
sns.lineplot(x="sepal_length", y="sepal_width", data=data)
plt.subplot(121)
graph()
plt.subplot(122)
graph()
plt.show()
13
• Using subplot2grid() method
data = sns.load_dataset("iris")
def graph():
sns.lineplot(x="sepal_length", y="sepal_width", data=data)
14
• Using FacetGrid() method
FacetGrid class helps in visualizing distribution of one variable as well as the relationship between
multiple variables separately within subsets of your dataset using multiple panels.
[27]: import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset("iris")
plt.show()
15
• Using PairGrid() method
Subplot grid for plotting pairwise relationships in a dataset.
This class maps each variable in a dataset onto a column and row in a grid of multiple axes.
Different axes-level plotting functions can be used to draw bivariate plots in the upper and lower
triangles, and the marginal distribution of each variable can be shown on the diagonal.
[28]: import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset("flights")
plot = sns.PairGrid(data)
plot.map(plt.plot)
plt.show()
16
[ ]:
17