{
  "id": "python/matplotlib-colorbar-mappable-error",
  "signature": "ValueError: Unable to determine Axes to steal space for Colorbar. Either provide the *cax* argument or pass *ax* or *axes* to the colorbar() method.",
  "signature_zh": "ValueError: 无法确定为 Colorbar 窃取空间的 Axes。请提供 *cax* 参数，或将 *ax* 或 *axes* 传递给 colorbar() 方法。",
  "regex": "ValueError:\\ Unable\\ to\\ determine\\ Axes\\ to\\ steal\\ space\\ for\\ Colorbar\\.\\ Either\\ provide\\ the\\ \\*cax\\*\\ argument\\ or\\ pass\\ \\*ax\\*\\ or\\ \\*axes\\*\\ to\\ the\\ colorbar\\(\\)\\ method\\.",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Calling plt.colorbar() with no current image/mappable or after clearing the axes, so matplotlib has no parent axes to attach the colorbar to.",
  "root_cause_type": "generic",
  "root_cause_zh": "在没有当前图像/mappable 或清空坐标轴后调用 plt.colorbar()，matplotlib 没有父坐标轴可附加色条。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "There is no mappable yet, so the same ValueError is raised.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The fig keyword does not tell matplotlib which axes to steal space from.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.96,
      "how": "im = ax.imshow(data)\nfig.colorbar(im, ax=ax)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.93,
      "how": "fig, (ax, cax) = plt.subplots(1, 2, gridspec_kw={'width_ratios':[4,1]})\nim = ax.imshow(data)\nfig.colorbar(im, cax=cax)",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-07-22",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}