Skip to main content

Seaborn Complete Notes

What is Seaborn?

Seaborn is a Python data visualization library built on top of Matplotlib. It helps to:
  • Create beautiful statistical plots
  • Reduce plotting code
  • Improve plot styling automatically
  • Visualize complex datasets easily
Used in:
  • Data Science
  • Machine Learning
  • Data Analysis

Installing Seaborn

Installation


Importing Libraries

Explanation

  • sns → seaborn alias
  • pd → pandas
  • np → numpy

Loading Dataset in Seaborn

sns.get_dataset_names()

Shows available built-in datasets.

Output

List of datasets like:

sns.load_dataset()

Loads built-in dataset.

Output

Explanation

Loads penguins dataset into DataFrame.

value_counts()

Counts category occurrences.

Output

Explanation

Counts penguins species frequency.

Scatter Plot

sns.scatterplot()

Used to visualize relationship between two numerical variables.

Explanation

  • x → x-axis variable
  • y → y-axis variable
  • hue → color grouping

Output

Scatter plot grouped by island colors.

Styling in Seaborn

sns.set_style()

Changes plot background style.

Available Styles

  • white
  • dark
  • whitegrid
  • darkgrid
  • ticks

sns.despine()

Removes plot borders/spines.

Explanation

Removes left spine.

sns.set_context()

Controls scaling of plot elements.

Context Types


Palette

palette

Controls color theme.

Explanation

Uses Dark2 color palette.

Scatter Plot with Style and Alpha

Explanation

  • style → marker style changes
  • alpha → transparency

Strip Plot

sns.stripplot()

Shows distribution of categorical data.

Output

Categorical scatter-like plot.

dodge=True

Separates hue categories.

jitter=True

Adds random spacing.

Explanation

Avoids overlapping points.

Swarm Plot

sns.swarmplot()

Automatically prevents overlap.

Output

Bee swarm arrangement of points.

Histogram

sns.histplot()

Shows data distribution.

Explanation

  • multiple='stack' → stacked histogram

Regression Plot

sns.regplot()

Adds regression trend line.

Explanation

Shows linear relationship between variables.

Line Plot

sns.lineplot()

Shows continuous trends.

Explanation

  • Different colors → islands
  • Different styles → sex

Joint Plot

sns.jointplot()

Combines scatter plot + distributions.

Output

Central scatter plot with side histograms.

KDE Joint Plot

Explanation

Uses density estimation instead of scatter points.

Bar Plot

sns.barplot()

Shows average values by category.

Explanation

Compares mean body mass.

Count Plot

sns.countplot()

Counts categorical occurrences.

Output

Bar chart of species counts.

Box Plot

sns.boxplot()

Shows:
  • median
  • quartiles
  • outliers

Output

Distribution comparison across species.

Violin Plot

sns.violinplot()

Combines boxplot + density plot.

Explanation

Width shows density of values.

Split Violin Plot

Explanation

Male and female shown in one violin.

Inner Quartiles

Explanation

Shows quartile lines inside violin.

Swarm + Violin Combined

Explanation

Combines density + individual points.

KDE Plot

sns.kdeplot()

Smooth probability density curve.

Explanation

  • Smooth histogram alternative
  • fill=True fills area

Heatmap

sns.heatmap()

Displays matrix with colors.

Explanation

  • corr() → correlation matrix
  • annot=True → show values
  • vmin → minimum color scale

Output

Correlation heatmap.

Rug Plot

sns.rugplot()

Shows individual data points as ticks.

Output

Small tick marks along axis.

Pair Plot

sns.pairplot()

Creates pairwise plots automatically.

Output

Grid of scatter plots and histograms.

Pair Plot with Histogram

Explanation

Diagonal uses histograms instead of KDE.

Pair Grid

sns.PairGrid()

Custom subplot grid.

Explanation

  • map_upper() → upper triangle plots
  • map_lower() → lower triangle plots
  • map_diag() → diagonal plots

Output

Fully customized pairwise visualization grid.

Seaborn Plot Summary


Important Seaborn Functions


Seaborn Helps

Seaborn helps to:
  • Create attractive statistical plots
  • Analyze distributions
  • Detect patterns
  • Understand correlations
  • Visualize categorical and numerical data easily
Advantages:
  • Less code
  • Better styling
  • Easy integration with Pandas
  • Built on Matplotlib