- Data Analysis with Python
-
Overview
- Introduction to Data Science and Analytics
- Loading and Cleaning Data in Pandas
- Data Manipulation with NumPy and Pandas
- Exploratory Data Analysis (EDA) Techniques
- Handling Missing Data and Duplicates
- Merging, Joining, and Concatenating DataFrames
- Time Series Analysis Basics
- Data Visualization with Matplotlib and Seaborn
- Descriptive Statistics and Data Summarization
- Advanced Pandas Operations
Data Visualization with Matplotlib and Seaborn
Add to BookmarkIntroduction
Data visualization is a crucial step in data analysis. It helps in understanding patterns, trends, and relationships within data. Matplotlib and Seaborn are two powerful Python libraries that enable effective data visualization.
In this tutorial, we will cover:
- Introduction to Matplotlib and Seaborn
- Creating Basic Plots with Matplotlib
- Customizing Plots in Matplotlib
- Seaborn for Statistical Data Visualization
- Advanced Plots with Seaborn
- Combining Matplotlib and Seaborn
1. Introduction to Matplotlib and Seaborn
- Matplotlib is a low-level library that provides complete control over plots.
- Seaborn is built on top of Matplotlib and is optimized for statistical data visualization.
Install the libraries if not already installed:
pip install matplotlib seaborn
Import required libraries:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
2. Creating Basic Plots with Matplotlib
Line Plot
# Sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Create a line plot
plt.plot(x, y, label='Sine Wave', color='blue', linestyle='dashed')
# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Line Plot Example')
plt.legend()
plt.show()
Bar Chart
# Sample data
categories = ['A', 'B', 'C', 'D']
values = [10, 25, 15, 30]
# Create a bar plot
plt.bar(categories, values, color='green')
# Adding labels
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Chart Example')
plt.show()
Histogram
# Generate random data
data = np.random.randn(1000)
# Create a histogram
plt.hist(data, bins=30, color='purple', edgecolor='black')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram Example')
plt.show()
3. Customizing Plots in Matplotlib
Matplotlib provides options to customize plots.
plt.figure(figsize=(8, 5))
plt.plot(x, y, color='red', linewidth=2, marker='o', markersize=5)
plt.xlabel('X-axis', fontsize=12)
plt.ylabel('Y-axis', fontsize=12)
plt.title('Customized Line Plot', fontsize=14)
plt.grid(True)
plt.show()
4. Seaborn for Statistical Data Visualization
Seaborn simplifies data visualization and provides aesthetically pleasing plots.
# Load sample dataset
df = sns.load_dataset("tips")
# Scatter plot using Seaborn
sns.scatterplot(x='total_bill', y='tip', data=df, hue='sex', style='smoker')
plt.title("Scatter Plot with Seaborn")
plt.show()
5. Advanced Plots with Seaborn
Box Plot (To analyze distributions and detect outliers)
sns.boxplot(x='day', y='total_bill', data=df, palette="coolwarm")
plt.title("Box Plot Example")
plt.show()
Heatmap (For correlation analysis)
correlation_matrix = df.corr()
sns.heatmap(correlation_matrix, annot=True, cmap="coolwarm", linewidths=0.5)
plt.title("Heatmap Example")
plt.show()
6. Combining Matplotlib and Seaborn
You can use Matplotlib for fine-tuning plots created with Seaborn.
plt.figure(figsize=(8, 5))
sns.histplot(df['total_bill'], kde=True, bins=20, color='blue')
plt.xlabel('Total Bill')
plt.ylabel('Count')
plt.title('Histogram with KDE using Seaborn')
plt.grid(True)
plt.show()
Conclusion
- Matplotlib is great for detailed custom visualizations.
- Seaborn simplifies statistical data visualization.
- Both libraries can be used together for powerful visual storytelling.
Prepare for Interview
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
- Debugging in Python
- Unit Testing in Python
- Asynchronous Programming in PYthon
- Multithreading and Multiprocessing in Python
- Context Managers in Python
- Decorators in Python
Random Blogs
- What Is SEO and Why Is It Important?
- 5 Ways Use Jupyter Notebook Online Free of Cost
- Why to learn Digital Marketing?
- Create Virtual Host for Nginx on Ubuntu (For Yii2 Basic & Advanced Templates)
- Loan Default Prediction Project Using Machine Learning
- Python Challenging Programming Exercises Part 1
- Variable Assignment in Python
- Google’s Core Update in May 2020: What You Need to Know
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- Grow your business with Facebook Marketing
- Downlaod Youtube Video in Any Format Using Python Pytube Library
- Understanding Data Lake, Data Warehouse, Data Mart, and Data Lakehouse – And Why We Need Them
- Convert RBG Image to Gray Scale Image Using CV2
- String Operations in Python
- Extract RGB Color From a Image Using CV2
Datasets for Machine Learning
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset