Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: multialignment='right' in ax.text() with path_effects breaks when using LaTeX package \usepackage[T1]{fontenc} #28675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
yjzhu-solar opened this issue Aug 7, 2024 · 1 comment

Comments

@yjzhu-solar
Copy link

yjzhu-solar commented Aug 7, 2024

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.
image

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{}.
image

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

@anntzer
Copy link
Contributor

anntzer commented Sep 12, 2024

I suspect this is related to the scaling issues in #14159. A more minimal repro, for reference:

import matplotlib as mpl
print(mpl.__version__)
import matplotlib.pyplot as plt, matplotlib.patheffects

mpl.rcParams['text.latex.preamble'] = r'\usepackage[T1]{fontenc}'  # comment out to compare
plt.figtext(.5, .5, "aa\naaaaaaaaa", ma="right", usetex=True,
            path_effects=[mpl.patheffects.Normal()])
plt.show()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants