{
  "id": "python/matplotlib-legend-handle-error",
  "signature": "TypeError: 'Line2D' object is not iterable",
  "signature_zh": "TypeError: 'Line2D'对象不可迭代",
  "regex": "TypeError:\\ 'Line2D'\\ object\\ is\\ not\\ iterable",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "Passing a single line object (not a list) to plt.legend() when it expects a list of handles, or using *args incorrectly.",
  "root_cause_type": "generic",
  "root_cause_zh": "向plt.legend()传递了单个线条对象（而非列表），但该函数期望句柄列表，或错误地使用了*args。",
  "versions": [
    {
      "version": "3.7",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Wrapping the single object in a list but still getting error",
      "why_fails": "If the list contains other non-handle objects, the error may persist.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using plt.legend() without arguments",
      "why_fails": "This works but may not show the desired legend if labels are not set.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Pass handles as a list",
      "success_rate": 0.95,
      "how": "line, = plt.plot([1,2,3]); plt.legend([line], ['my line'])",
      "condition": "",
      "sources": []
    },
    {
      "action": "Set labels during plot and call legend without arguments",
      "success_rate": 0.9,
      "how": "plt.plot([1,2,3], label='my line'); plt.legend()",
      "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.84,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-05-20",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}