By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
A Hyper-Practical, Zero-Fluff Study Guide for Data Scientists
Matplotlib is the foundation of Python data visualization. If you’ve ever generated a plot in pandas (df.plot()), you’ve used Matplotlib under the hood. But when you need custom layouts, multiple subplots, or fine-grained styling, you must work directly with Matplotlib’s core objects: Figure and Axes.
df.plot()
Figure
Axes
You’re building a real-time monitoring dashboard for a logistics company. The dashboard must show: 1. A line plot of daily package volumes (2020–2024).2. A bar plot of delivery delays by region.3. A pie chart of shipping method distribution.
Problem: If you use plt.plot() for each chart, they’ll stack vertically in one figure, wasting space. If you use subplots incorrectly, the pie chart will stretch into an oval. Solution: Master Figure, Axes, and subplots to control layout precisely.
plt.plot()
plt.close()
Subplot
plt.subplots()
plt.subplot()
tight_layout()
style
ggplot
seaborn
plt.style.use('seaborn')
savefig()
dpi=300
rcParams
matplotlib
pandas
bash pip install matplotlib pandas
Create a 3-panel dashboard with: 1. A line plot of daily sales (2020–2024).2. A bar plot of sales by region.3. A pie chart of product categories.
import pandas as pd import numpy as np import matplotlib.pyplot as plt # Generate synthetic data np.random.seed(42) dates = pd.date_range("2020-01-01", "2024-12-31", freq="D") sales = np.cumsum(np.random.normal(100, 10, len(dates))) + 1000 regions = ["North", "South", "East", "West"] region_sales = {r: np.random.randint(500, 2000) for r in regions} categories = ["Electronics", "Clothing", "Furniture", "Groceries"] category_sales = [35, 25, 20, 20] # Create DataFrames df_sales = pd.DataFrame({"Date": dates, "Sales": sales}) df_regions = pd.DataFrame({"Region": regions, "Sales": region_sales.values()}) df_categories = pd.DataFrame({"Category": categories, "Sales": category_sales})
# Create a 2x2 grid of subplots (we'll use 3 of them) fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(12, 8)) # Flatten axes for easy iteration (axes is a 2x2 array) axes = axes.flatten() # Line plot (top-left) axes[0].plot(df_sales["Date"], df_sales["Sales"], color="tab:blue") axes[0].set_title("Daily Sales (2020–2024)") axes[0].set_xlabel("Date") axes[0].set_ylabel("Sales ($)") # Bar plot (top-right) axes[1].bar(df_regions["Region"], df_regions["Sales"], color="tab:orange") axes[1].set_title("Sales by Region") axes[1].set_ylabel("Sales ($)") # Pie chart (bottom-left) axes[2].pie( df_categories["Sales"], labels=df_categories["Category"], autopct="%1.1f%%", startangle=90, colors=["tab:green", "tab:red", "tab:purple", "tab:brown"] ) axes[2].set_title("Sales by Category") # Hide the unused subplot (bottom-right) axes[3].axis("off") # Adjust layout to prevent overlap plt.tight_layout() # Save the figure plt.savefig("sales_dashboard.png", dpi=300, bbox_inches="tight") plt.show()
(Example output; actual plot will vary slightly.)
sales_dashboard.png
bash ls -lh sales_dashboard.png # Should be ~200–500 KB
plt.close(fig)
figsize
figsize=(width, height)
(10, 6)
plt.subplots_adjust()
python plt.rcParams.update({ "font.size": 12, "axes.titlesize": 14, "axes.labelsize": 12, "xtick.labelsize": 10, "ytick.labelsize": 10, "figure.titlesize": 16, "figure.figsize": (10, 6) })
plt.style.use()
tab10
bbox_inches="tight"
for ax in axes:
ax.clear()
fig, axes = plt.subplots()
axes[i]
plt.tight_layout()
plt.show()
plt
ax
ax.plot()
ax.bar()
dpi
autopct
pctdistance
fig, axes = plt.subplots(2, 3, figsize=(12, 8))
"How do you hide an unused subplot?" Answer: axes[5].axis("off").
axes[5].axis("off")
Styling:
plt.style.use("ggplot")
"How do you set the default font size for all plots?" Answer: plt.rcParams["font.size"] = 12.
plt.rcParams["font.size"] = 12
Saving Figures:
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
"You need to create a dashboard with 4 plots (2 rows, 2 columns). The bottom-right plot is unused. How do you hide it?" Answer:
fig, axes = plt.subplots(2, 2, figsize=(10, 8)) axes[1, 1].axis("off") # Hide the bottom-right subplot
Create a 2x2 subplot grid where: 1. Top-left: Line plot of y = sin(x) (0 to 2π).2. Top-right: Scatter plot of y = cos(x) vs. x.3. Bottom-left: Histogram of 1000 random normal values.4. Bottom-right: Hide the subplot.
y = sin(x)
y = cos(x)
x
Constraints: - Use plt.subplots().- Apply the seaborn style.- Save the figure as trig_plots.png (300 DPI).
trig_plots.png
import numpy as np import matplotlib.pyplot as plt # Apply style plt.style.use("seaborn") # Create subplots fig, axes = plt.subplots(2, 2, figsize=(10, 8)) x = np.linspace(0, 2 * np.pi, 100) # Top-left: sin(x) axes[0, 0].plot(x, np.sin(x), color="tab:blue") axes[0, 0].set_title("sin(x)") # Top-right: cos(x) scatter axes[0, 1].scatter(x, np.cos(x), color="tab:orange", s=10) axes[0, 1].set_title("cos(x)") # Bottom-left: histogram axes[1, 0].hist(np.random.normal(0, 1, 1000), bins=30, color="tab:green") axes[1, 0].set_title("Normal Distribution") # Hide bottom-right axes[1, 1].axis("off") # Adjust layout and save plt.tight_layout() plt.savefig("trig_plots.png", dpi=300, bbox_inches="tight") plt.show()
Why It Works: - plt.subplots(2, 2) creates a 2x2 grid.- axes[i, j] accesses subplots by row/column.- axis("off") hides the unused subplot.- tight_layout() prevents label overlap.
plt.subplots(2, 2)
axes[i, j]
axis("off")
plt.subplots(nrows, ncols)
fig, axes = plt.subplots(2, 2)
ax.plot(x, y, color="red")
ax.set_title()
ax.set_title("Sales")
plt.style.use("seaborn")
plt.savefig()
bbox_inches
ax.axis("off")
plt.rcParams
figsize=(10, 6)
⚠️ Default DPI
dpi=100
⚠️ Subplot indexing
axes[0, 1]
⚠️
vs.
methods
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.