[Matplotlib-users] Creating Colored Axis Labels using LaTeX in Interactive Mode

Joseph Fox-Rabinovitz jfoxrabinovitz at gmail.com
Mon Jul 11 15:37:55 EDT 2016


I will put in a ticket shortly. I noticed that text latex preamble is
not supported according to the docs, but there is nothing about pgf
latex preamble. Perhaps the docs need to be updated there as well?
Either way, I don't think the issue is with the preamble at all, since
that seems to be importing the requested packages as expected. I think
the unicode link you posted has bearing on the issue, but I have not
experimented enough yet to know exactly how.

Thanks,

    -Joe



On Mon, Jul 11, 2016 at 3:19 PM, Jens Nielsen <jenshnielsen at gmail.com> wrote:
> Well pdf from the PGF backend also work as you expect for me
>
> Feel free to open a ticket but note that the latex preample is not
> officially supported.
>
> I suspect that the reason setting the color doesn't work is that the
> matplotlib default color handling overwrites it
> as in this example
> http://matplotlib.org/users/usetex.html#usetex-with-unicode that is however
> not as easy if you only want to partially color the text
>
>
>
>
> On Mon, 11 Jul 2016 at 20:06 Joseph Fox-Rabinovitz
> <jfoxrabinovitz at gmail.com> wrote:
>>
>> It seems that for the time being, `use('ps')` is the only way to get
>> the rendering done correctly. I was hoping to get something that
>> rendered with partial accuracy in interactive mode while allowing me
>> to save correctly to a file. Any objections to me filing an issue
>> about this?
>>
>> Regards,
>>
>>     -Joe
>>
>>
>> On Mon, Jul 11, 2016 at 2:55 PM, Jens Nielsen <jenshnielsen at gmail.com>
>> wrote:
>> > You are right. I suspect that the register_backend logic does not work
>> > correctly for png. The code produces the expected results
>> > in pdf with register_backend and in both pdf and png if I do
>> > matplotlib.use('pgf') so it would seem like there are at least 2
>> > different
>> > bugs.
>> >
>> > The color does not work with the regular latex text backend.
>> >
>> > register_backend does not work correctly with png and the pgf backend.
>> >
>> > best
>> > Jens
>> >
>> >
>> >
>> >
>> > On Mon, 11 Jul 2016 at 18:49 Joseph Fox-Rabinovitz
>> > <jfoxrabinovitz at gmail.com> wrote:
>> >>
>> >> Thanks for the catch with `\usepackage{dashrule}`. Using matplotlib
>> >> 1.5, the code you posted does not work as expected. The labels are the
>> >> literal strings containing LaTeX commands, not the LaTeX output.
>> >> Adding in the commands under `# Text config` fixed at least that part
>> >> of the issue. It seems to be caused by the fact that I am using a
>> >> non-pgf backend by default.
>> >>
>> >> Regards,
>> >>
>> >>     -Joe
>> >>
>> >>
>> >>
>> >> On Mon, Jul 11, 2016 at 12:35 PM, Jens Nielsen <jenshnielsen at gmail.com>
>> >> wrote:
>> >> > I can confirm that this does not work as expected but I don't know
>> >> > exactly
>> >> > why.
>> >> >
>> >> > With regards to your pgf example. It seems to be missing a
>> >> > \usepackage{dashrule}
>> >> >
>> >> > The following runs for me but still generates the black non colored
>> >> > bars.
>> >> > The # Text config seems to do nothing in my test so I'm not sure why
>> >> > you
>> >> > need to set it.
>> >> >
>> >> > If I save the pgf backend to a .pgf file and include it in a texfile
>> >> > as
>> >> > below everything looks correct in that example
>> >> >
>> >> > test.py
>> >> > ```
>> >> > import matplotlib
>> >> > 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}')
>> >> >
>> >> > # Text config
>> >> > #matplotlib.rc('text', usetex=True)
>> >> > # preamble = matplotlib.rcParams.setdefault('text.latex.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.pgf')
>> >> > plt.savefig('test.png')
>> >> > ```
>> >> >
>> >> > test.tex
>> >> > ```
>> >> > \documentclass{article}
>> >> >
>> >> > \usepackage[utf8]{inputenc}
>> >> > \usepackage[english]{babel}
>> >> >
>> >> > \usepackage{color}
>> >> > \usepackage{dashrule}
>> >> > \usepackage{pgf}
>> >> >
>> >> > \begin{document}
>> >> >
>> >> > \input{test.pgf}
>> >> > \end{document}
>> >> > ```
>> >> >
>> >> > and running pdflatex test.tex generates a test.pdf that looks as
>> >> > expected.
>> >> >
>> >> > best
>> >> > Jens
>> >> >
>> >> >
>> >> >
>> >> > On Mon, 11 Jul 2016 at 17:08 Joseph Fox-Rabinovitz
>> >> > <jfoxrabinovitz at gmail.com> wrote:
>> >> >>
>> >> >> Have an update on my previous question. I have tried to follow the
>> >> >> instructions at http://matplotlib.org/users/pgf.html for settting up
>> >> >> the PGF backend for PNG and PDF rendering. This method does not work
>> >> >> either:
>> >> >>
>> >> >>     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}')
>> >> >>
>> >> >>     # Text config
>> >> >>     matplotlib.rc('text', usetex=True)
>> >> >>     preamble = matplotlib.rcParams.setdefault('text.latex.preamble',
>> >> >> [])
>> >> >>     preamble.append(r'\usepackage{color}')
>> >> >>
>> >> >>     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')
>> >> >>
>> >> >> Yields the exact same figure and PNG file with the incorrect black
>> >> >> lines as above. Note that turning off the three lines under `# Text
>> >> >> config` makes the TeX commands print out verbatim, even in the saved
>> >> >> figure. Perhaps there is a way to render the PNG correctly without
>> >> >> pre-clobbering with the text system?
>> >> >>
>> >> >> Regards,
>> >> >>
>> >> >>     -Joe
>> >> >>
>> >> >>
>> >> >>
>> >> >> On Mon, Jul 11, 2016 at 11:12 AM, Joseph Fox-Rabinovitz
>> >> >> <jfoxrabinovitz at gmail.com> wrote:
>> >> >> > I originally posted to Stack Overflow at
>> >> >> > http://stackoverflow.com/q/38274681/2988730 (and accidentally to
>> >> >> > the
>> >> >> > SourceForge version of this list).
>> >> >> >
>> >> >> > I am trying to follow the answer at
>> >> >> > http://stackoverflow.com/a/38008501/2988730 to an earlier question
>> >> >> > of
>> >> >> > mine to create colored and styled legend-like entries. I have the
>> >> >> > following code:
>> >> >> >
>> >> >> >     import matplotlib as mpl
>> >> >> >     mpl.use('ps')
>> >> >> >     from matplotlib import pyplot as plt
>> >> >> >
>> >> >> >     mpl.rc('text', usetex=True)
>> >> >> >     mpl.rc('text.latex',
>> >> >> > preamble='\\usepackage{color}\n\\usepackage{dashrule}')
>> >> >> >
>> >> >> >     plt.ion()
>> >> >> >     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.ps')
>> >> >> >
>> >> >> > The result is as expected. The labels contain black text with a
>> >> >> > red
>> >> >> > line on the Y label and a green line on the X label:
>> >> >> > http://i.stack.imgur.com/JCiLI.png.
>> >> >> >
>> >> >> > However, when I try the exact same set of commands without the
>> >> >> > `mpl.use('ps')` line (using `'qt4agg'` backend on my system), the
>> >> >> > figure neither saves corectly nor shows up correctly on screen:
>> >> >> >
>> >> >> >     import matplotlib as mpl
>> >> >> >     from matplotlib import pyplot as plt
>> >> >> >
>> >> >> >     mpl.rc('text', usetex=True)
>> >> >> >     mpl.rc('text.latex',
>> >> >> > preamble='\\usepackage{color}\n\\usepackage{dashrule}')
>> >> >> >
>> >> >> >     plt.ion()
>> >> >> >     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')
>> >> >> >     plt.show()
>> >> >> >
>> >> >> > The result of `plt.savefig` (http://i.stack.imgur.com/h2LXn.png)
>> >> >> > and
>> >> >> > `plt.show` (http://i.stack.imgur.com/0Ow7c.png) are basically the
>> >> >> > same
>> >> >> > in this case. The lines after the text show up black.
>> >> >> >
>> >> >> > How do I get the colors to show up in the labels with the default
>> >> >> > interactive backend?
>> >> >> >
>> >> >> > Regards,
>> >> >> >
>> >> >> >     -Joe
>> >> >> _______________________________________________
>> >> >> Matplotlib-users mailing list
>> >> >> Matplotlib-users at python.org
>> >> >> https://mail.python.org/mailman/listinfo/matplotlib-users


More information about the Matplotlib-users mailing list