📖 Scenario: You are working on a simple data visualization project. You want to create a blank figure using matplotlib to prepare for adding plots later.
🎯 Goal: Create a blank figure using plt.figure with a specific size.
📋 What You'll Learn
Create a figure using plt.figure
Set the figure size to 6 inches wide and 4 inches tall
Store the figure in a variable called fig
Print the figure object to see its details
💡 Why This Matters
🌍 Real World
Creating figures is the first step in data visualization to prepare a canvas for charts and graphs.
💼 Career
Data scientists and analysts often create and customize figures to present data clearly and attractively.
Progress0 / 4 steps
1
Import matplotlib and create a blank figure
Import matplotlib.pyplot as plt. Then create a blank figure using plt.figure() and store it in a variable called fig.
Matplotlib
Hint
Use import matplotlib.pyplot as plt to import the plotting library.
Use fig = plt.figure() to create a blank figure.
2
Set the figure size
Modify the plt.figure() call to set the figure size to 6 inches wide and 4 inches tall using the figsize parameter.
Matplotlib
Hint
Use figsize=(6, 4) inside plt.figure() to set the size.
3
Add a title to the figure
Use the fig.suptitle() method to add the title 'My Blank Figure' to the figure.
Matplotlib
Hint
Use fig.suptitle('My Blank Figure') to add a title to the figure.
4
Print the figure object
Print the variable fig to display the figure object details.
Matplotlib
Hint
Use print(fig) to show the figure object details.
Practice
(1/5)
1. What does the plt.figure() function do in matplotlib?
easy
A. Plots a line graph directly
B. Creates a new blank canvas for plotting graphs
C. Saves the current plot to a file
D. Displays the plot on the screen
Solution
Step 1: Understand the role of plt.figure()
The plt.figure() function creates a new empty figure, which is like a blank canvas for drawing plots.
Step 2: Differentiate from other functions
Functions like plt.plot() draw on the figure, plt.show() displays it, and plt.savefig() saves it. Only plt.figure() creates the blank space.
Final Answer:
Creates a new blank canvas for plotting graphs -> Option B
Quick Check:
plt.figure() = new blank canvas [OK]
Hint: Remember: figure = blank canvas for plots [OK]
Common Mistakes:
Confusing plt.figure() with plt.plot()
Thinking plt.figure() displays the plot
Assuming plt.figure() saves the plot
2. Which of the following is the correct way to create a figure with size 8 inches wide and 6 inches tall using plt.figure()?
easy
A. plt.figure(size=(8, 6))
B. plt.figure(dimensions=(8, 6))
C. plt.figure(size=[8, 6])
D. plt.figure(figsize=(8, 6))
Solution
Step 1: Recall the correct parameter name
The parameter to set figure size is figsize, which takes a tuple of (width, height) in inches.
Step 2: Check the syntax of options
Only plt.figure(figsize=(8, 6)) uses figsize=(8, 6) correctly. Others use wrong parameter names or data types.
A. The parameter 'size' is invalid; should be 'figsize'
B. plt.plot() cannot be used after plt.figure()
C. plt.show() is missing parentheses
D. The lists in plt.plot() have different lengths
Solution
Step 1: Check parameter names in plt.figure()
The correct parameter to set figure size is figsize, not size. Using size causes an error.
Step 2: Verify other parts of the code
Using plt.plot() after plt.figure() is valid. plt.show() has parentheses. Lists have same length.
Final Answer:
The parameter 'size' is invalid; should be 'figsize' -> Option A
Quick Check:
Use figsize= not size= in plt.figure() [OK]
Hint: Use figsize=, not size=, to set figure size [OK]
Common Mistakes:
Using 'size' instead of 'figsize'
Thinking plt.plot() can't follow plt.figure()
Forgetting parentheses in plt.show()
5. You want to create two separate plots in two separate windows. Which code correctly creates two figures and plots a simple line in each?
hard
A. fig1 = plt.figure(figsize=(4,3))
plt.plot([1,2,3],[4,5,6])
fig2 = plt.figure(figsize=(4,3))
plt.plot([1,2,3],[6,5,4])
plt.show()
B. plt.figure(figsize=(8,3))
plt.subplot(1,2,1)
plt.plot([1,2,3],[4,5,6])
plt.subplot(1,2,2)
plt.plot([1,2,3],[6,5,4])
plt.show()
C. fig = plt.figure(figsize=(8,3))
plt.plot([1,2,3],[4,5,6])
plt.plot([1,2,3],[6,5,4])
plt.show()
D. plt.figure(figsize=(4,3))
plt.plot([1,2,3],[4,5,6])
plt.figure(figsize=(4,3))
plt.plot([1,2,3],[6,5,4])
Solution
Step 1: Understand figure creation for multiple plots
Creating two separate figures requires calling plt.figure() twice, each time creating a new blank canvas.
Step 2: Check code correctness
fig1 = plt.figure(figsize=(4,3))
plt.plot([1,2,3],[4,5,6])
fig2 = plt.figure(figsize=(4,3))
plt.plot([1,2,3],[6,5,4])
plt.show() creates two figures with specified sizes and plots on each before showing. plt.figure(figsize=(8,3))
plt.subplot(1,2,1)
plt.plot([1,2,3],[4,5,6])
plt.subplot(1,2,2)
plt.plot([1,2,3],[6,5,4])
plt.show() uses subplots in one figure, not two figures. fig = plt.figure(figsize=(8,3))
plt.plot([1,2,3],[4,5,6])
plt.plot([1,2,3],[6,5,4])
plt.show() plots both lines on one figure. plt.figure(figsize=(4,3))
plt.plot([1,2,3],[4,5,6])
plt.figure(figsize=(4,3))
plt.plot([1,2,3],[6,5,4]) misses plt.show().