[Matplotlib-users] 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 (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)
Re: [Matplotlib-users] Tex error in plot label
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.fons...@manchester.ac.uk>: > 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/
Re: [Matplotlib-users] Tex error in plot label
On Tue, 28 Jul 2015 at 16:01 Joao Quinta da Fonseca < joao.fons...@manchester.ac.uk> 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 -- ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Tex error in plot label
That makes sense. Thank you. João > On 28 Jul 2015, at 16:08, Oscar Benjamin wrote: > > > > On Tue, 28 Jul 2015 at 16:01 Joao Quinta da Fonseca > 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 -- ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
[Matplotlib-users] Using dates in a simple plot with wxPython doesn't work
I try to use a dates on the x-aches. With pure Python3 code it works fine. BUt when I try to use this inside wxPython application on a FigureCanvas it doesn't work. And I don't understand the difference here. This is the error [err] plot.plot([datetime.date(2015, 1, 7), TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int' [/err] This is the fine working pure Python3 code. [code] #!/usr/bin/env python3 # -*- coding: utf-8 -*- import matplotlib.pyplot as plt import datetime plt.plot([datetime.date(2015, 1, 7), datetime.date(2015, 2, 5), datetime.date(2015, 6, 2)], [70.3, 60.1, 68.8]) plt.show() [/code] This is the piece of wxPython code that cause the error [code] # -*- coding: utf-8 -*- import matplotlib.pyplot as pyplot from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas from datetime import datetime #... class StatisticTab(wx.Panel): def __init__(self, parent): super(StatisticTab, self).__init__(parent) panel = wx.Panel(self) fig = pyplot.figure() plot = self.fig.add_subplot(1,1,1) plot.plot([datetime.date(2015, 1, 7), datetime.date(2015, 2, 5), datetime.date(2015, 6, 2)], [70.3, 60.1, 68.8]) self.canvas = FigureCanvas(self, -1, fig) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(panel) self.SetSizer(sizer) [/code] -- ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Using dates in a simple plot with wxPython doesn't work
On 2015-07-28 10:31, (by way of c.bu...@posteo.jp) wrote: > I try to use a dates on the x-aches. With pure Python3 code it works > fine. BUt when I try to use this inside wxPython application on a > FigureCanvas it doesn't work. And I don't understand the difference > here. > > This is the error > [err] > plot.plot([datetime.date(2015, 1, 7), > TypeError: descriptor 'date' requires a 'datetime.datetime' object but > received a 'int' > [/err] > > This is the fine working pure Python3 code. > import datetime > This is the piece of wxPython code that cause the error > from datetime import datetime There is the problem. The error has nothing to do with matplotlib. In one case you did "import datetime" and imported the datetime library. In the other you did "from datetime import datetime", thus importing the datetime type from that library. If your first version is working, change the second version to use the same import. -- Brendan Barnwell "Do not follow where the path may lead. Go, instead, where there is no path, and leave a trail." --author unknown -- ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users