Data Visualization in Python (Matplotlib & Seaborn)

📘 Python for Data Science 👁 53 views 📅 Nov 14, 2025
⏱ Estimated reading time: 4 min

Data visualization is one of the most essential steps in data analysis and data science. It allows us to understand data patterns, detect outliers, explore relationships, and communicate insights effectively. In Python, the two most popular libraries for visualization are:

  • Matplotlib → Low-level, highly customizable plotting library

  • Seaborn → High-level library built on top of Matplotlib, focused on statistical graphs

This answer covers concepts, features, syntax, examples, and differences in a comprehensive way.


-------------------------------------------

1. Matplotlib

-------------------------------------------

Matplotlib is the oldest and most widely-used Python plotting library. It provides fine-grained control over every element of a figure.

✔ Importing Matplotlib

import matplotlib.pyplot as plt

1.1 Basic Plotting with Matplotlib

Line Plot

Used for showing trends over time.

x = [1,2,3,4,5] y = [10, 14, 12, 18, 20] plt.plot(x, y) plt.xlabel("X-axis") plt.ylabel("Y-axis") plt.title("Simple Line Plot") plt.show()

1.2 Types of Plots in Matplotlib

(A) Bar Plot

plt.bar(['A','B','C'], [10,20,15])

(B) Scatter Plot

plt.scatter(x, y)

(C) Histogram

plt.hist(data, bins=10)

(D) Pie Chart

plt.pie(values, labels=labels, autopct='%1.1f%%')

(E) Box Plot

Used to detect outliers.

plt.boxplot(data)

1.3 Figure and Subplots

Creating multiple plots in one figure:

fig, axes = plt.subplots(1, 2, figsize=(10,4)) axes[0].plot(x, y) axes[1].bar(['A','B','C'], [10,20,15]) plt.show()

1.4 Customization in Matplotlib

  • Colors

  • Line styles (--, -., :)

  • Markers (o, *, ^)

  • Grid

  • Legends

  • Axis limits

Example:

plt.plot(x, y, color='red', linestyle='--', marker='o') plt.grid(True) plt.legend(["Sales"])

-------------------------------------------

2. Seaborn

-------------------------------------------

Seaborn is a statistical plotting library that provides beautiful and informative visualizations with less code.

✔ Importing Seaborn

import seaborn as sns

It works seamlessly with Pandas DataFrames.


2.1 Seaborn's Advantages

  • Automatically handles themes and color palettes

  • Best for statistical analysis

  • Less code, better visuals

  • Works with DataFrames & column names directly


2.2 Seaborn Built-in Styles

sns.set_style('whitegrid')

Styles: white, dark, whitegrid, darkgrid, ticks


2.3 Types of Plots in Seaborn

(A) Distribution Plots

Histogram + KDE (Kernel Density Estimate)

sns.histplot(data, kde=True)

KDE only

sns.kdeplot(data)

(B) Relational Plots

Scatter Plot

sns.scatterplot(x='age', y='salary', data=df)

Line Plot

sns.lineplot(x='year', y='sales', data=df)

(C) Categorical Plots

Bar Plot

sns.barplot(x='city', y='sales', data=df)

Count Plot

sns.countplot(x='city', data=df)

Box Plot

sns.boxplot(x='gender', y='salary', data=df)

Violin Plot

Combined distribution + boxplot.

sns.violinplot(x='city', y='sales', data=df)

(D) Pair Plot

Shows relationship between all numerical columns.

sns.pairplot(df)

(E) Heatmap

Best for correlation matrices.

sns.heatmap(df.corr(), annot=True, cmap='coolwarm')

-------------------------------------------

3. Matplotlib vs Seaborn

FeatureMatplotlibSeaborn
LevelLow-levelHigh-level
CustomizationFull controlLess control
Code LengthLongerShorter
AppearanceBasicBeautiful defaults
Statistical PlotsManualBuilt-in
DataFrame SupportLimitedExcellent

-------------------------------------------

4. Combining Seaborn + Matplotlib

Both libraries can be used together:

sns.barplot(x='city', y='sales', data=df) plt.title("Sales by City") plt.xlabel("City") plt.ylabel("Sales") plt.show()

-------------------------------------------

5. Best Practices for Data Visualization

✔ Choose the right chart

  • Line → trends

  • Bar → comparison

  • Heatmap → correlations

  • Box plot → outliers

  • Histogram → distribution

✔ Keep visuals clean

  • Minimal colors

  • Clear labels

  • Avoid clutter

✔ Use consistent style and color palette

✔ Include titles, axis labels, legends

✔ Always check data before plotting


-------------------------------------------

6. Real-Life Example: Exploratory Visualization

import pandas as pd import seaborn as sns import matplotlib.pyplot as plt df = pd.read_csv("sales_data.csv") sns.set_style("darkgrid") # 1. Distribution of sales sns.histplot(df['sales'], kde=True) plt.title("Distribution of Sales") plt.show() # 2. Sales by category sns.boxplot(x='category', y='sales', data=df) plt.show() # 3. Correlation heatmap sns.heatmap(df.corr(), annot=True) plt.show()

Conclusion

Data visualization using Matplotlib and Seaborn is a core skill in data science.

  • Matplotlib gives detailed control and flexibility.

  • Seaborn offers beautiful statistical plots with minimal code.

Both libraries together allow you to produce high-quality insights, dashboards, and reports for analysis and communication.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes