> ## Documentation Index
> Fetch the complete documentation index at: https://ai.tharung.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Seaborn

# 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

```bash theme={null}
pip install seaborn
```

***

# Importing Libraries

```python theme={null}
import seaborn as sns
import pandas as pd
import numpy as np
```

### Explanation

* `sns` → seaborn alias
* `pd` → pandas
* `np` → numpy

***

# Loading Dataset in Seaborn

# `sns.get_dataset_names()`

Shows available built-in datasets.

```python theme={null}
sns.get_dataset_names()
```

### Output

List of datasets like:

```python theme={null}
['penguins', 'tips', 'iris', 'diamonds', ...]
```

***

# `sns.load_dataset()`

Loads built-in dataset.

```python theme={null}
penguins = sns.load_dataset('penguins')

penguins.head()
```

### Output

```python theme={null}
species island bill_length_mm ...
```

### Explanation

Loads penguins dataset into DataFrame.

***

# `value_counts()`

Counts category occurrences.

```python theme={null}
penguins['species'].value_counts()
```

### Output

```python theme={null}
Adelie       152
Gentoo       124
Chinstrap     68
```

### Explanation

Counts penguins species frequency.

***

# Scatter Plot

# `sns.scatterplot()`

Used to visualize relationship between two numerical variables.

```python theme={null}
sns.scatterplot(
    data=penguins,
    x='flipper_length_mm',
    y='body_mass_g',
    hue='island'
)
```

### Explanation

* `x` → x-axis variable
* `y` → y-axis variable
* `hue` → color grouping

### Output

Scatter plot grouped by island colors.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/scatter.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=affa15bd321ec1bc3aede84c8226002d" alt="Scatter" width="580" height="433" data-path="images/scatter.png" />
</Frame>

***

# Styling in Seaborn

# `sns.set_style()`

Changes plot background style.

```python theme={null}
sns.set_style('whitegrid')
```

### Available Styles

* white
* dark
* whitegrid
* darkgrid
* ticks

***

# `sns.despine()`

Removes plot borders/spines.

```python theme={null}
sns.despine(left=True)
```

### Explanation

Removes left spine.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/despine.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=c38a0ddcc1fe83ef5470b004c84604a4" alt="Despine" width="580" height="433" data-path="images/despine.png" />
</Frame>

***

# `sns.set_context()`

Controls scaling of plot elements.

