Visualizing Data with Matplotlib and Seaborn
๐ Visualizing Data with Matplotlib and Seaborn
๐ง Getting Started
๐ฆ Installation (if not already installed):
pip install matplotlib seaborn
๐ Import the libraries:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
๐️ Sample Dataset
Let’s use Seaborn’s built-in dataset for simplicity:
df = sns.load_dataset("tips") # Dataset of restaurant bills
df.head()
๐จ 1. Matplotlib Basics
Matplotlib is low-level but highly customizable.
✅ Line Plot
plt.plot(df['total_bill'])
plt.title('Total Bill Over Time')
plt.xlabel('Index')
plt.ylabel('Total Bill ($)')
plt.show()
✅ Bar Chart
df['day'].value_counts().plot(kind='bar')
plt.title('Count of Records per Day')
plt.xlabel('Day')
plt.ylabel('Count')
plt.show()
✅ Histogram
plt.hist(df['total_bill'], bins=10, color='skyblue')
plt.title('Distribution of Total Bill')
plt.xlabel('Total Bill ($)')
plt.ylabel('Frequency')
plt.show()
๐ 2. Seaborn Basics
Seaborn is built on top of Matplotlib and makes beautiful statistical plots with much less code.
✅ Histogram / KDE Plot
sns.histplot(df['total_bill'], kde=True, color='purple')
plt.title('Total Bill Distribution with KDE')
plt.show()
✅ Box Plot
sns.boxplot(x='day', y='total_bill', data=df)
plt.title('Total Bill by Day')
plt.show()
✅ Violin Plot
sns.violinplot(x='day', y='tip', data=df)
plt.title('Tip Distribution by Day')
plt.show()
✅ Scatter Plot
sns.scatterplot(x='total_bill', y='tip', hue='sex', data=df)
plt.title('Tip vs Total Bill')
plt.show()
๐ 3. Relationships Between Variables
✅ Pair Plot
sns.pairplot(df, hue='sex')
plt.show()
✅ Heatmap (Correlation Matrix)
corr = df.corr(numeric_only=True)
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.title('Correlation Matrix')
plt.show()
๐ง 4. Customizing Your Plots
plt.figure(figsize=(10, 6))
sns.barplot(x='day', y='total_bill', data=df, palette='Blues')
plt.title('Average Total Bill by Day')
plt.xlabel('Day of Week')
plt.ylabel('Total Bill ($)')
plt.tight_layout()
plt.show()
๐ Best Practices
✅ Use clear titles and labels
✅ Choose the right chart for your data
✅ Use colors to add meaning, not just decoration
✅ Keep it simple and readable
✅ Use figsize to adjust plot size for reports or presentations
๐ Combining Seaborn with Matplotlib
You can always use plt functions after Seaborn to fine-tune:
sns.boxplot(x='day', y='tip', data=df)
plt.title('Tips by Day')
plt.ylabel('Tip Amount ($)')
plt.grid(True)
plt.show()
๐งช Suggested Mini Project
Dataset: iris (available via sns.load_dataset('iris'))
Goal: Explore the relationships between flower measurements.
Tasks:
Histogram of sepal_length
Box plot of petal_length by species
Pairplot of all numeric columns
Correlation heatmap
✅ Summary Table
Task Matplotlib Function Seaborn Function
Line plot plt.plot() —
Bar chart plt.bar() sns.barplot()
Histogram plt.hist() sns.histplot()
Box plot — sns.boxplot()
Scatter plot plt.scatter() sns.scatterplot()
Correlation heatmap imshow() (complex) sns.heatmap()
Multi-variate overview — sns.pairplot()
Learn Data Science Course in Hyderabad
Read More
Data Manipulation with dplyr in R
10 Pandas Functions Every Data Scientist Should Know
Focus on the practical, code-based aspects of data science.
Visit Our Quality Thought Training Institute in Hyderabad
Comments
Post a Comment