Matplotlib Masterclass
Introduction to Matplotlib
📌 What is Matplotlib?
Matplotlib is the most popular data visualization library in Python. It was created by John D. Hunter in 2003 and is now maintained by a large community. It produces publication-quality figures in a variety of formats.
🚀 Why Matplotlib?
- Versatile: Line plots, bar charts, scatter plots, histograms, pie charts, and more.
- Customizable: Full control over every element — colors, labels, fonts, axes, legends.
- Foundation: Seaborn, Pandas plotting, and other libraries are built on top of Matplotlib.
- Export: Save charts as PNG, JPG, SVG, PDF — perfect for reports and papers.
🛠️ Setup
# Install (if not already installed)
# pip install matplotlib
import matplotlib.pyplot as plt
import numpy as np
print("Matplotlib ready! ✅")
Convention: We always import matplotlib.pyplot as plt. This shorthand is the universal standard.
Plotting Basics
📌 The plot() Function
The plot() function draws points (markers) in a diagram. By default, it connects them with a line. It takes two arrays: x-points and y-points.
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([0, 6])
ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints)
plt.show()
📌 Plotting Without X-Points
If you only supply one array, Matplotlib uses the index (0, 1, 2, 3...) as the x-axis.
ypoints = np.array([3, 8, 1, 10, 5, 7])
plt.plot(ypoints)
plt.show()
📌 Multiple Lines
Call plt.plot() multiple times before plt.show() to draw multiple lines on the same figure.
x = np.array([0, 1, 2, 3])
y1 = np.array([3, 8, 1, 10])
y2 = np.array([6, 2, 7, 11])
plt.plot(x, y1)
plt.plot(x, y2)
plt.show()
Markers & Lines
📌 Markers
Use the marker keyword to emphasize each data point with a symbol.
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o') # Circle marker
plt.show()
📌 Common Marker Characters
'o'— Circle'*'— Star'.'— Point','— Pixel'x'— X mark'+'— Plus's'— Square'D'— Diamond'^'— Triangle up
📌 Format Strings (fmt)
Shortcut syntax: 'marker|line|color'. Example: 'o:r' = circle markers, dotted line, red color.
plt.plot(ypoints, 'o:r') # Circle, dotted, red
plt.show()
📌 Line Styles
Use linestyle (or ls) to change the line style.
'solid'or'-'— Default solid line'dotted'or':'— Dotted'dashed'or'--'— Dashed'dashdot'or'-.'— Dash-dot
plt.plot(ypoints, linestyle = 'dashed', color = 'hotpink', linewidth = 3)
plt.show()
📌 Line Color
Use color (or c). Accepts named colors ('red'), hex ('#FF5733'), or short form ('r', 'g', 'b').
Labels & Title
📌 Adding Context
Always label your axes and add a title! It makes your graphs readable and professional.
x = np.array([80, 85, 90, 95, 100, 105, 110])
y = np.array([240, 250, 260, 270, 280, 290, 300])
plt.plot(x, y)
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.title("Sports Watch Data")
plt.show()
📌 Custom Font Properties
Use a fontdict dictionary to customize fonts.
font_title = {'family':'serif', 'color':'blue', 'size':20}
font_label = {'family':'serif', 'color':'darkred', 'size':15}
plt.title("Sports Watch Data", fontdict = font_title)
plt.xlabel("Average Pulse", fontdict = font_label)
plt.ylabel("Calorie Burnage", fontdict = font_label)
📌 Title Positioning
Use the loc parameter: 'left', 'right', or 'center' (default).
plt.title("My Title", loc = 'left')
Grid
📌 Adding Grid Lines
Use plt.grid() to add grid lines. You can control which axis gets lines.
x = np.array([80, 85, 90, 95, 100])
y = np.array([240, 250, 260, 270, 280])
plt.plot(x, y)
plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.grid() # Add grid
plt.show()
📌 Specify Axis
plt.grid(axis = 'x')— Only vertical grid linesplt.grid(axis = 'y')— Only horizontal grid linesplt.grid(axis = 'both')— Both (default)
📌 Grid Line Properties
plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)
Subplots
📌 Display Multiple Plots
The subplot() function takes three arguments: rows, columns, and index (position).
import matplotlib.pyplot as plt
import numpy as np
# Plot 1
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1) # 1 row, 2 cols, position 1
plt.plot(x, y)
plt.title("Plot 1")
# Plot 2
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2) # 1 row, 2 cols, position 2
plt.plot(x, y)
plt.title("Plot 2")
plt.suptitle("My Subplots")
plt.show()
📌 Super Title
Use plt.suptitle() to add a title for the entire figure (above all subplots).
📌 2x2 Grid Layout
plt.subplot(2, 2, 1) # Top-left
plt.subplot(2, 2, 2) # Top-right
plt.subplot(2, 2, 3) # Bottom-left
plt.subplot(2, 2, 4) # Bottom-right
Scatter Plots
📌 Compare Two Variables
Scatter plots are used to observe relationships between two numeric variables. Each dot represents one observation.
x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11])
y = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78])
plt.scatter(x, y)
plt.xlabel("Car Age")
plt.ylabel("Speed")
plt.title("Car Age vs Speed")
plt.show()
📌 Color, Size & Transparency
Customize with color, s (size), and alpha (transparency).
colors = np.array([0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
sizes = np.array([20, 50, 100, 200, 500, 1000, 60, 90, 10, 300])
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='viridis')
plt.colorbar() # Shows the color scale
plt.show()
📌 Colormaps
Popular colormaps: 'viridis', 'plasma', 'inferno', 'coolwarm', 'RdYlGn'.
Bar Charts
📌 Vertical Bars
Use plt.bar() to draw vertical bar charts. Great for comparing categorical data.
x = np.array(["Apples", "Bananas", "Cherries", "Dates"])
y = np.array([400, 350, 500, 200])
plt.bar(x, y, color = "#4CAF50", width = 0.5)
plt.title("Fruit Sales")
plt.show()
📌 Horizontal Bars
Use plt.barh() for horizontal bars. Use height instead of width.
plt.barh(x, y, color = "hotpink")
plt.show()
📌 Bar Color & Width
color— Set bar color (name, hex, or short form)width— Width of vertical bars (default 0.8)height— Height of horizontal bars (default 0.8)
Histograms
📌 Frequency Distribution
A histogram shows frequency of data within specific ranges (bins). It's ideal for understanding the distribution of continuous data.
x = np.random.normal(170, 10, 250) # 250 values, mean=170, std=10
plt.hist(x, bins=20, color='skyblue', edgecolor='black')
plt.title("Height Distribution")
plt.xlabel("Height (cm)")
plt.ylabel("Frequency")
plt.show()
📌 Key Parameters
bins— Number of bars/ranges (default: 10)color— Fill coloredgecolor— Border color of each baralpha— Transparency (0-1)
Tip: More bins = more detail. Fewer bins = smoother shape. The "right" number depends on your data size.
Pie Charts
📌 Show Proportions
Pie charts are used to display data as proportions of a whole. Each slice represents a category's share.
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
mycolors = ["#ff9999", "#ffcc99", "#99ff99", "#66b3ff"]
plt.pie(y, labels=mylabels, colors=mycolors,
autopct='%1.1f%%', startangle=90)
plt.title("Fruit Popularity")
plt.show()
📌 Key Parameters
labels— List of labels for each slicecolors— List of colors for each sliceautopct— Display percentage on each slicestartangle— Rotate the start (default: 0)explode— Pull out a slice for emphasisshadow— Add a shadow (True/False)
📌 Explode a Slice
myexplode = [0.2, 0, 0, 0] # Pull out Apples
plt.pie(y, labels=mylabels, explode=myexplode, shadow=True)
📌 Legend
plt.legend(title = "Fruits:")
🚀 Real World Projects
🟢 Beginner: Monthly Sales Bar Chart
Goal: Create a bar chart showing monthly sales for a store. Use different colors per bar and add labels.
🟡 Intermediate: Stock Price Line Graph
Goal: Plot daily stock prices over a month. Add grid lines, markers, a title, and axis labels. Overlay a moving average line.
🔴 Advanced: Multi-Metric Dashboard
Goal: Create a 2x2 subplot layout showing 4 different metrics:
- Line chart — Revenue trend
- Bar chart — Sales by category
- Scatter plot — Price vs Quantity
- Pie chart — Market share
🎯 Matplotlib Mini Task
Goal: Visualize Company Growth.
📋 Requirements:
- Create two arrays: Year (2020-2025) and Profit (use your own numbers).
- Plot a line graph with circle markers.
- Add a Title "Company Profit 2020-2025".
- Label both axes.
- Show the grid.
- Bonus: Add a second line for "Expenses" on the same plot.
Paint with data! 🎨