Back to Study Material Python

Seaborn

Seaborn is a statistical data visualization library built on Matplotlib. It simplifies the creation of attractive, informative charts and integrates tightl...

Seaborn

Statistical Visualization with Seaborn

Seaborn is a statistical data visualization library built on Matplotlib. It simplifies the creation of attractive, informative charts and integrates tightly with Pandas DataFrames. Where Matplotlib requires many lines to configure a chart, Seaborn often needs just one.

Install with pip install seaborn and import as: import seaborn as sns.

Seaborn's key strength is that it understands DataFrames natively. You pass the DataFrame and column names directly: import seaborn as sns import matplotlib.pyplot as plt tips = sns.load_dataset('tips') # built-in sample dataset sns.scatterplot(data=tips, x='total_bill', y='tip', hue='sex') plt.show()

The hue parameter automatically colors points by category, and Seaborn adds a legend. The size and style parameters add further dimensions to a single plot.

Core plot types and when to use them: - sns.histplot / sns.kdeplot: distribution of a single variable - sns.boxplot / sns.violinplot: spread and outliers by category - sns.heatmap: correlation matrices and pivot tables - sns.pairplot: all pairwise relationships in a dataset at once - sns.barplot: mean and confidence interval by category - sns.lineplot: trends over time with optional confidence bands

Correlation heatmap example: corr = tips.corr(numeric_only=True) sns.heatmap(corr, annot=True, cmap='coolwarm', fmt='.2f')

FacetGrid creates small multiples, the same chart repeated for each category: g = sns.FacetGrid(tips, col='time', row='sex') g.map(sns.scatterplot, 'total_bill', 'tip')

Seaborn themes control the overall look: sns.set_theme(style='whitegrid') sets a clean white background with grid lines. Other styles include 'darkgrid', 'ticks', and 'white'.

Practical tip: Use sns.pairplot(df, hue='target') as a quick exploratory step on any new dataset. It immediately reveals correlations and class separability before you build models.

Exam tip: Seaborn plots return Matplotlib Axes objects, so all Matplotlib customizations (ax.set_title, ax.set_xlabel) work on them. Remember this when asked how to add titles to Seaborn plots.