matplotlib / matplotlib/matplotlib

[Bug]: PGF file produces incorrect whitespace in the legend when using certain documentclass.

Open
#29,978 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

backend: pgf
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

![Image](https://github.com/user-attachments/assets/1d1aaec8-cb3c-4668-80fc-2f9cd55e3d3d)

### Expected outcome

![Image](https://github.com/user-attachments/assets/8ea52ae5-70e0-420a-ba0d-7256bb20ce27)

### 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.