Logo
READLEARNKNOWCONNECT
Back to Lessons

    Page

  • - Why Visualize Data?
  • - Installing Libraries
  • - Basic Matplotlib Plots
  • - Customizing Plots
  • - Introduction to Seaborn
  • - Common Plot Types
  • - Best Practices for Data Visualization
  • - Common Misconceptions
  • - Mini Project Step

22. Data Visualization with Matplotlib and Seaborn

Level: IntermediateDuration: 42m

Why Visualize Data?

Visualizations help you understand patterns, trends, and insights in your data quickly. Python libraries like Matplotlib and Seaborn provide powerful ways to create customizable and attractive plots.

Installing Libraries

bash
pip install matplotlib seaborn

Basic Matplotlib Plots

Matplotlib is a flexible library for creating 2D plots. Let's start with line and scatter plots.

python
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 25, 30]

# Line plot
plt.plot(x, y, label='Line Plot', color='blue', marker='o')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Basic Line Plot')
plt.legend()
plt.show()

# Scatter plot
plt.scatter(x, y, color='red', label='Scatter Plot')
plt.show()

Customizing Plots

You can change colors, markers, line styles, labels, titles, and add legends or grids for clarity.

python
plt.plot(x, y, color='green', linestyle='--', marker='s', linewidth=2)
plt.title('Styled Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid(True)
plt.show()

Introduction to Seaborn

Seaborn is built on Matplotlib and simplifies creating attractive statistical plots. It also integrates well with Pandas DataFrames.

python
import seaborn as sns
import pandas as pd

data = pd.DataFrame({
    'x': [1, 2, 3, 4, 5],
    'y': [10, 20, 15, 25, 30],
    'category': ['A', 'B', 'A', 'B', 'A']
})

# Line plot
sns.lineplot(data=data, x='x', y='y', hue='category', marker='o')
plt.show()

# Scatter plot
sns.scatterplot(data=data, x='x', y='y', hue='category', style='category', s=100)
plt.show()

Common Plot Types

  • Line Plot – trends over time or sequences
  • Scatter Plot – relationship between two variables
  • Bar Chart – categorical comparison
  • Histogram – distribution of values
  • Box Plot – summary statistics and outliers
  • Heatmap – correlation or matrix data

Best Practices for Data Visualization

  • Always label axes and include units if needed.
  • Use titles and legends to make plots self-explanatory.
  • Avoid clutter – focus on clarity.
  • Choose color palettes that are accessible (e.g., colorblind-friendly).
  • Use Seaborn styles (`sns.set_style('whitegrid')`) for cleaner visuals.

Common Misconceptions

  • Matplotlib is outdated – it’s still the core library and highly customizable.
  • Seaborn is slower – for large datasets, combining Seaborn with Matplotlib for customization works best.
  • More colors or 3D plots aren’t always better – clarity comes first.

Matplotlib and Seaborn Tutorials

💡 Good visualizations tell a story. Think about what insight you want to communicate and pick the simplest, clearest plot type for it.

Mini Project Step

Using a sample dataset (e.g., CSV of sales or weather data), create a line plot, scatter plot, and bar chart. Customize colors, add labels, and use Seaborn styling. Bonus: create a heatmap of correlations between numerical columns.