📖 Scenario: You are working on a simple data visualization project. You want to understand how Figure and Axes work together in matplotlib to create plots.Think of the Figure as a blank canvas or a picture frame, and the Axes as the area inside the frame where you draw your graph.
🎯 Goal: Build a basic plot using matplotlib by creating a Figure and adding an Axes to it. Then plot some simple data on the Axes.
📋 What You'll Learn
Create a Figure object using plt.figure()
Add an Axes object to the Figure using fig.add_subplot()
Plot a simple line graph on the Axes using ax.plot()
Display the plot using plt.show()
💡 Why This Matters
🌍 Real World
Understanding the Figure and Axes model helps you create clear and organized visualizations for data analysis and presentations.
💼 Career
Data scientists and analysts use matplotlib's Figure and Axes to build professional charts and graphs that communicate insights effectively.
Progress0 / 4 steps
1
Create a Figure object
Write code to create a Figure object called fig using plt.figure().
Matplotlib
Hint
Use plt.figure() to create a new Figure object and assign it to fig.
2
Add an Axes to the Figure
Add an Axes object called ax to the Figurefig using fig.add_subplot(1, 1, 1).
Matplotlib
Hint
Use fig.add_subplot(1, 1, 1) to add a single Axes to the Figure and assign it to ax.
3
Plot data on the Axes
Use the ax.plot() method to plot the data points [1, 2, 3] on the x-axis and [4, 5, 6] on the y-axis.
Matplotlib
Hint
Call ax.plot() with the x and y data lists as arguments.
4
Display the plot
Use plt.show() to display the plot you created.
Matplotlib
Hint
Call plt.show() to open a window showing your plot.
Practice
(1/5)
1. What does the Figure represent in matplotlib's Figure and Axes model?
easy
A. A single data point on the plot
B. The area where data points are drawn
C. The entire image or window that contains all plots
D. The color scheme of the plot
Solution
Step 1: Understand the role of Figure
The Figure is the whole container or window that holds one or more plots.
Step 2: Differentiate Figure from Axes
Axes is the part inside the Figure where data is drawn, not the entire window.
Final Answer:
The entire image or window that contains all plots -> Option C
Quick Check:
Figure = whole window [OK]
Hint: Figure is the whole plot window, not just the data area [OK]
Common Mistakes:
Confusing Figure with Axes
Thinking Figure is a single plot line
Mixing Figure with plot colors
2. Which of the following is the correct way to create a Figure and Axes in matplotlib?
easy
A. fig, ax = plt.subplots()
B. fig = plt.figure(), ax = plt.plot()
C. ax = plt.figure(), fig = plt.axes()
D. plt.create_figure(), plt.create_axes()
Solution
Step 1: Recall matplotlib syntax
The standard way to create Figure and Axes together is using plt.subplots().
Step 2: Check each option
fig, ax = plt.subplots() correctly assigns Figure and Axes from plt.subplots(). Others misuse functions or assign incorrectly.
Final Answer:
fig, ax = plt.subplots() -> Option A
Quick Check:
Use plt.subplots() to get Figure and Axes [OK]
Hint: Use plt.subplots() to get both Figure and Axes [OK]
Common Mistakes:
Using plt.plot() to create Axes
Confusing plt.figure() with plt.subplots()
Trying non-existent functions like plt.create_figure()
3. What will be the output type of the variable ax after running this code?
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
medium
A. A matplotlib Figure object
B. A matplotlib Axes object
C. A list of Axes objects
D. A NumPy array
Solution
Step 1: Understand plt.subplots() return values
It returns a tuple: (Figure object, Axes object or array of Axes).
Step 2: Check variable assignment
Here, ax is assigned the second element, which is a single Axes object.
A. plt.axes() should be called on the Figure object
B. plt.axes() creates a new Figure, not Axes
C. plt.figure() must be called after plt.axes()
D. No error, code runs fine
Solution
Step 1: Understand plt.figure() and plt.axes()
plt.figure() creates a Figure and sets it as current. plt.axes() creates Axes in the current Figure using default positioning.
Step 2: Verify code execution
The plot is correctly added to ax and plt.show() displays the figure. The code runs without errors.
Final Answer:
No error, code runs fine -> Option D
Quick Check:
plt.axes() adds to current Figure [OK]
Hint: plt.figure() sets current figure for plt.axes() [OK]
Common Mistakes:
Calling plt.axes() without linking to Figure
Assuming plt.axes() auto-links to last Figure
Calling plt.figure() after plt.axes()
5. You want to create a figure with two side-by-side plots sharing the same y-axis. Which code correctly uses the Figure and Axes model to achieve this?