Matplotlib Ref Figure
* * Matplotlib Reference](#)
`figure()` is used to create a new Figure (canvas) or activate an existing Figure.
Figure is the top-level container for all plotting, managing the entire chart's size, DPI, and all subplots.
## Function Definition
matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, *, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, layout=None, **kwargs)
## Parameter Description
| Parameter | Type | Description |
| --- | --- | --- |
| num | int or str | Figure identifier. Integer for number, string for label. Creates new if doesn't exist, activates existing if present |
| figsize | tuple (w, h) | Figure size (inches), e.g. (8, 6). Default determined by rcParams |
| dpi | float | Resolution (dots per inch), e.g. 100. Affects pixel output size |
| facecolor | color | Figure background color |
| edgecolor | color | Figure border color |
| frameon | bool | Whether to display Figure background frame, default True |
| clear | bool | If num already exists, whether to clear before use |
| layout | str or LayoutEngine | Recommended layout engines: 'constrained', 'compressed', 'tight', 'none' |
> In daily use, `figure()` is rarely called directly; `subplots()` is more commonly used to create Figure and Axes at once. Using figure() alone is mainly for multi-Figure scenarios or custom canvas properties.
* * *
## Usage Examples
### Example 1: Creating a Figure with Custom Size
## Example
import matplotlib.pyplot as plt
import numpy as np
# Create a Figure with specified size and high DPI
fig = plt.figure(figsize=(10,4), dpi=100,
facecolor='#f8f9fa',
layout='constrained')
# Manually add subplots
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
x = np.linspace(0,10,100)
ax1.plot(x, np.sin(x),'steelblue', linewidth=2)
ax1.set_title('Plot 1')
ax2.plot(x, np.cos(x),'coral', linewidth=2)
ax2.set_title('Plot 2')
plt.show()
### Example 2: Multiple Figure Management
## Example
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,10,100)
# Create Figure #1
fig1 = plt.figure(1, figsize=(6,4))
ax1 = fig1.subplots()
ax1.plot(x, np.sin(x),'blue')
ax1.set_title('Figure 1: sin(x)')
# Create Figure #2
fig2 = plt.figure(2, figsize=(6,4))
ax2 = fig2.subplots()
ax2.plot(x, np.cos(x),'red')
ax2.set_title('Figure 2: cos(x)')
# Activate Figure #1 and continue operations
plt.figure(1)
plt.plot(x, np.sin(2*x),'--', alpha=0.5)
# Save specific Figure
fig1.savefig('tutorial_fig1.png', dpi=150)
fig2.savefig('tutorial_fig2.png', dpi=150)
print("tutorial: two figures saved")
plt.show()
### Example 3: Automatic Layout with layout Parameter
## Example
import matplotlib.pyplot as plt
import numpy as np
# Compare layout settings
# Without layout (labels may overlap)
fig1 = plt.figure(1, figsize=(8,4))
for i in range(3):
ax = fig1.add_subplot(1,3, i+1)
ax.plot(np.random.randn(50).cumsum())
ax.set_title(f'Plot {i+1}')
ax.set_xlabel('A very long x label')
# Using layout='constrained' (automatically handles overlap)
fig2 = plt.figure(2, figsize=(8,4), layout='constrained')
for i in range(3):
ax = fig2.add_subplot(1,3, i+1)
ax.plot(np.random.randn(50).cumsum())
ax.set_title(f'Plot {i+1}')
ax.set_xlabel('A very long x label')
plt.figure(1)
plt.suptitle('Without layout', y=1.02)
plt.figure(2)
plt.suptitle('With layout="constrained"', y=1.02)
plt.show()
print("tutorial: compare layout settings")
* * *
## Frequently Asked Questions
### What is the unit of measurement for figsize?
Inches. Actual pixel size = figsize Γ dpi. For example, `figsize=(8,6), dpi=100` generates an 800Γ600 pixel image.
### Which layout option is best?
`'constrained'` is the recommended option for most scenarios, automatically handling subplot and label spacing.
`'compressed'` further compresses margins, suitable for scenarios requiring maximized data area.
`'none'` does not use automatic layout (default), allowing manual adjustment via subplots_adjust().
[ Matplotlib Reference](#)
YouTip