We use plt.figure to create a new drawing space for our charts. It helps us organize and control how our plots look.
Figure creation with plt.figure in Matplotlib
plt.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, tight_layout=False)
num gives the figure a number or name to identify it.
figsize sets the width and height in inches (like size of paper).
plt.figure()
plt.figure(figsize=(8, 6))
plt.figure(num=2, dpi=100)
This code creates a new figure with a size of 6 by 4 inches, draws a simple line plot, adds a title, and then shows the plot.
import matplotlib.pyplot as plt # Create a figure with size 6x4 inches plt.figure(figsize=(6, 4)) # Plot a simple line plt.plot([1, 2, 3], [4, 5, 6]) # Add title plt.title('Simple Line Plot') # Show the plot plt.show()
Calling plt.figure() without arguments creates a new figure with default size.
You can create multiple figures by calling plt.figure() multiple times with different num or figsize.
Use plt.show() to display all created figures.
plt.figure creates a new drawing space for your plots.
You can control size and resolution with figsize and dpi.
Use it to organize multiple plots or customize plot appearance before drawing.
Practice
plt.figure() function do in matplotlib?Solution
Step 1: Understand the role of plt.figure()
Theplt.figure()function creates a new empty figure, which is like a blank canvas for drawing plots.Step 2: Differentiate from other functions
Functions likeplt.plot()draw on the figure,plt.show()displays it, andplt.savefig()saves it. Onlyplt.figure()creates the blank space.Final Answer:
Creates a new blank canvas for plotting graphs -> Option BQuick Check:
plt.figure() = new blank canvas [OK]
- Confusing plt.figure() with plt.plot()
- Thinking plt.figure() displays the plot
- Assuming plt.figure() saves the plot
plt.figure()?Solution
Step 1: Recall the correct parameter name
The parameter to set figure size isfigsize, which takes a tuple of (width, height) in inches.Step 2: Check the syntax of options
Only plt.figure(figsize=(8, 6)) usesfigsize=(8, 6)correctly. Others use wrong parameter names or data types.Final Answer:
plt.figure(figsize=(8, 6)) -> Option DQuick Check:
figsize=(width, height) = plt.figure(figsize=(8, 6)) [OK]
- Using 'size' instead of 'figsize'
- Passing list instead of tuple
- Using wrong parameter names like 'dimensions'
import matplotlib.pyplot as plt fig = plt.figure(figsize=(4, 3), dpi=100) print(fig.get_size_inches()) print(fig.dpi)
Solution
Step 1: Understand figsize and dpi
Thefigsizeis in inches as a float array, sofig.get_size_inches()returns [4. 3.]. The dpi is dots per inch, set to 100.0.Step 2: Check print outputs
Printingfig.get_size_inches()shows floats with decimal points. Printingfig.dpishows 100.0 as set.Final Answer:
[4. 3.] 100.0 -> Option CQuick Check:
figsize floats and dpi number = [4. 3.] and 100.0 [OK]
- Expecting integers instead of floats for size
- Confusing dpi value with size
- Misreading output format
import matplotlib.pyplot as plt fig = plt.figure(size=(5, 4)) plt.plot([1, 2, 3], [4, 5, 6]) plt.show()
Solution
Step 1: Check parameter names in plt.figure()
The correct parameter to set figure size isfigsize, notsize. Usingsizecauses an error.Step 2: Verify other parts of the code
Usingplt.plot()afterplt.figure()is valid.plt.show()has parentheses. Lists have same length.Final Answer:
The parameter 'size' is invalid; should be 'figsize' -> Option AQuick Check:
Use figsize= not size= in plt.figure() [OK]
- Using 'size' instead of 'figsize'
- Thinking plt.plot() can't follow plt.figure()
- Forgetting parentheses in plt.show()
Solution
Step 1: Understand figure creation for multiple plots
Creating two separate figures requires callingplt.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]) missesplt.show().Final Answer:
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() -> Option AQuick Check:
Two plt.figure() calls = two separate plots [OK]
- Using subplots instead of separate figures
- Plotting multiple lines on one figure expecting separate windows
- Forgetting plt.show() to display plots
Ready to prove it?
Pass the test and this pattern moves unknown
