# ValueError: 无法确定为 Colorbar 窃取空间的 Axes。请提供 *cax* 参数，或将 *ax* 或 *axes* 传递给 colorbar() 方法。

- **ID:** `python/matplotlib-colorbar-mappable-error`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在没有当前图像/mappable 或清空坐标轴后调用 plt.colorbar()，matplotlib 没有父坐标轴可附加色条。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (96% 成功率)
   ```
   im = ax.imshow(data)
fig.colorbar(im, ax=ax)
   ```
2. **** (93% 成功率)
   ```
   fig, (ax, cax) = plt.subplots(1, 2, gridspec_kw={'width_ratios':[4,1]})
im = ax.imshow(data)
fig.colorbar(im, cax=cax)
   ```

## 无效尝试

- **** — There is no mappable yet, so the same ValueError is raised. (95% 失败率)
- **** — The fig keyword does not tell matplotlib which axes to steal space from. (80% 失败率)