```python theme={null}
sns.set_context('talk')
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/talk.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=3ca47d74a8e66ab1c33d929edcd2fdd7" alt="Talk" width="622" height="460" data-path="images/talk.png" />
</Frame>

### Context Types

| Context  | Usage          |
| :------- | :------------- |
| paper    | Small plots    |
| notebook | Default        |
| talk     | Presentation   |
| poster   | Large displays |

***

# Palette

# `palette`

Controls color theme.

```python theme={null}
sns.scatterplot(
    data=penguins,
    x='flipper_length_mm',
    y='body_mass_g',
    hue='island',
    palette='Dark2'
)
```

### Explanation

Uses Dark2 color palette.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/palette.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=0509014b1240ceff2101da6267d8ded7" alt="Palette" width="556" height="415" data-path="images/palette.png" />
</Frame>

***

# Scatter Plot with Style and Alpha

```python theme={null}
sns.scatterplot(
    data=penguins,
    x='species',
    y='body_mass_g',
    hue='island',
    style='sex',
    alpha=0.5
)
```

### Explanation

* `style` → marker style changes
* `alpha` → transparency
  <Frame>
    <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/styleandaplpha.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=85daa6331b53b7e8d250c5b8b2fd0b8c" alt="Styleandaplpha" width="531" height="393" data-path="images/styleandaplpha.png" />
  </Frame>

***

# Strip Plot

# `sns.stripplot()`

Shows distribution of categorical data.

```python theme={null}
sns.stripplot(
    data=penguins,
    x='species',
    y='body_mass_g',
    hue='island'
)
```

### Output

Categorical scatter-like plot.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/stripplot.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=8741c10c9ce199a7ff7fd07345c86799" alt="Stripplot" width="591" height="441" data-path="images/stripplot.png" />
</Frame>

***

# `dodge=True`

Separates hue categories.

```python theme={null}
sns.stripplot(
    data=penguins,
    x='species',
    y='body_mass_g',
    hue='island',
    dodge=True
)
```

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/dodge.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=a418fa4a2cb734e7f28364685dc6b2a6" alt="Dodge" width="591" height="441" data-path="images/dodge.png" />
</Frame>

***

# `jitter=True`

Adds random spacing.

```python theme={null}
sns.stripplot(
    data=penguins,
    x='species',
    y='body_mass_g',
    hue='island',
    dodge=True,
    jitter=True
)
```

### Explanation

Avoids overlapping points.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/jitter.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=c909094b2365104a44020c16bc417d79" alt="Jitter" width="591" height="441" data-path="images/jitter.png" />
</Frame>

***

# Swarm Plot

# `sns.swarmplot()`

Automatically prevents overlap.

```python theme={null}
sns.swarmplot(
    data=penguins,
    x='species',
    y='body_mass_g',
    hue='island'
)
```

### Output

Bee swarm arrangement of points.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/swarmplot.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=cf98218e43f7d6325ab192d5160071f8" alt="Swarmplot" width="591" height="441" data-path="images/swarmplot.png" />
</Frame>

***

# Histogram

# `sns.histplot()`

Shows data distribution.

```python theme={null}
sns.histplot(
    data=penguins,
    x='body_mass_g',
    hue='sex',
    multiple='stack'
)
```

### Explanation

* `multiple='stack'` → stacked histogram
  <Frame>
    <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/histogram1.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=bf520beaaab16b5f803eae745b04dd92" alt="Histogram1" width="572" height="441" data-path="images/histogram1.png" />
  </Frame>

***

# Regression Plot

# `sns.regplot()`

Adds regression trend line.

```python theme={null}
sns.regplot(
    data=penguins,
    x='body_mass_g',
    y='flipper_length_mm',
    color='green'
)
```

### Explanation

Shows linear relationship between variables.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/regressionplo.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=a2b4db072809fa29c181c106ad54e0e9" alt="Regressionplo" width="582" height="441" data-path="images/regressionplo.png" />
</Frame>

***

# Line Plot

# `sns.lineplot()`

Shows continuous trends.

```python theme={null}
sns.lineplot(
    data=penguins,
    x='body_mass_g',
    y='flipper_length_mm',
    hue='island',
    style='sex'
)
```

### Explanation

* Different colors → islands
* Different styles → sex
  <Frame>
    <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/linelot12.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=0ebdc10b606a5cc5007f0d82cf17c031" alt="Linelot12" width="582" height="441" data-path="images/linelot12.png" />
  </Frame>

***

# Joint Plot

# `sns.jointplot()`

Combines scatter plot + distributions.

```python theme={null}
sns.jointplot(
    data=penguins,
    x='body_mass_g',
    y='flipper_length_mm',
    kind='scatter'
)
```

### Output

Central scatter plot with side histograms.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/joiin.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=6db2cfc6192fb89ce49e4710c3607700" alt="Joiin" width="588" height="584" data-path="images/joiin.png" />
</Frame>

***

# KDE Joint Plot

```python theme={null}
sns.jointplot(
    data=penguins,
    x='body_mass_g',
    y='flipper_length_mm',
    hue='sex',
    kind='kde'
)
```

### Explanation

Uses density estimation instead of scatter points.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/kdeplot.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=476f6731638a75e7a9a9732fbca3cd1f" alt="Kdeplot" width="588" height="584" data-path="images/kdeplot.png" />
</Frame>

***

# Bar Plot

# `sns.barplot()`

Shows average values by category.

```python theme={null}
sns.barplot(
    data=penguins,
    x='species',
    y='body_mass_g',
    hue='sex',
    palette=['red','blue']
)
```

### Explanation

Compares mean body mass.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/barplot.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=bf8ecfc03c4a7b9acb7e9fbbebd2fd2a" alt="Barplot" width="591" height="441" data-path="images/barplot.png" />
</Frame>

***

# Count Plot

# `sns.countplot()`

Counts categorical occurrences.

```python theme={null}
sns.countplot(
    data=penguins,
    x='species'
)
```

### Output

Bar chart of species counts.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/countplot.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=dee0967b1f945b31123697bb0ca64abc" alt="Countplot" width="581" height="441" data-path="images/countplot.png" />
</Frame>

***

# Box Plot

# `sns.boxplot()`

Shows:

* median
* quartiles
* outliers

```python theme={null}
sns.boxplot(
    data=penguins,
    x='species',
    y='body_mass_g',
    hue='sex'
)
```

### Output

Distribution comparison across species.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/boxplot2.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=fa446972eed015953caba9a9396501f9" alt="Boxplot2" width="591" height="441" data-path="images/boxplot2.png" />
</Frame>

***

# Violin Plot

# `sns.violinplot()`

Combines boxplot + density plot.

```python theme={null}
sns.violinplot(
    data=penguins,
    x='species',
    y='body_mass_g',
    hue='sex'
)
```

### Explanation

Width shows density of values.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/violin.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=022a33f9abdeebc2c7df6696750ae8c1" alt="Violin" width="591" height="441" data-path="images/violin.png" />
</Frame>

***

# Split Violin Plot

```python theme={null}
sns.violinplot(
    data=penguins,
    x='species',
    y='body_mass_g',
    hue='sex',
    split=True
)
```

### Explanation

Male and female shown in one violin.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/violinsplit.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=b94775b46cfd1ce0a06039380e1a30ee" alt="Violinsplit" width="591" height="441" data-path="images/violinsplit.png" />
</Frame>

***

# Inner Quartiles

```python theme={null}
sns.violinplot(
    data=penguins,
    x='species',
    y='body_mass_g',
    hue='sex',
    split=True,
    inner='quartile'
)
```

### Explanation

Shows quartile lines inside violin.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/violinquat.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=8aeb893706667447792bc7481566492e" alt="Violinquat" width="591" height="441" data-path="images/violinquat.png" />
</Frame>

***

# Swarm + Violin Combined

```python theme={null}
sns.violinplot(data=penguins,
               x='species',
               y='body_mass_g')

