# UnicodeEncodeError: 'locale' codec can't encode character '\u2014' in position 0: encoding error

- **ID:** `python/matplotlib-unicode-title-error`
- **Domain:** python
- **Category:** encoding_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Matplotlib's default backend tries to encode Unicode characters (e.g., em dash) using the system's locale encoding, which may not support them, especially on non-UTF-8 locales.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |

## Workarounds

1. **Set the environment variable PYTHONIOENCODING to utf-8 before importing matplotlib** (85% success)
   ```
   import os; os.environ['PYTHONIOENCODING'] = 'utf-8'; import matplotlib.pyplot as plt
   ```
2. **Use a non-interactive backend like 'Agg'** (90% success)
   ```
   import matplotlib; matplotlib.use('Agg'); import matplotlib.pyplot as plt
   ```

## Dead Ends

- **Setting plt.rcParams['font.sans-serif'] to a different font without addressing locale encoding** — Font change does not affect the encoding step; the error persists because the backend still uses locale for output. (80% fail)
- **Adding # -*- coding: utf-8 -*- to the script** — This only affects source file encoding, not runtime output encoding; the locale issue remains. (90% fail)
