python
runtime_error
ai_generated
true
ValueError: 无法确定为 Colorbar 窃取空间的 Axes。请提供 *cax* 参数,或将 *ax* 或 *axes* 传递给 colorbar() 方法。
ValueError: Unable to determine Axes to steal space for Colorbar. Either provide the *cax* argument or pass *ax* or *axes* to the colorbar() method.
ID: python/matplotlib-colorbar-mappable-error
80%修复率
88%置信度
0证据数
2024-07-22首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
在没有当前图像/mappable 或清空坐标轴后调用 plt.colorbar(),matplotlib 没有父坐标轴可附加色条。
English
Calling plt.colorbar() with no current image/mappable or after clearing the axes, so matplotlib has no parent axes to attach the colorbar to.
解决方案
-
96% 成功率
im = ax.imshow(data) fig.colorbar(im, ax=ax)
-
93% 成功率
fig, (ax, cax) = plt.subplots(1, 2, gridspec_kw={'width_ratios':[4,1]}) im = ax.imshow(data) fig.colorbar(im, cax=cax)
无效尝试
常见但无效的做法:
-
95% 失败
There is no mappable yet, so the same ValueError is raised.
-
80% 失败
The fig keyword does not tell matplotlib which axes to steal space from.