DEV Community

Cover image for 🎨 Data Visualization with Seaborn – A Beginner’s Guide
likhitha manikonda
likhitha manikonda

Posted on

🎨 Data Visualization with Seaborn – A Beginner’s Guide

If you’re new to Python and want beautiful, informative charts with minimal code, Seaborn is your best friend!

This guide covers all essential concepts with simple explanations and examples.


✅ What is Seaborn?

Seaborn is a Python library built on Matplotlib that makes it easy to create attractive and statistical plots.


📦 Installing Seaborn

pip install seaborn
Enter fullscreen mode Exit fullscreen mode

Import it:

import seaborn as sns
import matplotlib.pyplot as plt
Enter fullscreen mode Exit fullscreen mode

🔑 Why Seaborn?

  • Less code, more beauty.
  • Built-in themes and color palettes.
  • Handles DataFrames directly.
  • Great for statistical visualization.

✅ Basic Setup

import seaborn as sns
import matplotlib.pyplot as plt

# Load sample dataset
tips = sns.load_dataset('tips')
print(tips.head())
Enter fullscreen mode Exit fullscreen mode

1️⃣ Line Plot

sns.lineplot(x='total_bill', y='tip', data=tips)
plt.title('Line Plot: Bill vs Tip')
plt.show()
Enter fullscreen mode Exit fullscreen mode

Use Case: Show trends or continuous relationships.


2️⃣ Bar Plot

sns.barplot(x='day', y='total_bill', data=tips)
plt.title('Average Bill by Day')
plt.show()
Enter fullscreen mode Exit fullscreen mode

Use Case: Compare categories.


3️⃣ Count Plot

sns.countplot(x='day', data=tips)
plt.title('Count of Records per Day')
plt.show()
Enter fullscreen mode Exit fullscreen mode

Use Case: Frequency of categories.


4️⃣ Histogram & KDE

sns.histplot(tips['total_bill'], bins=10, kde=True)
plt.title('Distribution of Total Bill')
plt.show()
Enter fullscreen mode Exit fullscreen mode

Use Case: Show distribution with smooth curve.


5️⃣ Scatter Plot

sns.scatterplot(x='total_bill', y='tip', hue='sex', data=tips)
plt.title('Bill vs Tip by Gender')
plt.show()
Enter fullscreen mode Exit fullscreen mode

Use Case: Relationship between two variables.


6️⃣ Box Plot

sns.boxplot(x='day', y='total_bill', data=tips)
plt.title('Bill Spread by Day')
plt.show()
Enter fullscreen mode Exit fullscreen mode

Use Case: Detect outliers and spread.


7️⃣ Pair Plot

sns.pairplot(tips, hue='sex')
plt.show()
Enter fullscreen mode Exit fullscreen mode

Use Case: Explore relationships across multiple variables.


🎨 Customization

  • Themes:
sns.set_style('whitegrid')
sns.set_palette('pastel')
Enter fullscreen mode Exit fullscreen mode
  • Titles & Labels:
plt.title('My Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
Enter fullscreen mode Exit fullscreen mode

💾 Save Your Plot

plt.savefig('seaborn_plot.png')
Enter fullscreen mode Exit fullscreen mode

✅ Summary Table

Plot Type Use Case
Line Plot Trends over time
Bar Plot Compare categories
Count Plot Frequency of categories
Histogram Distribution of data
Scatter Correlation between variables
Box Plot Spread & outliers
Pair Plot Multi-variable relationships

🔥 Pro Tip: Seaborn works best with Pandas DataFrames and categorical + numerical data.


📊 Matplotlib vs Seaborn: Which Should You Use for Data Visualization?

If you’re starting with Python visualization, you’ll often hear about Matplotlib and Seaborn. Both are powerful, but they serve different purposes. This guide explains the differences and shows examples side by side so you can choose the right tool.


✅ What is Matplotlib?

  • Low-level library for creating static, interactive, and animated plots.
  • Offers full control over every element of the plot.
  • Requires more code for styling and aesthetics.

✅ What is Seaborn?

  • Built on top of Matplotlib.
  • Provides high-level API for attractive, statistical plots.
  • Works seamlessly with Pandas DataFrames.
  • Comes with themes and color palettes out of the box.

🔍 Key Differences

Feature Matplotlib Seaborn
Level Low-level (manual styling) High-level (auto styling)
Ease of Use More code, more control Less code, quick results
Data Support Lists, arrays Pandas DataFrames
Styling Manual Built-in themes & palettes
Focus General plotting Statistical visualization

✅ Installation

pip install matplotlib seaborn
Enter fullscreen mode Exit fullscreen mode

✅ Example 1: Line Plot

🔹 Matplotlib

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]

plt.plot(x, y, marker='o')
plt.title('Matplotlib Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid(True)
plt.show()
Enter fullscreen mode Exit fullscreen mode

🔹 Seaborn

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset('tips')
sns.lineplot(x='total_bill', y='tip', data=tips)
plt.title('Seaborn Line Plot')
plt.show()
Enter fullscreen mode Exit fullscreen mode

✅ Example 2: Bar Chart

🔹 Matplotlib

categories = ['A', 'B', 'C', 'D']
values = [10, 15, 7, 12]

plt.bar(categories, values, color='skyblue')
plt.title('Matplotlib Bar Chart')
plt.show()
Enter fullscreen mode Exit fullscreen mode

🔹 Seaborn

sns.barplot(x='day', y='total_bill', data=tips)
plt.title('Seaborn Bar Chart')
plt.show()
Enter fullscreen mode Exit fullscreen mode

✅ Example 3: Scatter Plot

🔹 Matplotlib

x = [5, 7, 8, 7, 2, 17, 2, 9]
y = [99, 86, 87, 88, 100, 86, 103, 87]

plt.scatter(x, y, color='green')
plt.title('Matplotlib Scatter Plot')
plt.show()
Enter fullscreen mode Exit fullscreen mode

🔹 Seaborn

sns.scatterplot(x='total_bill', y='tip', hue='sex', data=tips)
plt.title('Seaborn Scatter Plot')
plt.show()
Enter fullscreen mode Exit fullscreen mode

✅ Styling & Customization

🔹 Matplotlib

plt.title('Custom Title')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
Enter fullscreen mode Exit fullscreen mode

🔹 Seaborn

sns.set_style('whitegrid')
sns.set_palette('pastel')
Enter fullscreen mode Exit fullscreen mode

✅ When to Use Which?

  • Use Matplotlib when you need full control and custom designs.
  • Use Seaborn for quick, beautiful, and statistical plots.

🔥 Pro Tip: You can combine both! Use Seaborn for the main plot and Matplotlib for fine-tuning.


Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.