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.
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.
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!
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!
Activity
jenshnielsen commented on Jul 11, 2016
I cannot confirm the last statement. The following code produces the correct result.
anntzer commented on Oct 1, 2021
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
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.
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?
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!
anntzer commented on Mar 27, 2023
Issue is still valid and actionable.
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!
Fix color from latex code in pdf