# ValueError: 'loc'参数必须是字符串或0到10之间的整数，但您提供了'center left'，这是一个拼写错误的有效字符串。

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

## 根因

图例位置的'loc'字符串区分大小写，必须是预定义字符串之一，例如'upper left'、'lower right'等；拼写错误（如'centr left'）会导致此错误。

## 版本兼容性

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

## 解决方案

1. **Use the exact valid string from documentation** (95% 成功率)
   ```
   plt.legend(loc='upper left')  # or 'lower right', 'center', etc.
   ```
2. **Use a valid numeric code (0-10) for backward compatibility** (90% 成功率)
   ```
   plt.legend(loc=2)  # 2 corresponds to 'upper left'
   ```

## 无效尝试

- **Using a numeric code like 10 thinking it maps to 'center'** — Numeric codes are deprecated in newer versions and may not work as expected; also 'center left' is not a single numeric code. (70% 失败率)
- **Ignoring case sensitivity and trying 'Upper Left'** — The strings are case-sensitive; 'Upper Left' is not valid, only 'upper left' works. (80% 失败率)
