Matplotlib figure() 函数
figure() 用于创建新的 Figure(画布)或激活已有的 Figure。
Figure 是所有绘图的顶层容器,管理整个图表的尺寸、DPI 和所有子图。
函数定义
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)
参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
| num | int 或 str | Figure 标识符。整数为编号,字符串为标签。若不存在则创建新的,存在则激活已有 |
| figsize | tuple (w, h) | Figure 尺寸(英寸),如 (8, 6)。默认由 rcParams 决定 |
| dpi | float | 分辨率(每英寸点数),如 100。影响像素输出尺寸 |
| facecolor | color | Figure 背景色 |
| edgecolor | color | Figure 边框颜色 |
| frameon | bool | 是否显示 Figure 背景框,默认 True |
| clear | bool | 若 num 已存在,是否先清空再使用 |
| layout | str 或 LayoutEngine | 推荐 布局引擎:'constrained'、'compressed'、'tight'、'none' |
日常使用时很少直接调用
figure(),更常用subplots()一次性创建 Figure 和 Axes。单独使用 figure() 主要用于多 Figure 场景或自定义画布属性。
使用示例
示例 1:创建自定义尺寸的 Figure
实例
import matplotlib.pyplot as plt
import numpy as np
# 创建指定尺寸和高 DPI 的 Figure
fig = plt.figure(figsize=(10, 4), dpi=100,
facecolor='#f8f9fa',
layout='constrained')
# 手动添加子图
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()
import numpy as np
# 创建指定尺寸和高 DPI 的 Figure
fig = plt.figure(figsize=(10, 4), dpi=100,
facecolor='#f8f9fa',
layout='constrained')
# 手动添加子图
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()
示例 2:多 Figure 管理
实例
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
# 创建 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)')
# 创建 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)')
# 激活 Figure #1 并继续操作
plt.figure(1)
plt.plot(x, np.sin(2*x), '--', alpha=0.5)
# 保存特定 Figure
fig1.savefig('runoob_fig1.png', dpi=150)
fig2.savefig('runoob_fig2.png', dpi=150)
print("runoob: two figures saved")
plt.show()
import numpy as np
x = np.linspace(0, 10, 100)
# 创建 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)')
# 创建 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)')
# 激活 Figure #1 并继续操作
plt.figure(1)
plt.plot(x, np.sin(2*x), '--', alpha=0.5)
# 保存特定 Figure
fig1.savefig('runoob_fig1.png', dpi=150)
fig2.savefig('runoob_fig2.png', dpi=150)
print("runoob: two figures saved")
plt.show()
示例 3:layout 参数自动布局
实例
import matplotlib.pyplot as plt
import numpy as np
# 对比 layout 设置
# 不使用 layout(可能标签重叠)
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')
# 使用 layout='constrained'(自动处理重叠)
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("runoob: compare layout settings")
import numpy as np
# 对比 layout 设置
# 不使用 layout(可能标签重叠)
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')
# 使用 layout='constrained'(自动处理重叠)
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("runoob: compare layout settings")
常见问题
figsize 的尺寸单位是什么?
英寸(inches)。实际像素尺寸 = figsize × dpi。例如 figsize=(8,6), dpi=100 生成 800×600 像素的图像。
layout 哪个选项最好?
'constrained' 是多数场景的推荐选项,自动处理子图和标签间距。
'compressed' 更进一步压缩边距,适合需要最大化数据区域的场景。
'none' 不使用自动布局(默认),可通过 subplots_adjust() 手动调整。

Matplotlib 参考文档