Matplotlib & Seaborn
Matplotlib & Seaborn – Visualizing Data with Python
Data visualization is one of the most essential skills in data science. Raw data is hard to understand, but visualizations make patterns, trends, and insights easy to interpret.
Python provides powerful libraries like Matplotlib and Seaborn to create clear, professional, and informative visualizations.
In this guide, you will learn:
✔ How to use Matplotlib for basic plots
✔ How to use Seaborn for advanced visualizations
✔ Tips for styling and customizing charts
✔ Real-world examples
What is Matplotlib?
Matplotlib is a foundational Python library for creating static, animated, and interactive plots.
Key features:
-
Line plots, scatter plots, bar charts, histograms
-
Highly customizable
-
Integrates with Pandas and NumPy
Installation
pip install matplotlibBasic Plots with Matplotlib
1️⃣ Line Plot
import matplotlib.pyplot as plt
# Sample data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [250, 400, 300, 500, 450]
plt.plot(months, sales, marker='o', color='blue', linestyle='--')
plt.title("Monthly Sales Trend")
plt.xlabel("Month")
plt.ylabel("Sales")
plt.grid(True)
plt.show()2️⃣ Bar Chart
products = ['Laptop', 'Phone', 'Tablet']
revenue = [1000, 1500, 800]
plt.bar(products, revenue, color='green')
plt.title("Revenue by Product")
plt.ylabel("Revenue")
plt.show()3️⃣ Histogram
ages = [22, 25, 29, 30, 32, 35, 37, 40, 42, 45]
plt.hist(ages, bins=5, color='purple', edgecolor='black')
plt.title("Age Distribution")
plt.xlabel("Age")
plt.ylabel("Frequency")
plt.show()What is Seaborn?
Seaborn is built on top of Matplotlib and provides a high-level interface for creating attractive and informative statistical graphics.
Advantages:
Beautiful default styles
Simplifies complex plots
Integrates seamlessly with Pandas DataFrames
Installation - pip install seaborn
Visualizations with Seaborn
1️⃣ Scatter Plot
import seaborn as sns
import pandas as pd
# Sample DataFrame
data = pd.DataFrame({
'Age': [22, 25, 29, 30, 32, 35, 37, 40, 42, 45],
'Salary': [20000, 25000, 30000, 32000, 35000, 40000, 42000, 45000, 47000, 50000]
})
sns.scatterplot(x='Age', y='Salary', data=data, color='red', s=100)
plt.title("Age vs Salary")
plt.show()2️⃣ Box Plotsns.boxplot(x='Age', data=data, color='lightblue')
plt.title("Age Distribution Box Plot")
plt.show()3️⃣ Histogram & KDE
sns.histplot(data['Salary'], bins=5, kde=True, color='orange')
plt.title("Salary Distribution with KDE")
plt.show()4️⃣ Heatmap
corr = data.corr()
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.title("Correlation Heatmap")
plt.show()Customizing Plots
Matplotlib & Seaborn allow full customization:
Colors & styles:
color,palette,linestyleFigure size:
plt.figure(figsize=(10,6))Titles & labels:
plt.title(),plt.xlabel(),plt.ylabel()Grids & legends:
plt.grid(True),plt.legend()Example:
plt.figure(figsize=(8,5))
plt.plot(months, sales, marker='s', color='magenta', linestyle='-')
plt.title("Customized Sales Chart", fontsize=14)
plt.xlabel("Month", fontsize=12)
plt.ylabel("Sales", fontsize=12)
plt.grid(True)
plt.show()Real-World Use Cases
Business reporting dashboards
Financial data visualization
Sales & marketing trends
Data exploration in machine learning projects
Academic research & presentations
Tips for Effective Data Visualization
Always label axes and titles
Choose appropriate chart types
Avoid cluttered visuals
Use colors meaningfully
Combine Matplotlib & Seaborn for best results
Data visualization is key to understanding datasets and communicating insights effectively.
Use Matplotlib for flexibility and low-level control
Use Seaborn for aesthetics and statistical visualizations
Mastering both libraries allows you to build professional dashboards, reports, and presentations with Python.
Comments
Post a Comment