Skip to content

[ENH]: Format PercentFormatter based on usetex value #28126

Open

Description

@voidstarstar

Problem

When usetex=True, the PercentFormatter renders using text mode instead of math mode.

Proposed solution

It should be treated similar to how ScalarFormatter wraps the string. The entire string should be wrapped including both the number and symbol.

Related:
#26179
#6886
#7965

Activity

  1. voidstarstar commented on Apr 24, 2024

    @voidstarstar
    Author

    I think this can be done by changing this line to:

    return r'$\mathdefault{%s}$' % (s + self.symbol)

    when text.usetex is True.

  2. AllanChain commented on Mar 14, 2025

    @AllanChain

    I would also like to have PercentFormatter to support mathtext.

    Currently, there are font errors when using cmr10 font with negative percents.
    from matplotlib import pyplot as plt
    import matplotlib.ticker as mtick
    
    plt.rcParams["axes.formatter.use_mathtext"] = True
    plt.rcParams["mathtext.fontset"] = "cm"
    plt.rcParams["font.serif"] = "cmr10"
    plt.rcParams["font.family"] = "serif"
    
    plt.plot([1, 2, 3], [-1, 0, 1])
    plt.gca().yaxis.set_major_formatter(mtick.PercentFormatter())

    Image

    The default `ScalarFormatter` produces correct results.

    Image

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions