Below is a complete, beginner-friendly but deep explanation of Matplotlib, from the absolute basics to
advanced usage. I’ll break it into clear sections so you can understand it fully.
■ WHAT IS MATPLOTLIB?
Matplotlib is Python’s standard plotting library used for creating graphs, charts, statistical plots,
scientific figures, and data visualizations. The most commonly used module is:
import [Link] as plt
plt works like a drawing board where you create plots.
■ WHY MATPLOTLIB IS SO IMPORTANT
• Used in engineering, data science, machine learning, research • Works perfectly with NumPy, Pandas
• Completely customizable • You can draw any type of plot, from simple line graphs to advanced
heatmaps
■ CORE CONCEPTS YOU MUST UNDERSTAND Everything in Matplotlib revolves around 3 core
ideas:
1■■ The Figure
Think of it as the entire canvas. A figure can contain multiple plots.
fig = [Link]()
2■■ Axes (Subplots)
This is the actual plot area, where you see the graph.
fig, ax = [Link]()
fig → whole page ax → one plot (graph)
You can create multiple subplots:
fig, ax = [Link](2, 2)
3■■ The Plotting Functions
Examples:
[Link]() # line graph
[Link]() # scatter plot
[Link]() # bar chart
[Link]() # histogram
[Link]() # image
[Link]() # pie chart
Almost everything is built on these.
■ STYLING A PLOT (Very Important)
ax.set_title("Speed vs Time")
ax.set_xlabel("Time (s)")
ax.set_ylabel("Speed (m/s)")
[Link](True)
[Link]()
[Link](x, y, color='red', linestyle='--', linewidth=2)
■ MOST COMMON PLOT TYPES IN MATPLOTLIB
1■■ Line Plot
[Link](x, y)
2■■ Scatter Plot
[Link](x, y)
3■■ Bar Plot
[Link](categories, values)
4■■ Histogram
[Link](values, bins=20)
5■■ Pie Chart
[Link](values, labels=labels)
6■■ Heatmap
[Link](data, cmap='hot')
■ FIGURE SIZE, DPI & SAVING PLOTS
[Link](figsize=(8, 6))
[Link](dpi=300)
[Link]("[Link]")
■ THE OBJECT-ORIENTED (OO) APPROACH (Recommended)
fig, ax = [Link]()
[Link](x, y)
ax.set_title("Example")
[Link]()
■ SUBPLOTS (Multiple Plots)
fig, ax = [Link](2, 2)
ax[0, 0].plot(x, y)
ax[1, 1].scatter(a, b)
■ STYLES & THEMES
[Link]("seaborn-v0_8")
Popular styles: classic ggplot fivethirtyeight seaborn-v0_8-dark grayscale
■ INTERACTION (ZOOM, PAN)
%matplotlib inline
%matplotlib notebook
■ ADVANCED CONCEPTS (FULL UNDERSTANDING)
1■■ Artists
Everything you see on the plot (lines, text, patches) is an artist object.
line = [Link](x, y)
You can modify line width, color, marker, transparency.
2■■ Transformations
Matplotlib supports data coordinates, axes coordinates, figure coordinates.
[Link]("Point", xy=(2,3), xytext=(3,5),
arrowprops=dict(arrowstyle="->"))
3■■ Patches (Shapes)
from [Link] import Circle
ax.add_patch(Circle((2,3), radius=0.4))
4■■ Animation
from [Link] import FuncAnimation
5■■ 3D PLOTS
from mpl_toolkits.mplot3d import Axes3D
ax = fig.add_subplot(111, projection='3d')
■ SUMMARY — EVERYTHING YOU NEED TO KNOW Figure = Whole canvas Axes = A single plot
Artists = Everything drawn pyplot = Quick and simple plotting OO approach = Professional control
Subplots = Multiple plots Styles = Visual themes Annotations = Add notes/arrows 3D/animation =
Advanced visualization
■ Additional help available: cheat sheet, exercises, engineering examples, and more.