[ SKILL_DOCUMENTATION ]
# Matplotlib 图表类型指南
Matplotlib 中不同图表类型的综合指南,包含示例和使用场景。
## 1. 折线图
**使用场景:** 时间序列、连续数据、趋势、函数可视化
### 基础折线图
python
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, y, linewidth=2, label='Data')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.legend()
### 多条折线
python
ax.plot(x, y1, label='Dataset 1', linewidth=2)
ax.plot(x, y2, label='Dataset 2', linewidth=2, linestyle='--')
ax.plot(x, y3, label='Dataset 3', linewidth=2, linestyle=':')
ax.legend()
### 带标记的折线
python
ax.plot(x, y, marker='o', markersize=8, linestyle='-',
linewidth=2, markerfacecolor='red', markeredgecolor='black')
### 阶梯图
python
ax.step(x, y, where='mid', linewidth=2, label='Step function')
# where 选项: 'pre', 'post', 'mid'
### 误差棒图
python
ax.errorbar(x, y, yerr=error, fmt='o-', linewidth=2,
capsize=5, capthick=2, label='With uncertainty')
## 2. 散点图
**使用场景:** 相关性、变量间关系、聚类、异常值
### 基础散点图
python
ax.scatter(x, y, s=50, alpha=0.6)
### 大小和颜色映射散点图
python
scatter = ax.scatter(x, y, s=sizes*100, c=colors,
cmap='viridis', alpha=0.6, edgecolors='black')
plt.colorbar(scatter, ax=ax, label='Color variable')
### 分类散点图
python
for category in categories:
mask = data['category'] == category
ax.scatter(data[mask]['x'], data[mask]['y'],
label=category, s=50, alpha=0.7)
ax.legend()
## 3. 柱状图
**使用场景:** 分类比较、离散数据、计数
### 垂直柱状图
python
ax.bar(categories, values, color='steelblue',
edgecolor='black', linewidth=1.5)
ax.set_ylabel('Values')
### 水平柱状图
python
ax.barh(categories, values, color='coral',
edgecolor='black', linewidth=1.5)
ax.set_xlabel('Values')
### 分组柱状图
python
x = np.arange(len(categories))
width = 0.35
ax.bar(x - width/2, values1, width, label='Group 1')
ax.bar(x + width/2, values2, width, label='Group 2')
ax.set_xticks(x)
ax.set_xticklabels(categories)
ax.legend()
### 堆叠柱状图
python
ax.bar(categories, values1, label='Part 1')
ax.bar(categories, values2, bottom=values1, label='Part 2')
ax.bar(categories, values3, bottom=values1+values2, label='Part 3')
ax.legend()
### 柱状