FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Inconsistent behavior of backends when rendering latex colors · Issue #6724 · matplotlib/matplotlib · GitHub

Repository navigation

Inconsistent behavior of backends when rendering latex colors #6724

Open

Description

MPL 1.5.1 on Python 3.5 (Anaconda) running on Red Hat 6.5.

This issue is a followup to the discussion on matplotlib-users Creating Colored Axis Labels using LaTeX in Interactive Mode, which in turn is a followup to Stack Overflow Inconsistent rendering of hdashrule in matplotlib axis labels.

In my attempts to get latex colors to show up correctly in axis labels using interactive backends with LaTeX enabled, I came across http://matplotlib.org/users/pgf.html. I expected to be able to do the following:

import matplotlib as mpl
from matplotlib.backends.backend_pgf import FigureCanvasPgf
matplotlib.backend_bases.register_backend('png', FigureCanvasPgf)
from matplotlib import pyplot as plt

matplotlib.rc('pgf', texsystem='pdflatex')  # from running latex -v
preamble = matplotlib.rcParams.setdefault('pgf.preamble', [])
preamble.append(r'\usepackage{color}')
preamble.append(r'\usepackage{dashrule}')

ax = plt.plot((0, 1), (1, 2))[0].axes
ax.set_ylabel(r'Y $\;$ \textcolor[rgb]{1.0, 0.0, 0.0}{\hdashrule[0.5ex]{3cm}{1pt}{1pt 0pt}}')
ax.set_xlabel(r'N $\;$ \textcolor[rgb]{0.0, 1.0, 0.0}{\rule[0.5ex]{3cm}{1pt}}')
plt.savefig('test.png')

With the default backend, this does not work at all, and the labels are rendered as raw strings containing TeX commands. With mpl.use('pgf'), the lines in the labels are rendered black instead of the intended color. Please see the SO question and mailing list discussion for more info.

Activity

  1. jenshnielsen commented on Jul 11, 2016

    Member

    I cannot confirm the last statement. The following code produces the correct result.

    import matplotlib as matplotlib
    matplotlib.use('pgf')
    from matplotlib.backends.backend_pgf import FigureCanvasPgf
    matplotlib.backend_bases.register_backend('png', FigureCanvasPgf)
    from matplotlib import pyplot as plt
    
    matplotlib.rc('pgf', texsystem='pdflatex')  # from running latex -v
    preamble = matplotlib.rcParams.setdefault('pgf.preamble', [])
    preamble.append(r'\usepackage{color}')
    preamble.append(r'\usepackage{dashrule}')
    
    ax = plt.plot((0, 1), (1, 2))[0].axes
    ax.set_ylabel(r'Y $\;$ \textcolor[rgb]{1.0, 0.0, 0.0}{\hdashrule[0.5ex]{3cm}{1pt}{1pt 0pt}}')
    ax.set_xlabel(r'N $\;$ \textcolor[rgb]{0.0, 1.0, 0.0}{\rule[0.5ex]{3cm}{1pt}}')
    plt.savefig('test.png')
  2. anntzer commented on Oct 1, 2021

    Contributor

    I agree that things seem to work with the pgf backend.

    As for agg, this is because you also need to enable usetex and set the preamble via text.latex.preamble, i.e. this should now read

    from matplotlib import pyplot as plt
    
    plt.rcParams.update({
        'pgf.texsystem': 'pdflatex',
        'pgf.preamble': r'\usepackage{color}\usepackage{dashrule}',
        'text.usetex': True,
        'text.latex.preamble':  r'\usepackage{color}\usepackage{dashrule}',
    })
    
    fig, ax = plt.subplots()
    ax.set_ylabel(r'Y $\;$ \textcolor[rgb]{1.0, 0.0, 0.0}{\hdashrule[0.5ex]{3cm}{1pt}{1pt 0pt}}')
    ax.set_xlabel(r'N $\;$ \textcolor[rgb]{0.0, 1.0, 0.0}{\rule[0.5ex]{3cm}{1pt}}')
    fig.savefig('test.png', backend='pgf')
    plt.show()

    One then sees that indeed, color is not working for png output (it is also not working for vector output but I think that would be extremely difficult to fix because of how color is encoded in dvi specials). I think this ultimately arises because RendererAgg.draw_tex only passes a grayscale image to draw_text_image (which also only supports grayscale), even though it could quite reasonably use RGBA images instead. At least this point seems reasonably actionable. It's also quite self-contained (no API decision), but fixing would require plunging into the Agg parts of the code, so I'm labeling as medium/hard but good first issue.

  3. added
    Difficulty: Hardhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    and removed on Oct 1, 2021
  4. Atcold commented on Feb 25, 2022

    Oh, wow, that backend='pgf' solved all my issues.
    Still, the preview on the notebook uses an uncoloured version.
    Is there something else I should set?

  5. github-actions commented on Mar 27, 2023

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  6. anntzer commented on Mar 27, 2023

    Contributor

    Issue is still valid and actionable.

  7. github-actions commented on Mar 29, 2024

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  8. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on Mar 29, 2024
  9. added a commit that references this issue on May 7, 2025
  10. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    and removed
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Hardhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesDifficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issueskeepItems to be ignored by the “Stale” Github Actiontopic: text/usetex🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL