Closed
Description
Bug summary
When I plot a multiline text using ax.text()
with path_effects
under TeX environment with package \usepackage[T1]{fontenc}
the multiline coalignment to right breaks.
Code for reproduction
import matplotlib
print(matplotlib.__version__)
import matplotlib.pyplot as plt
from matplotlib import rc_context, patheffects as path_effects
print(matplotlib.get_backend())
with rc_context({'text.usetex': True, 'font.family': 'serif', 'font.serif':'cm',
'text.latex.preamble': r'\usepackage[T1]{fontenc}'
}):
fig, ax = plt.subplots(layout='constrained')
text1 = ax.text(0, 0.95, r'\textbf{aaaaaaaaaaaaaaa}'
"\n" + r'\textbf{{\quad\textsc{aaaaaa}}}',
transform=ax.transAxes, fontsize=12, va='top', ha='left',linespacing=1.2,ma='left',
color='green', )
text2 = ax.text(0, 0.8, r'\textbf{aa}'
"\n" + r'\textbf{{aaaaaaaaaaaaaaa}}',
transform=ax.transAxes, fontsize=12, va='top', ha='left',linespacing=1.2,ma='right',
color='red', path_effects=[path_effects.Stroke(linewidth=1.2, foreground='black'),
path_effects.Normal()])
text3 = ax.text(1, 0.95, r'\textbf{aa}'
"\n" + r'\textbf{{aaaaaaaaaaaaaaa}}',
transform=ax.transAxes, fontsize=12, va='top', ha='right',linespacing=1.2,ma='right',
color='red',)
text4 = ax.text(1, 0.8, r'\textbf{aa}'
"\n" + r'\textbf{{aaaaaaaaaaaaaaa}}',
transform=ax.transAxes, fontsize=12, va='top', ha='right',linespacing=1.2,ma='right',
color='red', path_effects=[path_effects.Stroke(linewidth=1.2, foreground='black'),
path_effects.Normal()])
ax.set_title(r'With \textbackslash usepackage[T1]\{fontenc\}')
plt.show()
with rc_context({'text.usetex': True, 'font.family': 'serif', 'font.serif':'cm',
}):
fig, ax = plt.subplots(layout='constrained')
text1 = ax.text(0, 0.95, r'\textbf{aaaaaaaaaaaaaaa}'
"\n" + r'\textbf{{\quad\textsc{aaaaaa}}}',
transform=ax.transAxes, fontsize=12, va='top', ha='left',linespacing=1.2,ma='left',
color='green', )
text2 = ax.text(0, 0.8, r'\textbf{aa}'
"\n" + r'\textbf{{aaaaaaaaaaaaaaa}}',
transform=ax.transAxes, fontsize=12, va='top', ha='left',linespacing=1.2,ma='right',
color='red', path_effects=[path_effects.Stroke(linewidth=1.2, foreground='black'),
path_effects.Normal()])
text3 = ax.text(1, 0.95, r'\textbf{aa}'
"\n" + r'\textbf{{aaaaaaaaaaaaaaa}}',
transform=ax.transAxes, fontsize=12, va='top', ha='right',linespacing=1.2,ma='right',
color='red',)
text4 = ax.text(1, 0.8, r'\textbf{aa}'
"\n" + r'\textbf{{aaaaaaaaaaaaaaa}}',
transform=ax.transAxes, fontsize=12, va='top', ha='right',linespacing=1.2,ma='right',
color='red', path_effects=[path_effects.Stroke(linewidth=1.2, foreground='black'),
path_effects.Normal()])
ax.set_title(r'Without \textbackslash usepackage[T1]\{fontenc\}')
plt.show()
Actual outcome
The ma='right'
breaks for the bottom two texts with path_effects
.
Expected outcome
I would expect something similar without \usepackage[T1]{fontenc}
below, but somehow I really want to include it because it provides some additional support, for example, nesting \textsc{}
in \textbf{}
.
Additional information
No response
Operating system
WSL 2.2.4.0/Ubuntu 22.04.3 LTS
Matplotlib Version
3.9.1
Matplotlib Backend
qtagg
Python version
3.12.2
Jupyter version
No response
Installation
None