# TypeError: 'Line2D'对象不可迭代

- **ID:** `python/matplotlib-legend-handle-error`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

向plt.legend()传递了单个线条对象（而非列表），但该函数期望句柄列表，或错误地使用了*args。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.7 | active | — | — |
| 3.8 | active | — | — |

## 解决方案

1. **Pass handles as a list** (95% 成功率)
   ```
   line, = plt.plot([1,2,3]); plt.legend([line], ['my line'])
   ```
2. **Set labels during plot and call legend without arguments** (90% 成功率)
   ```
   plt.plot([1,2,3], label='my line'); plt.legend()
   ```

## 无效尝试

- **Wrapping the single object in a list but still getting error** — If the list contains other non-handle objects, the error may persist. (60% 失败率)
- **Using plt.legend() without arguments** — This works but may not show the desired legend if labels are not set. (40% 失败率)
