Seaborn Basics

Python · Data Visualization

Theory

Seaborn is a statistical charting library built on matplotlib that works directly with DataFrames: you pass data=df and column names, and it groups, aggregates, colours and adds legends for you. sns.histplot(data=df, x='bill', hue='day') draws overlapping histograms per group, sns.boxplot(data=df, x='day', y='bill') draws one box per day, and sns.barplot(data=df, x='day', y='bill') draws the mean bill per day with an error bar for its uncertainty.

sns.scatterplot adds hue= and size= encodings, sns.heatmap(df.corr(), annot=True) colours a matrix and writes the numbers, and sns.pairplot(df) draws a scatter plot for every pair of numeric columns with histograms on the diagonal, a fast first look at a new dataset. Every seaborn call returns or draws on a matplotlib Axes, so ax.set_title() still works. Seaborn is not available in this in-browser runner, so run seaborn code locally after pip install seaborn.

Why we use it

Seaborn turns common statistical charts into one line with sensible defaults: grouping by hue, automatic legends, error bars and attractive styles. That makes exploratory analysis much faster than building the same charts by hand in matplotlib.

When to use it

Use seaborn for exploring DataFrames: distributions, comparisons across groups, relationships and correlation heatmaps. Use plain matplotlib for highly customised figures, and pandas df.plot for the quickest simple charts.

How it works

  1. Tidy DataFrame: one row per record
  2. What question are you asking? Yes: Pick histplot / boxplot / barplot / scatterplot / heatmap. No: Start with sns.pairplot(df).
  3. Map columns: x=, y=, hue=
  4. Seaborn groups, aggregates and draws via matplotlib
  5. Polish with ax.set_title / ax.set_xlabel
  6. Statistical chart in one line

Syntax

import seaborn as sns
sns.histplot(data=df, x='col', hue='group')
sns.boxplot(data=df, x='group', y='value')
sns.barplot(data=df, x='group', y='value')
sns.heatmap(df.corr(numeric_only=True), annot=True)
sns.pairplot(df)

Example

import seaborn as sns
sns.barplot(data=tips, x='day', y='bill')
sns.boxplot(data=tips, x='day', y='bill')
sns.pairplot(tips)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic