matplotlib / matplotlib/matplotlib

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

未关闭
#29,978 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

backend: pgf
主要语言
Python
星标
23.2k
派生
8.5k
平均合并
1 天 6 小时
30 天内合并 PR
66

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

检查 matplotlib.backends.backend_pgf 和 _DOCUMENTCLASS 覆盖;使用 IEEEtran 和较小的图例字体运行所提供的 Python/LaTeX 重现。完成标准是可以通过受支持的接口选择文档类,并且无需使用私有覆盖即可解决报告的额外图例空白问题。

由索引模型根据 Issue 内容生成。

评估

技术栈
latex, python
领域
data-visualization
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。