Skip to main content

Introduction to Matplotlib

What is Data Visualization?

Data Visualization is the process of converting raw data into charts, graphs, and plots to:
  • Understand patterns
  • Detect trends
  • Find outliers
  • Analyze relationships

What is Matplotlib?

Matplotlib is a Python library used for creating:
  • Line charts
  • Histograms
  • Boxplots
  • Pie charts
  • Scatter plots
  • Bar charts
  • 3D plots
Widely used in:
  • Data Science
  • Machine Learning
  • Analytics

Installing Matplotlib

Installation


Importing Libraries

Explanation

  • matplotlib.pyplot → plotting functions
  • pandas → data handling

Basic Plot Function

plt.plot()

Creates a line plot.

Output

A line graph connecting points:
  • (1,4)
  • (2,5)
  • (3,6)

Explanation

  • plot() → creates line graph
  • grid() → adds background grid
  • show() → displays graph
    Basicline

Components of a Figure


Univariate Analysis

Analyzing one variable.

Line Plot

Shows trends or changes in numerical data.

Explanation

  • color='red' → line color
  • marker='o' → circle markers
  • linestyle=':' → dotted line
  • linewidth=2 → line thickness

Output

Salary trend line graph.
Lineplot

Histogram

plt.hist()

Shows frequency distribution.

Explanation

  • bins=5 → divides data into 5 ranges
  • Shows how many values fall into each range

Output

Bar-like histogram distribution.
Histogram

Box Plot

plt.boxplot()

Used for:
  • detecting outliers
  • understanding spread
  • quartile analysis

Output

Boxplot showing median and quartiles.
Boxplot

Outlier Detection

Explanation

Adding 0 creates an outlier visible in boxplot.

Categorical Analysis


Pie Chart

Shows percentage contribution.

Explanation

  • labels → category names
  • autopct → percentage display
  • explode → separates slice
  • axis('equal') → perfect circle

Output

Department percentage pie chart.
Pie

Count Plot / Bar Chart

plt.bar()

Shows category frequencies.

Output

Bar chart of department counts.
Countplot25

Bivariate Analysis

Analyzing relationship between two variables.

Scatter Plot

plt.scatter()

Shows relationship between two numerical variables.

Explanation

Each dot represents:
  • X → Salary
  • Y → Age

Output

Scatter plot showing salary vs age.
Scatterplot26

Sorted Line Plot

Explanation

Sorting improves line continuity.
Lineplot

Bar Chart

Explanation

Compares salary for each age.
Bar1

Numerical vs Categorical Analysis


Multiple Boxplots

Explanation

Compares salary distributions across departments.

Output

Three side-by-side boxplots.
Multibox

Department Salary Pie Chart

Explanation

Shows total salary contribution by department.
Pie2

Mean Salary Bar Chart

Explanation

Displays average salary per department.
Meanbar

Multivariate Analysis

Analyzing 3 or more variables.

Bubble Plot

Explanation

  • X → Salary
  • Y → Age
  • Bubble Size → Experience

Output

Bubble plot with varying circle sizes.
Bubble

Color-Based Scatter Plot

Explanation

Different colors represent departments.
Colorscatter

Scatter Plot with Legend

Explanation

Adds department-wise legend.
Scatterlegend

Object Oriented API

Provides more control over plots.

plt.subplots()

Creates multiple plots.

Explanation

  • 2 rows
  • 2 columns
  • Figure size = 10x10
    Download

Multiple Plots

Line Plot


Histogram

Multiplot2

Boxplot


Saving Figures

savefig()

Saves plot locally.

Explanation

Saves graph as PNG image.

Multiple Line Plots

Explanation

Displays multiple lines in same graph.

Output

Sales, Profit, and Expenses comparison graph.
Multilineplot

3D Plot

Explanation

Creates 3D scatter plot. Axes:
  • X → Year
  • Y → Sales
  • Z → Profit
    3d

Plotly 3D Plot

Explanation

Interactive 3D visualization using Plotly.
I3d

Important Plot Types Summary


Important Matplotlib Functions


Matplotlib Usage

Matplotlib helps to:
  • Visualize datasets
  • Understand trends
  • Detect outliers
  • Compare categories
  • Analyze relationships
  • Create professional graphs