matplotlib / matplotlib/matplotlib
[Bug]: PGF file produces incorrect whitespace in the legend when using certain documentclass.
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 23.2k
- Forks
- 8.5k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 66
Description
### Bug summary
When using Matplotlib to generate PGF files with the `IEEEtran` document class, I noticed that if the legend font size is smaller than 5, extra whitespace appears on the right side. Currently, my workaround is to forcibly override the default `article` document class, which avoids this issue.
Would it be possible to provide an interface allowing users to directly modify the documentclass used by Matplotlib? Below is the reproducible code and a screenshot demonstrating the issue.
```latex
\documentclass{IEEEtran}
\usepackage{pgf}
\begin{document}
\input{main.pgf}
\end{document}
```
Overriding the default definition can resolve this issue.
```python
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.backends.backend_pgf
matplotlib.backends.backend_pgf._DOCUMENTCLASS = r"\documentclass{IEEEtran}"
if __name__ == '__main__':
matplotlib.rcParams['pgf.texsystem'] = 'pdflatex'
fig, ax = plt.subplots(figsize=(2, 2),constrained_layout=True)
ax.plot([1, 2, 3, 4, 5], [5, 5, 5, 5, 5], label='legend')
ax.plot([1, 2, 3, 4, 5], [5, 5, 5, 5, 5], label='test legend')
legend=ax.legend(fontsize=2)
pfg_file = 'main.pgf'
plt.savefig(pfg_file, transparent=True)
plt.close()
```
### Code for reproduction
```Python
import matplotlib
import matplotlib.pyplot as plt
if __name__ == '__main__':
matplotlib.rcParams['pgf.texsystem'] = 'pdflatex'
fig, ax = plt.subplots(figsize=(2, 2),constrained_layout=True)
ax.plot([1, 2, 3, 4, 5], [5, 5, 5, 5, 5], label='legend')
ax.plot([1, 2, 3, 4, 5], [5, 5, 5, 5, 5], label='test legend')
legend=ax.legend(fontsize=2)
pfg_file = 'main.pgf'
plt.savefig(pfg_file, transparent=True)
plt.close()
```
### Actual outcome

### Expected outcome

### Additional information
_No response_
### Operating system
_No response_
### Matplotlib Version
3.10.0
### Matplotlib Backend
_No response_
### Python version
3.12.4
### Jupyter version
_No response_
### Installation
pip
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Inspect matplotlib.backends.backend_pgf and the _DOCUMENTCLASS override; run the supplied Python/LaTeX reproduction with IEEEtran and a small legend font. Done means the document class can be selected through a supported interface and the reported extra legend whitespace is addressed without the private override.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- latex, python
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100