sns.swarmplot(
    data=penguins,
    x='species',
    y='body_mass_g',
    color='black',
    size=3
)
```

### Explanation

Combines density + individual points.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/violin_swarm.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=6bb0872b336e9ee75b025d7ef237a5d1" alt="Violin Swarm" width="528" height="397" data-path="images/violin_swarm.png" />
</Frame>

***

# KDE Plot

# `sns.kdeplot()`

Smooth probability density curve.

```python theme={null}
sns.kdeplot(
    data=penguins,
    x='body_mass_g',
    hue='species',
    fill=True
)
```

### Explanation

* Smooth histogram alternative
* `fill=True` fills area
  <Frame>
    <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/kdeplot1.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=e117631ac5bdd60db38de09293b76686" alt="Kdeplot1" width="624" height="441" data-path="images/kdeplot1.png" />
  </Frame>

***

# Heatmap

# `sns.heatmap()`

Displays matrix with colors.

```python theme={null}
columns = [
    "bill_length_mm",
    "bill_depth_mm",
    "flipper_length_mm",
    "body_mass_g"
]

penguins_corr = penguins[columns].corr()

sns.heatmap(
    data=penguins_corr,
    annot=True,
    vmin=-0.2
)
```

### Explanation

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

### Output

Correlation heatmap.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/heatmap.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=49a5548011a4df1635ab0db80bd9d2d0" alt="Heatmap" width="666" height="551" data-path="images/heatmap.png" />
</Frame>

***

# Rug Plot

# `sns.rugplot()`

Shows individual data points as ticks.

```python theme={null}
sns.rugplot(
    data=penguins,
    x='body_mass_g',
    hue='species',
    palette='Set2'
)
```

### Output

Small tick marks along axis.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/rugplot.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=5ef40aaad300680b52e4718cefbb82ab" alt="Rugplot" width="594" height="441" data-path="images/rugplot.png" />
</Frame>

***

# Pair Plot

# `sns.pairplot()`

Creates pairwise plots automatically.

```python theme={null}
sns.pairplot(
    data=penguins,
    hue='species'
)
```

### Output

Grid of scatter plots and histograms.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/pairplot1.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=045e8debf951fc4cbf46c98191317e9f" alt="Pairplot1" width="1121" height="983" data-path="images/pairplot1.png" />
</Frame>

***

# Pair Plot with Histogram

```python theme={null}
sns.pairplot(
    data=penguins,
    hue='species',
    diag_kind='hist'
)
```

### Explanation

Diagonal uses histograms instead of KDE.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/pairhisto.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=46c3b43b4a7ca7b403196e00069269fd" alt="Pairhisto" width="1121" height="983" data-path="images/pairhisto.png" />
</Frame>

***

# Pair Grid

# `sns.PairGrid()`

Custom subplot grid.

```python theme={null}
g = sns.PairGrid(
    data=penguins,
    hue='sex',
    palette='Set2'
)

