Tex error in plot label

I am trying to use LaTeX on the ylabel of a plot using:

plt.ylabel("$\alpha$”)

and I am seeing very inconsistent behaviour. If I use $\alpha$ I get an error (see below), but \gamma is fine. $\tau does not give an error but plots a strange character.

Can you help? thanks

Joao

Using $\alpha$ like above gives a long error message:

···

---------------------------------------------------------------------------
ValueError
                                Traceback (most recent call last)

/Users/joao/anaconda/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)
    328 pass
    329 else:
--> 330 return printer(obj)
    331 # Finally look for special method names
    332 method = _safe_get_formatter_method(obj, self.print_method)

/Users/joao/anaconda/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in <lambda>(fig)
    205
    206 if 'png' in formats:
--> 207 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    208 if 'retina' in formats or 'png2x' in formats:
    209 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

/Users/joao/anaconda/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in print_figure(fig, fmt, bbox_inches, **kwargs)
    115
    116 bytes_io = BytesIO()
--> 117 fig.canvas.print_figure(bytes_io, **kw)
    118 data = bytes_io.getvalue()
    119 if fmt == 'svg':

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2156 orientation=orientation,
   2157 dryrun=True,
-> 2158
                     **kwargs)

   2159 renderer = self.figure._cachedRenderer
   2160 bbox_inches = self.figure.get_tightbbox(renderer)

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs)
    519
    520 def print_png(self, filename_or_obj, *args, **kwargs):
--> 521 FigureCanvasAgg.draw(self)
    522 renderer = self.get_renderer()
    523 original_dpi = renderer.dpi

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in draw(self)
    467
    468 try:
--> 469 self.figure.draw(self.renderer)
    470 finally:
    471 RendererAgg.lock.release()

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     57 def draw_wrapper(artist, renderer, *args, **kwargs):
     58 before(artist, renderer)
---> 59 draw(artist, renderer, *args, **kwargs)
     60 after(artist, renderer)
     61

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/figure.pyc in draw(self, renderer)
   1083 dsu.sort(key=itemgetter(0))
   1084 for zorder, a, func, args in dsu:
-> 1085 func(*args)
   1086
   1087 renderer.close_group('figure')

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     57 def draw_wrapper(artist, renderer, *args, **kwargs):
     58 before(artist, renderer)
---> 59 draw(artist, renderer, *args, **kwargs)
     60 after(artist, renderer)
     61

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/axes/_base.pyc in draw(self, renderer, inframe)
   2108
   2109 for zorder, a in dsu:
-> 2110 a.draw(renderer)
   2111
   2112 renderer.close_group('axes')

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     57 def draw_wrapper(artist, renderer, *args, **kwargs):
     58 before(artist, renderer)
---> 59 draw(artist, renderer, *args, **kwargs)
     60 after(artist, renderer)
     61

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/axis.pyc in draw(self, renderer, *args, **kwargs)
   1126 self._update_label_position(ticklabelBoxes, ticklabelBoxes2)
   1127
-> 1128 self.label.draw(renderer)
   1129
   1130 self._update_offset_text_position(ticklabelBoxes, ticklabelBoxes2)

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     57 def draw_wrapper(artist, renderer, *args, **kwargs):
     58 before(artist, renderer)
---> 59 draw(artist, renderer, *args, **kwargs)
     60 after(artist, renderer)
     61

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/text.pyc in draw(self, renderer)
    593 renderer.open_group('text', self.get_gid())
    594
--> 595 bbox, info, descent = self._get_layout(renderer)
    596 trans = self.get_transform()
    597

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/text.pyc in _get_layout(self, renderer)
    318
                 w, h, d = renderer.get_text_width_height_descent(clean_line,

    319 self._fontproperties,
--> 320
                                                         ismath=ismath)

    321 else:
    322 w, h, d = 0, 0, 0

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in get_text_width_height_descent(self, s, prop, ismath)
    226 if ismath:
    227 ox, oy, width, height, descent, fonts, used_characters = \
--> 228 self.mathtext_parser.parse(s, self.dpi, prop)
    229 return width, height, descent
    230

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/mathtext.pyc in parse(self, s, dpi, prop)
   3003 self.__class__._parser = Parser()
   3004
