R
Rishtaara
Python: Zero to Professional
Lesson 20 of 40Article16 min

Matplotlib Intro, Pyplot & Plotting

Matplotlib draws charts in Python — line, bar, scatter, histogram, pie. Install: pip install matplotlib.

Matplotlib Intro

Matplotlib draws charts in Python — line, bar, scatter, histogram, pie. Install: pip install matplotlib.

It is the standard plotting library paired with NumPy and Pandas.

Real-life example: Matplotlib is graph paper that draws itself — you give numbers, it sketches the picture.

Matplotlib Pyplot

Import as import matplotlib.pyplot as plt. plt.plot(), plt.show() display charts.

Works in scripts and Jupyter notebooks.

Real-life example: pyplot is the remote for your chart TV — plot, label, then show.

First plot
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [2, 4, 1, 5]
plt.plot(x, y)
plt.show()

Matplotlib Plotting & Markers

Customize lines with color, linestyle, linewidth. Markers: 'o', 's', '^' show points.

plt.plot(x, y, marker='o', color='green', linestyle='--')

Real-life example: Markers are pins on a map — the line is the road, pins show stops.

Markers and styles
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [3, 2, 5], marker="o", color="blue")
plt.show()

Matplotlib Line

Multiple plt.plot() calls overlay lines. Use legend() to label each series.

Line charts show trends over time — sales, temperature, scores.

Real-life example: Multiple lines on one chart compare two students' progress on the same exam timeline.

Two lines
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [1, 4, 9], label="A")
plt.plot([1, 2, 3], [2, 3, 8], label="B")
plt.legend()
plt.show()