g.map_upper(sns.scatterplot)
g.map_lower(sns.kdeplot)
g.map_diag(sns.histplot)

g.add_legend()
```

### Explanation

* `map_upper()` → upper triangle plots
* `map_lower()` → lower triangle plots
* `map_diag()` → diagonal plots

### Output

Fully customized pairwise visualization grid.

<Frame>
  <img src="https://mintcdn.com/ai-923f8160/mpAiq2-25r3qtTyK/images/pair_subplot.png?fit=max&auto=format&n=mpAiq2-25r3qtTyK&q=85&s=73a8daac84bc90d428792fa82f868003" alt="Pair Subplot" width="1109" height="977" data-path="images/pair_subplot.png" />
</Frame>

***

# Seaborn Plot Summary

| Plot        | Purpose                        |
| :---------- | :----------------------------- |
| scatterplot | Relationship between variables |
| stripplot   | Categorical spread             |
| swarmplot   | Non-overlapping stripplot      |
| histplot    | Distribution                   |
| regplot     | Regression trend               |
| lineplot    | Continuous trends              |
| jointplot   | Combined distributions         |
| barplot     | Average comparison             |
| countplot   | Frequency count                |
| boxplot     | Outlier detection              |
| violinplot  | Density + boxplot              |
| kdeplot     | Smooth distribution            |
| heatmap     | Correlation matrix             |
| rugplot     | Individual data ticks          |
| pairplot    | Automatic pair relationships   |
| PairGrid    | Custom pairwise plots          |

***

# Important Seaborn Functions

| Function        | Purpose               |
| :-------------- | :-------------------- |
| `set_style()`   | Plot style            |
| `set_context()` | Scaling               |
| `despine()`     | Remove borders        |
| `scatterplot()` | Scatter plot          |
| `histplot()`    | Histogram             |
| `regplot()`     | Regression line       |
| `lineplot()`    | Line graph            |
| `barplot()`     | Bar chart             |
| `countplot()`   | Count categories      |
| `boxplot()`     | Quartiles & outliers  |
| `violinplot()`  | Density visualization |
| `heatmap()`     | Matrix heatmap        |
| `pairplot()`    | Pairwise analysis     |

***

# 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