-> 3005 box = self._parser.parse(s, font_output, fontsize, dpi)
   3006 font_output.set_canvas_size(box.width, box.height, box.depth)
   3007 result = font_output.get_results(box)

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/mathtext.pyc in parse(self, s, fonts_object, fontsize, dpi)
   2337 err.line,
   2338 " " * (err.column - 1) + "^",
-> 2339
                         six.text_type(err)]))

   2340 self._state_stack = None
   2341 self._em_width_cache = {}

ValueError
:
$alpha$
^
Expected end of text (at char 0), (line:1, col:1)

I think you either need to escape the \ as \ or use a raw string .i.e do r’$\alpha$’ otherwise \a gets interpreted as control character.

try entering ‘\alpha’ in a regular python prompt and you will se what happens.

Best

Jens

tir. 28. jul. 2015 kl. 16.01 skrev Joao Quinta da Fonseca <Joao.Fonseca@…1203…>:

···

I am trying to use LaTeX on the ylabel of a plot using:

plt.ylabel("$\alpha$”)

and I am seeing very inconsistent behaviour. If I use $\alpha$ I get an error (see below), but \gamma is fine. $\tau does not give an error but plots a strange character.

Can you help? thanks

Joao

Using $\alpha$ like above gives a long error message:


ValueError

                            Traceback (most recent call last)

/Users/joao/anaconda/lib/python2.7/site-packages/IPython/core/formatters.pyc in call(self, obj)

328                 pass

329             else:

–> 330 return printer(obj)

331             # Finally look for special method names

332             method = _safe_get_formatter_method(obj, self.print_method)

/Users/joao/anaconda/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in (fig)

205

206     if 'png' in formats:

–> 207 png_formatter.for_type(Figure, lambda fig: print_figure(fig, ‘png’, **kwargs))

208     if 'retina' in formats or 'png2x' in formats:

209         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

/Users/joao/anaconda/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in print_figure(fig, fmt, bbox_inches, **kwargs)

115

116     bytes_io = BytesIO()

–> 117 fig.canvas.print_figure(bytes_io, **kw)

118     data = bytes_io.getvalue()

119     if fmt == 'svg':

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)

2156 orientation=orientation,

2157 dryrun=True,

-> 2158

                 **kwargs)

2159 renderer = self.figure._cachedRenderer

2160 bbox_inches = self.figure.get_tightbbox(renderer)

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs)

519

520     def print_png(self, filename_or_obj, *args, **kwargs):

–> 521 FigureCanvasAgg.draw(self)

522         renderer = self.get_renderer()

523         original_dpi = renderer.dpi

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in draw(self)

467

468         try:

–> 469 self.figure.draw(self.renderer)

470         finally:

471             RendererAgg.lock.release()

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)

 57     def draw_wrapper(artist, renderer, *args, **kwargs):

 58         before(artist, renderer)

—> 59 draw(artist, renderer, *args, **kwargs)

 60         after(artist, renderer)

 61

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/figure.pyc in draw(self, renderer)

1083 dsu.sort(key=itemgetter(0))

1084 for zorder, a, func, args in dsu:

-> 1085 func(*args)

1086

1087 renderer.close_group(‘figure’)

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)

 57     def draw_wrapper(artist, renderer, *args, **kwargs):

 58         before(artist, renderer)

—> 59 draw(artist, renderer, *args, **kwargs)

 60         after(artist, renderer)

 61

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/axes/_base.pyc in draw(self, renderer, inframe)

2108

2109 for zorder, a in dsu:

-> 2110 a.draw(renderer)

2111

2112 renderer.close_group(‘axes’)

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)

 57     def draw_wrapper(artist, renderer, *args, **kwargs):

 58         before(artist, renderer)

—> 59 draw(artist, renderer, *args, **kwargs)

 60         after(artist, renderer)

 61

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/axis.pyc in draw(self, renderer, *args, **kwargs)

1126 self._update_label_position(ticklabelBoxes, ticklabelBoxes2)

1127

-> 1128 self.label.draw(renderer)

