[ SKILL_DOCUMENTATION ]
# Matplotlib API 参考
本文档提供了最常用的 matplotlib 类和方法的快速参考。
## 核心类
### Figure (画布)
所有图表元素的顶级容器。
**创建:**
python
fig = plt.figure(figsize=(10, 6), dpi=100, facecolor='white')
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 6))
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
**关键方法:**
- `fig.add_subplot(nrows, ncols, index)` - 添加子图
- `fig.add_axes([left, bottom, width, height])` - 在特定位置添加坐标轴
- `fig.savefig(filename, dpi=300, bbox_inches='tight')` - 保存图表
- `fig.tight_layout()` - 调整间距以防止重叠
- `fig.suptitle(title)` - 设置图表总标题
- `fig.legend()` - 创建图表级图例
- `fig.colorbar(mappable)` - 添加颜色条
- `plt.close(fig)` - 关闭图表以释放内存
**关键属性:**
- `fig.axes` - 图表中所有坐标轴的列表
- `fig.dpi` - 每英寸点数的分辨率
- `fig.figsize` - 图表尺寸(宽,高,单位为英寸)
### Axes (坐标轴)
数据可视化的实际绘图区域。
**创建:**
python
fig, ax = plt.subplots() # 单个坐标轴
ax = fig.add_subplot(111) # 替代方法
**绘图方法:**
**折线图:**
- `ax.plot(x, y, **kwargs)` - 折线图
- `ax.step(x, y, where='pre'/'mid'/'post')` - 阶梯图
- `ax.errorbar(x, y, yerr, xerr)` - 误差棒图
**散点图:**
- `ax.scatter(x, y, s=size, c=color, marker='o', alpha=0.5)` - 散点图
**柱状图:**
- `ax.bar(x, height, width=0.8, align='center')` - 垂直柱状图
- `ax.barh(y, width)` - 水平柱状图
**统计图:**
- `ax.hist(data, bins=10, density=False)` - 直方图
- `ax.boxplot(data, labels=None)` - 箱线图
- `ax.violinplot(data)` - 小提琴图
**2D 图:**
- `ax.imshow(array, cmap='viridis', aspect='auto')` - 显示图像/矩阵
- `ax.contour(X, Y, Z, levels=10)` - 等高线图
- `ax.contourf(X, Y, Z, levels=10)` - 填充等高线图
- `ax.pcolormesh(X, Y, Z)` - 伪彩色图
**填充:**
- `ax.fill_between(x, y1, y2, alpha=0.3)` - 曲线间填充
- `ax.fill_betweenx(y, x1, x2)` - 垂直曲线间填充
**文本与注释:**
- `ax.text(x, y, text, fontsize=12)` - 添加文本
- `ax.annotate(text, xy=(x, y), xytext=(x2, y2), arrowprops={})` - 带箭头的注释
**自定义方法:**
**标签与标题:**
- `ax.set_xlabel(label, fontsize=12)` - 设置 x 轴标签
- `ax.set_ylabel(label, fontsize=12)` - 设置 y 轴标签