matplotlib / matplotlib/matplotlib
[Bug]: bbox and alignment issues with math text
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
There are several issues with bbox and text alignement when math is used, as evidenced in the mwe below.
In the 'normal' case (1st line), all is ok.
When using mathtext (2nd), the height of the bounding box depends on the text contents while it should not (no fancy super/under script that would extend beyond 'lp'). Also note that the horizontal alignment is different wrt the 1st and 3rd lines (space to the left of the V, no space to the right of the last letter).
When using 'usetex' (3rd line), the bounding box seems correctly placed, but the text position itself in the second case is off by a little bit (see the 'l' being slightly above the line).
### Code for reproduction
```python
import matplotlib.pyplot as plt
kw = dict(
fontsize=55,
bbox=dict(boxstyle='round,pad=0', fc='lightsteelblue', ec='none')
)
plt.annotate('V lp', xy=(0.2, 0.7), **kw)
plt.annotate('V l', xy=(0.5, 0.7), **kw)
plt.axhline(y=0.7, zorder=10)
plt.annotate('$V$ lp', xy=(0.2, 0.4), **kw)
plt.annotate('$V$ l', xy=(0.5, 0.4), **kw)
plt.axhline(y=0.4, zorder=10)
plt.annotate('$V$ lp', xy=(0.2, 0.1), usetex=True, **kw)
plt.annotate('$V$ l', xy=(0.5, 0.1), usetex=True, **kw)
plt.axhline(y=0.1, zorder=10)
plt.show()
```
### Actual outcome

### Expected outcome
2nd row: the height of the first bbox should be fixed.
3rd row: the vertical positioning of the text in the second case should be fixed.
### Additional information
_No response_
### Operating system
_No response_
### Matplotlib Version
3.5.1
### Matplotlib Backend
QtAgg
### Python version
3.10.2
### Jupyter version
_No response_
### Installation
_No response_
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
Start by running the provided Python reproduction with Matplotlib 3.5.1 and compare the three annotation rows. Trace the annotation, text-layout, and mathtext bounding-box handling used by the reproduction; done means mathtext produces a fixed expected bbox and the usetex text is vertically aligned consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100