<div dir="ltr">You are right. I suspect that the register_backend logic does not work correctly for png. The code produces the expected results <div>in pdf with register_backend and in both pdf and png if I do <span style="line-height:1.5">matplotlib.use('pgf') so it would seem like there are at least 2 different bugs.</span></div><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">The color does not work with the regular latex text backend.</span></div><div><span style="line-height:1.5"><br></span></div><div>register_backend does not work correctly with png and the pgf backend.<br></div><div><br></div><div>best</div><div>Jens</div><div><br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, 11 Jul 2016 at 18:49 Joseph Fox-Rabinovitz <<a href="mailto:jfoxrabinovitz@gmail.com">jfoxrabinovitz@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks for the catch with `\usepackage{dashrule}`. Using matplotlib<br>
1.5, the code you posted does not work as expected. The labels are the<br>
literal strings containing LaTeX commands, not the LaTeX output.<br>
Adding in the commands under `# Text config` fixed at least that part<br>
of the issue. It seems to be caused by the fact that I am using a<br>
non-pgf backend by default.<br>
<br>
Regards,<br>
<br>
-Joe<br>
<br>
<br>
<br>
On Mon, Jul 11, 2016 at 12:35 PM, Jens Nielsen <<a href="mailto:jenshnielsen@gmail.com" target="_blank">jenshnielsen@gmail.com</a>> wrote:<br>
> I can confirm that this does not work as expected but I don't know exactly<br>
> why.<br>
><br>
> With regards to your pgf example. It seems to be missing a<br>
> \usepackage{dashrule}<br>
><br>
> The following runs for me but still generates the black non colored bars.<br>
> The # Text config seems to do nothing in my test so I'm not sure why you<br>
> need to set it.<br>
><br>
> If I save the pgf backend to a .pgf file and include it in a texfile as<br>
> below everything looks correct in that example<br>
><br>
> test.py<br>
> ```<br>
> import matplotlib<br>
> from matplotlib.backends.backend_pgf import FigureCanvasPgf<br>
> matplotlib.backend_bases.register_backend('png', FigureCanvasPgf)<br>
> from matplotlib import pyplot as plt<br>
><br>
> matplotlib.rc('pgf', texsystem='pdflatex') # from running latex -v<br>
> preamble = matplotlib.rcParams.setdefault('pgf.preamble', [])<br>
> preamble.append(r'\usepackage{color}')<br>
> preamble.append(r'\usepackage{dashrule}')<br>
><br>
> # Text config<br>
> #matplotlib.rc('text', usetex=True)<br>
> # preamble = matplotlib.rcParams.setdefault('text.latex.preamble', [])<br>
> #preamble.append(r'\usepackage{color}')<br>
> #preamble.append(r'\usepackage{dashrule}')<br>
><br>
> ax = plt.plot((0, 1), (1, 2))[0].axes<br>
> ax.set_ylabel(r'Y $\;$ \textcolor[rgb]{1.0, 0.0,<br>
> 0.0}{\hdashrule[0.5ex]{3cm}{1pt}{1pt 0pt}}')<br>
> ax.set_xlabel(r'N $\;$ \textcolor[rgb]{0.0, 1.0,<br>
> 0.0}{\rule[0.5ex]{3cm}{1pt}}')<br>
> plt.savefig('test.pgf')<br>
> plt.savefig('test.png')<br>
> ```<br>
><br>
> test.tex<br>
> ```<br>
> \documentclass{article}<br>
><br>
> \usepackage[utf8]{inputenc}<br>
> \usepackage[english]{babel}<br>
><br>
> \usepackage{color}<br>
> \usepackage{dashrule}<br>
> \usepackage{pgf}<br>
><br>
> \begin{document}<br>
><br>
> \input{test.pgf}<br>
> \end{document}<br>
> ```<br>
><br>
> and running pdflatex test.tex generates a test.pdf that looks as expected.<br>
><br>
> best<br>
> Jens<br>
><br>
><br>
><br>
> On Mon, 11 Jul 2016 at 17:08 Joseph Fox-Rabinovitz<br>
> <<a href="mailto:jfoxrabinovitz@gmail.com" target="_blank">jfoxrabinovitz@gmail.com</a>> wrote:<br>
>><br>
>> Have an update on my previous question. I have tried to follow the<br>
>> instructions at <a href="http://matplotlib.org/users/pgf.html" rel="noreferrer" target="_blank">http://matplotlib.org/users/pgf.html</a> for settting up<br>
>> the PGF backend for PNG and PDF rendering. This method does not work<br>
>> either:<br>
>><br>
>> import matplotlib as mpl<br>
>> from matplotlib.backends.backend_pgf import FigureCanvasPgf<br>
>> matplotlib.backend_bases.register_backend('png', FigureCanvasPgf)<br>
>> from matplotlib import pyplot as plt<br>
>><br>
>> matplotlib.rc('pgf', texsystem='pdflatex') # from running latex -v<br>
>> preamble = matplotlib.rcParams.setdefault('pgf.preamble', [])<br>
>> preamble.append(r'\usepackage{color}')<br>
>><br>
>> # Text config<br>
>> matplotlib.rc('text', usetex=True)<br>
>> preamble = matplotlib.rcParams.setdefault('text.latex.preamble', [])<br>
>> preamble.append(r'\usepackage{color}')<br>
>><br>
>> ax = plt.plot((0, 1), (1, 2))[0].axes<br>
>> ax.set_ylabel(r'Y $\;$ \textcolor[rgb]{1.0, 0.0,<br>
>> 0.0}{\hdashrule[0.5ex]{3cm}{1pt}{1pt 0pt}}')<br>
>> ax.set_xlabel(r'N $\;$ \textcolor[rgb]{0.0, 1.0,<br>
>> 0.0}{\rule[0.5ex]{3cm}{1pt}}')<br>
>> plt.savefig('test.png')<br>
>><br>
>> Yields the exact same figure and PNG file with the incorrect black<br>
>> lines as above. Note that turning off the three lines under `# Text<br>
>> config` makes the TeX commands print out verbatim, even in the saved<br>
>> figure. Perhaps there is a way to render the PNG correctly without<br>
>> pre-clobbering with the text system?<br>
>><br>
>> Regards,<br>
>><br>
>> -Joe<br>
>><br>
>><br>
>><br>
>> On Mon, Jul 11, 2016 at 11:12 AM, Joseph Fox-Rabinovitz<br>
>> <<a href="mailto:jfoxrabinovitz@gmail.com" target="_blank">jfoxrabinovitz@gmail.com</a>> wrote:<br>
>> > I originally posted to Stack Overflow at<br>
>> > <a href="http://stackoverflow.com/q/38274681/2988730" rel="noreferrer" target="_blank">http://stackoverflow.com/q/38274681/2988730</a> (and accidentally to the<br>
>> > SourceForge version of this list).<br>
>> ><br>
>> > I am trying to follow the answer at<br>
>> > <a href="http://stackoverflow.com/a/38008501/2988730" rel="noreferrer" target="_blank">http://stackoverflow.com/a/38008501/2988730</a> to an earlier question of<br>
>> > mine to create colored and styled legend-like entries. I have the<br>
>> > following code:<br>
>> ><br>
>> > import matplotlib as mpl<br>
>> > mpl.use('ps')<br>
>> > from matplotlib import pyplot as plt<br>
>> ><br>
>> > mpl.rc('text', usetex=True)<br>
>> > mpl.rc('text.latex',<br>
>> > preamble='\\usepackage{color}\n\\usepackage{dashrule}')<br>
>> ><br>
>> > plt.ion()<br>
>> > ax = plt.plot((0, 1), (1, 2))[0].axes<br>
>> > ax.set_ylabel(r'Y $\;$ \textcolor[rgb]{1.0, 0.0,<br>
>> > 0.0}{\hdashrule[0.5ex]{3cm}{1pt}{1pt 0pt}}')<br>
>> > ax.set_xlabel(r'N $\;$ \textcolor[rgb]{0.0, 1.0,<br>
>> > 0.0}{\rule[0.5ex]{3cm}{1pt}}')<br>
>> > plt.savefig('<a href="http://test.ps" rel="noreferrer" target="_blank">test.ps</a>')<br>
>> ><br>
>> > The result is as expected. The labels contain black text with a red<br>
>> > line on the Y label and a green line on the X label:<br>
>> > <a href="http://i.stack.imgur.com/JCiLI.png" rel="noreferrer" target="_blank">http://i.stack.imgur.com/JCiLI.png</a>.<br>
>> ><br>
>> > However, when I try the exact same set of commands without the<br>
>> > `mpl.use('ps')` line (using `'qt4agg'` backend on my system), the<br>
>> > figure neither saves corectly nor shows up correctly on screen:<br>
>> ><br>
>> > import matplotlib as mpl<br>
>> > from matplotlib import pyplot as plt<br>
>> ><br>
>> > mpl.rc('text', usetex=True)<br>
>> > mpl.rc('text.latex',<br>
>> > preamble='\\usepackage{color}\n\\usepackage{dashrule}')<br>
>> ><br>
>> > plt.ion()<br>
>> > ax = plt.plot((0, 1), (1, 2))[0].axes<br>
>> > ax.set_ylabel(r'Y $\;$ \textcolor[rgb]{1.0, 0.0,<br>
>> > 0.0}{\hdashrule[0.5ex]{3cm}{1pt}{1pt 0pt}}')<br>
>> > ax.set_xlabel(r'N $\;$ \textcolor[rgb]{0.0, 1.0,<br>
>> > 0.0}{\rule[0.5ex]{3cm}{1pt}}')<br>
>> ><br>
>> > plt.savefig('test.png')<br>
>> > plt.show()<br>
>> ><br>
>> > The result of `plt.savefig` (<a href="http://i.stack.imgur.com/h2LXn.png" rel="noreferrer" target="_blank">http://i.stack.imgur.com/h2LXn.png</a>) and<br>
>> > `plt.show` (<a href="http://i.stack.imgur.com/0Ow7c.png" rel="noreferrer" target="_blank">http://i.stack.imgur.com/0Ow7c.png</a>) are basically the same<br>
>> > in this case. The lines after the text show up black.<br>
>> ><br>
>> > How do I get the colors to show up in the labels with the default<br>
>> > interactive backend?<br>
>> ><br>
>> > Regards,<br>
>> ><br>
>> > -Joe<br>
>> _______________________________________________<br>
>> Matplotlib-users mailing list<br>
>> <a href="mailto:Matplotlib-users@python.org" target="_blank">Matplotlib-users@python.org</a><br>
>> <a href="https://mail.python.org/mailman/listinfo/matplotlib-users" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/matplotlib-users</a><br>
</blockquote></div>