Docsity

Matplotlib; introduction, Summaries of Geometry

Toggle sidebar
bg1
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 matplotlib.pyplot 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 = plt.figure()
2■■ Axes (Subplots)
This is the actual plot area, where you see the graph.
fig, ax = plt.subplots()
fig → whole page ax → one plot (graph)
You can create multiple subplots:
fig, ax = plt.subplots(2, 2)
3■■ The Plotting Functions
Examples:
ax.plot() # line graph
ax.scatter() # scatter plot
ax.bar() # bar chart
ax.hist() # histogram
ax.imshow() # image
ax.pie() # pie chart
Almost everything is built on these.
■ STYLING A PLOT (Very Important)
ax.set_title("Speed vs Time")
ax.set_xlabel("Time (s)")
pf3

Partial preview of the text

Download Matplotlib; introduction and more Summaries Geometry in PDF only on Docsity!

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.

n 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 matplotlib.pyplot as plt

plt works like a drawing board where you create plots.

n 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

n CORE CONCEPTS YOU MUST UNDERSTAND Everything in Matplotlib revolves around 3 core

ideas:

1 nn The Figure

Think of it as the entire canvas. A figure can contain multiple plots.

fig = plt.figure()

2 nn Axes (Subplots)

This is the actual plot area, where you see the graph.

fig, ax = plt.subplots()

fig → whole page ax → one plot (graph)

You can create multiple subplots:

fig, ax = plt.subplots(2, 2)

3 nn The Plotting Functions

Examples:

ax.plot() # line graph ax.scatter() # scatter plot ax.bar() # bar chart ax.hist() # histogram ax.imshow() # image ax.pie() # pie chart

Almost everything is built on these.

n STYLING A PLOT (Very Important)

ax.set_title("Speed vs Time") ax.set_xlabel("Time (s)")

ax.set_ylabel("Speed (m/s)") ax.grid(True) ax.legend() ax.plot(x, y, color='red', linestyle='--', linewidth=2)

n MOST COMMON PLOT TYPES IN MATPLOTLIB

1 nn Line Plot

plt.plot(x, y)

2 nn Scatter Plot

plt.scatter(x, y)

3 nn Bar Plot

plt.bar(categories, values)

4 nn Histogram

plt.hist(values, bins=20)

5 nn Pie Chart

plt.pie(values, labels=labels)

6 nn Heatmap

plt.imshow(data, cmap='hot')

n FIGURE SIZE, DPI & SAVING PLOTS

plt.figure(figsize=(8, 6)) plt.figure(dpi=300) plt.savefig("plot.png")

n THE OBJECT-ORIENTED (OO) APPROACH (Recommended)

fig, ax = plt.subplots() ax.plot(x, y) ax.set_title("Example") plt.show()

n SUBPLOTS (Multiple Plots)

fig, ax = plt.subplots(2, 2) ax[0, 0].plot(x, y) ax[1, 1].scatter(a, b)

n STYLES & THEMES

plt.style.use("seaborn-v0_8")

Popular styles: classic ggplot fivethirtyeight seaborn-v0_8-dark grayscale

n INTERACTION (ZOOM, PAN)