Python Docs
Matplotlib & Seaborn for Data Visualization
Matplotlib provides low-level control for building any type of plot, while Seaborn builds on top of it to create beautiful, statistical visualizations quickly. Together, they form the core of Python’s data visualization ecosystem.
Why Matplotlib?
Matplotlib is extremely flexible and can create almost any type of plot:
- Line plots
- Bar charts
- Scatter plots
- Histograms
- Custom drawings with fine-grained control
It's especially powerful when you need full customization.
Matplotlib Example
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.cos(x)
plt.figure(figsize=(6, 4))
plt.plot(x, y, label='Cosine', color='purple')
plt.title('Cosine Curve')
plt.xlabel('x')
plt.ylabel('cos(x)')
plt.legend()
plt.grid(True)
plt.show()Why Seaborn?
Seaborn focuses on statistical visualizations and works seamlessly with pandas DataFrames. It offers:
- Beautiful default themes
- Built-in statistical plots (boxplot, violin, KDE plot, etc.)
- Instant integration with categorical and numeric features
- Automatic handling of grouping with
hue
Perfect for Exploratory Data Analysis (EDA).
Seaborn Example
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('tips')
plt.figure(figsize=(7,5))
sns.scatterplot(data=df, x='total_bill', y='tip', hue='sex', style='time')
plt.title('Bill Amount vs Tip (colored by gender)')
plt.grid(True)
plt.show()Common Plot Types (Quick Overview)
- Line Plot — trends over time
- Scatter Plot — relationships between two variables
- Bar Chart — categorical comparison
- Histogram — distribution of numerical data
- Box / Violin Plot — statistical distribution
- Heatmap — correlation matrices & pivot tables
Tips
- Use Seaborn for EDA, Matplotlib for customization.
- Always add titles, labels, and legends.
- Start with simple plots → then build complexity.
- Use
figsizeto control plot size. - Use
plt.grid(True)to make plots easier to read.