0% found this document useful (0 votes)
16 views17 pages

Day 14

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)
16 views17 pages

Day 14

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/ 17

seaborn-introduction

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

[2]: import seaborn as sns


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

[2]: <Axes: xlabel='sepal_length', ylabel='sepal_width'>

1
2 Using Seaborn with Matplotlib
[3]: 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.title('Title using Matplotlib Function')


plt.show()

2
3 Setting the xlim and ylim
[8]: 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.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")

sns.lineplot(x="sepal_length", y="sepal_width", data=data)

# changing the theme to dark


sns.set_style("dark")
plt.show()

• 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.

[12]: import seaborn as sns


import matplotlib.pyplot as plt

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

# Removing the spines

5
sns.despine()
plt.show()

• Changing the figure Size


The figure size can be changed using the figure() method of Matplotlib.
figure() method creates a new figure of the specified size passed in the figsize parameter.

[14]: import seaborn as sns


import matplotlib.pyplot as plt

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)

# Setting the scale of the plot


sns.set_context("paper")

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()

Sequential Color Palette


[21]: import seaborn as sns
import matplotlib.pyplot as plt

palette = sns.color_palette('Greens', 11)

sns.palplot(palette)

plt.show()

Setting the default Color Palette


set_palette() method is used to set the default color palette for all the plots.
The arguments for both color_palette() and set_palette() is same.
set_palette() changes the default matplotlib parameters.

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

[24]: import seaborn as sns


import matplotlib.pyplot as plt

data = sns.load_dataset("iris")

def graph():
sns.lineplot(x="sepal_length", y="sepal_width", data=data)

fig = plt.figure(figsize =(5, 4))

# Creating first axes for the figure


ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])

# plotting the graph


graph()

# Creating second axes for the figure


ax2 = fig.add_axes([0.5, 0.5, 0.3, 0.3])

# plotting the graph


graph()

plt.show()

12
• Using subplot() method

[25]: import seaborn as sns


import matplotlib.pyplot as plt

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

[26]: import seaborn as sns


import matplotlib.pyplot as plt

data = sns.load_dataset("iris")

def graph():
sns.lineplot(x="sepal_length", y="sepal_width", data=data)

# adding the subplots


axes1 = plt.subplot2grid ( (7, 1), (0, 0), rowspan = 2, colspan = 1)
graph()

axes2 = plt.subplot2grid ( (7, 1), (2, 0), rowspan = 2, colspan = 1)


graph()

axes3 = plt.subplot2grid ( (7, 1), (4, 0), rowspan = 2, colspan = 1)


graph()

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")

plot = sns.FacetGrid(data, col="species")


plot.map(plt.plot, "sepal_width")

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

You might also like