1129

1130 self._update_offset_text_position(ticklabelBoxes, ticklabelBoxes2)

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)

 57     def draw_wrapper(artist, renderer, *args, **kwargs):

 58         before(artist, renderer)

—> 59 draw(artist, renderer, *args, **kwargs)

 60         after(artist, renderer)

 61

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/text.pyc in draw(self, renderer)

593         renderer.open_group('text', self.get_gid())

594

–> 595 bbox, info, descent = self._get_layout(renderer)

596         trans = self.get_transform()

597

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/text.pyc in _get_layout(self, renderer)

318

             w, h, d = renderer.get_text_width_height_descent(clean_line,



319                                                         self._fontproperties,

–> 320

                                                     ismath=ismath)



321             else:

322                 w, h, d = 0, 0, 0

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in get_text_width_height_descent(self, s, prop, ismath)

226         if ismath:

227             ox, oy, width, height, descent, fonts, used_characters = \

–> 228 self.mathtext_parser.parse(s, self.dpi, prop)

229             return width, height, descent

230

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/mathtext.pyc in parse(self, s, dpi, prop)

3003 self.class._parser = Parser()

3004

-> 3005 box = self._parser.parse(s, font_output, fontsize, dpi)

3006 font_output.set_canvas_size(box.width, box.height, box.depth)

3007 result = font_output.get_results(box)

/Users/joao/anaconda/lib/python2.7/site-packages/matplotlib/mathtext.pyc in parse(self, s, fonts_object, fontsize, dpi)

2337 err.line,

2338 " " * (err.column - 1) + “^”,

-> 2339

                     six.text_type(err)]))

2340 self._state_stack = None

2341 self._em_width_cache = {}

ValueError

:

$ lpha$

^

Expected end of text (at char 0), (line:1, col:1)



Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

This is to do with how Python handles strings and is not a matplotlib issue. The \ is an escape character in Python strings but only for certain letters e.g. \t is a tab character etc:

In [3]: print(’$\tau$’)
$ au$

In [4]: print(’$\alpha$’)
$lpha$

\g is not an escape so:

In [5]: print(’$\gamma$’)
$\gamma$

To include slashes in your string you either need to double them up or use raw strings:

In [8]: print(r’$\alpha$’)
$\alpha$

In [9]: print(’$\alpha$’)
$\alpha$

···

On Tue, 28 Jul 2015 at 16:01 Joao Quinta da Fonseca <Joao.Fonseca@…1203…> wrote:

I am trying to use LaTeX on the ylabel of a plot using:

plt.ylabel("$\alpha$”)

and I am seeing very inconsistent behaviour. If I use $\alpha$ I get an error (see below), but \gamma is fine. $\tau does not give an error but plots a strange character.
Can you help? thanks

Oscar

That makes sense. Thank you.

João

···

On 28 Jul 2015, at 16:08, Oscar Benjamin <oscar.j.benjamin@...287...> wrote:

On Tue, 28 Jul 2015 at 16:01 Joao Quinta da Fonseca <Joao.Fonseca@...1203...> wrote:
I am trying to use LaTeX on the ylabel of a plot using:

plt.ylabel("$\alpha$”)

and I am seeing very inconsistent behaviour. If I use $\alpha$ I get an error (see below), but \gamma is fine. $\tau does not give an error but plots a strange character.

Can you help? thanks

This is to do with how Python handles strings and is not a matplotlib issue. The \ is an escape character in Python strings but only for certain letters e.g. \t is a tab character etc:

In [3]: print('$\tau$')
$ au$

In [4]: print('$\alpha$')
$lpha$

\g is not an escape so:

In [5]: print('$\gamma$')
$\gamma$

To include slashes in your string you either need to double them up or use raw strings:

In [8]: print(r'$\alpha$')
$\alpha$

In [9]: print('$\\alpha$')
$\alpha$

--
Oscar

Have u tried ?

plt.ylabel(r'$\alpha$')

···

--
View this message in context: http://matplotlib.1069221.n5.nabble.com/Tex-error-in-plot-label-tp45964p45979.html
Sent from the matplotlib - users mailing list archive at Nabble.com.