First of all, to use "\color" with TeX, you need to use "color"
package, i.e., you need to modify your latex preamble to include that
package. This can be done by

import matplotlib.pyplot as plt
plt.rcParams["text.latex.preamble"] = r"\usepackage{color}"

I guess the error you encounter is because of this (I'm not sure as
the error message is slightly different in my machine).

However, this WON'T work in general!
The color information in the dvi (or png) file is ignored in most
cases (the only exception I know is the ps backend which does not read
in dvi of png output).
So, if you only want ps output, you can use usetex with "\color" command.

In matplotlib, a single Text object only have a single color
associated. So changing the color of the text within the string is be
hardly possible (except in the ps backend). So, I guess your best
option is to call "text" (or "annotate") multiple times with different
colors with fine-tuned text positions.

FYI, It is possible to have two (or more) text instances of different
colors packed together (somewhat similar to this example
http://matplotlib.sourceforge.net/plot_directive/users/plotting/examples/anchored_box04.hires.png),

Regards,

-JJ

On Mon, Jan 11, 2010 at 11:13 AM, Gökhan Sever <gokhanse...@gmail.com> wrote:
> This seems like the solution. However when I try:
>
> I[1]: plt.plot(range(10))
> I[2]: plt.ylabel(r"{\color{red} a red text}")
>
> It doesn't function and produce an ugly set of error messages:
>
> I[3]: ERROR: An unexpected error occurred while tokenizing input
> The following traceback may be corrupted or invalid
> The error message is: ('EOF in multi-line statement', (573, 0))
>
> ERROR: An unexpected error occurred while tokenizing input
> The following traceback may be corrupted or invalid
> The error message is: ('EOF in multi-line statement', (573, 0))
>
> ERROR: An unexpected error occurred while tokenizing input
> The following traceback may be corrupted or invalid
> The error message is: ('EOF in multi-line statement', (573, 0))
>
> ERROR: An unexpected error occurred while tokenizing input
> The following traceback may be corrupted or invalid
> The error message is: ('EOF in multi-line statement', (573, 0))
>
> ERROR: An unexpected error occurred while tokenizing input
> The following traceback may be corrupted or invalid
> The error message is: ('EOF in multi-line statement', (566, 0))
>
> ---------------------------------------------------------------------------
> RuntimeError                              Traceback (most recent call last)
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/backends/backend_qt4.pyc
> in resizeEvent(self, event)
>     167         hinch = h/dpival
>     168         self.figure.set_size_inches( winch, hinch )
> --> 169         self.draw()
>     170         self.update()
>     171         QtGui.QWidget.resizeEvent(self, event)
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/backends/backend_qt4agg.pyc
> in draw(self)
>     128         if DEBUG: print "FigureCanvasQtAgg.draw", self
>     129         self.replot = True
> --> 130         FigureCanvasAgg.draw(self)
>     131         self.update()
>     132
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/backends/backend_agg.pyc
> in draw(self)
>     386
>     387         self.renderer = self.get_renderer()
> --> 388         self.figure.draw(self.renderer)
>     389
>     390     def get_renderer(self):
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/artist.pyc in
> draw_wrapper(artist, renderer, *kl)
>      53     def draw_wrapper(artist, renderer, *kl):
>      54         before(artist, renderer)
> ---> 55         draw(artist, renderer, *kl)
>      56         after(artist, renderer)
>      57
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/figure.pyc in
> draw(self, renderer)
>     800         dsu.sort(key=itemgetter(0))
>     801         for zorder, func, args in dsu:
> --> 802             func(*args)
>     803
>     804         renderer.close_group('figure')
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/artist.pyc in
> draw_wrapper(artist, renderer, *kl)
>      53     def draw_wrapper(artist, renderer, *kl):
>      54         before(artist, renderer)
> ---> 55         draw(artist, renderer, *kl)
>      56         after(artist, renderer)
>      57
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/axes.pyc in
> draw(self, renderer, inframe)
>    1755
>    1756         for zorder, a in dsu:
> -> 1757             a.draw(renderer)
>    1758
>    1759         renderer.close_group('axes')
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/artist.pyc in
> draw_wrapper(artist, renderer, *kl)
>      53     def draw_wrapper(artist, renderer, *kl):
>      54         before(artist, renderer)
> ---> 55         draw(artist, renderer, *kl)
>      56         after(artist, renderer)
>      57
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/axis.pyc in
> draw(self, renderer, *args, **kwargs)
>     819         self._update_label_position(ticklabelBoxes, ticklabelBoxes2)
>     820
> --> 821         self.label.draw(renderer)
>     822
>     823         self._update_offset_text_position(ticklabelBoxes,
> ticklabelBoxes2)
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/artist.pyc in
> draw_wrapper(artist, renderer, *kl)
>      53     def draw_wrapper(artist, renderer, *kl):
>      54         before(artist, renderer)
> ---> 55         draw(artist, renderer, *kl)
>      56         after(artist, renderer)
>      57
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/text.pyc in
> draw(self, renderer)
>     547         renderer.open_group('text', self.get_gid())
>     548
> --> 549         bbox, info = self._get_layout(renderer)
>     550         trans = self.get_transform()
>     551
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/text.pyc in
> _get_layout(self, renderer)
>     303             w, h, d = get_text_width_height_descent(clean_line,
>     304
> self._fontproperties,
> --> 305                                                     ismath=ismath)
>     306             if baseline is None:
>     307                 baseline = h - d
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/backends/backend_agg.pyc
> in get_text_width_height_descent(self, s, prop, ismath)
>     169             fontsize = prop.get_size_in_points()
>     170             w, h, d = texmanager.get_text_width_height_descent(s,
> fontsize,
> --> 171
> renderer=self)
>     172             return w, h, d
>     173
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/texmanager.pyc in
> get_text_width_height_descent(self, tex, fontsize, renderer)
>     599         else:
>     600             # use dviread. It sometimes returns a wrong descent.
>
> --> 601             dvifile = self.make_dvi(tex, fontsize)
>     602             dvi = dviread.Dvi(dvifile, 72*dpi_fraction)
>     603             page = iter(dvi).next()
>
> /home/gsever/Desktop/python-repo/matplotlib/lib/matplotlib/texmanager.pyc in
> make_dvi(self, tex, fontsize)
>     369             if exit_status or not exists:
>     370                 raise RuntimeError(('LaTeX was not able to process
> the following \
> --> 371 string:\n%s\nHere is the full report generated by LaTeX: \n\n'%
> repr(tex)) + report)
>     372             else: mpl.verbose.report(report, 'debug')
>     373             for fname in glob.glob(basefile+'*'):
>
> RuntimeError: LaTeX was not able to process the following string:
> '{\\color{red} a red text}'
> Here is the full report generated by LaTeX:
>
> This is pdfTeXk, Version 3.141592-1.40.3 (Web2C 7.5.6)
>  %&-line parsing enabled.
> entering extended mode
> (./f5e3ddba2ab4bf5147d4e32e96deb043.tex
> LaTeX2e <2005/12/01>
> Babel <v3.8h> and hyphenation patterns for english, usenglishmax, dumylang,
> noh
> yphenation, arabic, basque, bulgarian, coptic, welsh, czech, slovak, german,
> ng
> erman, danish, esperanto, spanish, catalan, galician, estonian, farsi,
> finnish,
>  french, greek, monogreek, ancientgreek, croatian, hungarian, interlingua,
> ibyc
> us, indonesian, icelandic, italian, latin, mongolian, dutch, norsk, polish,
> por
> tuguese, pinyin, romanian, russian, slovenian, uppersorbian, serbian,
> swedish,
> turkish, ukenglish, ukrainian, loaded.
> (/usr/share/texmf/tex/latex/base/article.cls
> Document Class: article 2005/09/16 v1.4f Standard LaTeX document class
> (/usr/share/texmf/tex/latex/base/size10.clo))
> (/usr/share/texmf/tex/latex/type1cm/type1cm.sty)
> (/usr/share/texmf/tex/latex/psnfss/helvet.sty
> (/usr/share/texmf/tex/latex/graphics/keyval.sty))
> (/usr/share/texmf/tex/latex/psnfss/courier.sty)
> (/usr/share/texmf/tex/latex/base/textcomp.sty
> (/usr/share/texmf/tex/latex/base/ts1enc.def))
> (/usr/share/texmf/tex/latex/geometry/geometry.sty
>
> Package geometry Warning: Over-specification in `h'-direction.
>     `width' (5058.9pt) is ignored.
>
>
> Package geometry Warning: Over-specification in `v'-direction.
>     `height' (5058.9pt) is ignored.
>
> )
> No file f5e3ddba2ab4bf5147d4e32e96deb043.aux.
> (/usr/share/texmf/tex/latex/base/ts1cmr.fd)
> (/usr/share/texmf/tex/latex/psnfss/ot1pnc.fd)
> (/usr/share/texmf/tex/latex/psnfss/ot1phv.fd)
> ! Undefined control sequence.
> l.12 ...ze{12.000000}{15.000000}{\sffamily {\color
>                                                   {red} a red text}}
> [1] (./f5e3ddba2ab4bf5147d4e32e96deb043.aux) )
> (see the transcript file for additional information)
> Output written on f5e3ddba2ab4bf5147d4e32e96deb043.dvi (1 page, 224 bytes).
> Transcript written on f5e3ddba2ab4bf5147d4e32e96deb043.log.
>
>
> Interestingly tex_demo.py works fine and renders the text properly onto the
> figure.
>
>
> On Mon, Jan 11, 2010 at 4:36 AM, Damon McDougall <d.mcdoug...@warwick.ac.uk>
> wrote:
>>
>> Hi Gökhan,
>> Do you have usetex=True set? If so, have you tried the LaTeX command
>> sequence \color{red}{Red text goes here} ?
>> Regards,
>> -- Damon
>>
>> --------------------------
>> Damon McDougall
>> Mathematics Institute
>> University of Warwick
>> Coventry
>> CV4 7AL
>> d.mcdoug...@warwick.ac.uk
>> On 11 Jan 2010, at 05:48, Gökhan Sever wrote:
>>
>> Hello,
>>
>> Is there a way to color words differently in a label or title, for
>> example:
>>
>> ylabel("measurement 1 (in red color)" , "measurement 2 (in blue color)")
>> to match with the plotted line colors?
>>
>> Thanks.
>>
>> --
>> Gökhan
>>
>> ------------------------------------------------------------------------------
>> This SF.Net email is sponsored by the Verizon Developer Community
>> Take advantage of Verizon's best-in-class app development support
>> A streamlined, 14 day to market process makes app distribution fast and
>> easy
>> Join now and get one step closer to millions of Verizon customers
>> http://p.sf.net/sfu/verizon-dev2dev
>> _______________________________________________
>> Matplotlib-users mailing list
>> Matplotlib-users@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>>
>
>
>
> --
> Gökhan
>
> ------------------------------------------------------------------------------
> This SF.Net email is sponsored by the Verizon Developer Community
> Take advantage of Verizon's best-in-class app development support
> A streamlined, 14 day to market process makes app distribution fast and easy
> Join now and get one step closer to millions of Verizon customers
> http://p.sf.net/sfu/verizon-dev2dev
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
>

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to