|
From: Michael D. <md...@st...> - 2008-02-01 16:16:28
|
Unicode in Python is tricky. It is explained in gory detail here:
http://www.amk.ca/python/howto/unicode
But to save you the trouble of reading the whole thing, unless you're an
i18n geek like me, here's my list of recommendations to (somewhat)
reliably get non-ASCII characters to work in Python. There are other
ways, (and people tend to have their own opinions about this, mostly
based on the capabilities of their editors).
1. "Non-ASCII" means any codepoint that is greater than 127. Your
local encoding may have characters with accents in the range 128-255,
but that is *not* ASCII.
2. Always use Unicode literals for anything that contains non-ASCII
characters (by prefixing the literal with a 'u'). It is never a good
idea to put non-ASCII characters in a regular 8-bit Python string --
they do not "remember" their encoding, and the application has no way to
know how to interpret it.
3. Always declare an encoding at the top of the file, e.g.:
# -*- coding: utf-8 -*-
(I like utf-8 for generality, but you could use any of the
encodings that Python understands, see here:
http://docs.python.org/lib/standard-encodings.html)
4. Make sure your editor is correctly saving the file in that
specified encoding. This is perhaps the hardest step because editors
all handle it a little differently. Some editors have an option
somewhere to set the encoding that files are saved in. Others may
automatically understand the "coding" comment line in the file. (Same
goes for any terminal emulator you may be using for interactive plotting.)
If you can't get step 4 to work successfully, you can write Unicode
strings in Python using only ASCII characters using the "\u0000" escape
sequence.
u"Flamb\u00e9e"
(Here, the Unicode code point for e with accent-aigu is 00E9).
So, following all of the above, the attached works fine for me with .eps
output on 0.91.2. (There were various Unicode issues in 0.90.x that
were fixed.) If it still doesn't work for you, please let us know.
Cheers,
Mike
Matthieu Brucher wrote:
> No problem with the png backend.
>
> I tried with Latex for the accent, but it didn't work :
> Traceback (most recent call last):
> File "<input>", line 1, in <module>
> File
> "/home/brucher/local//lib/python2.5/site-packages/matplotlib/pyplot.py",
> line 265, in draw
> get_current_fig_manager().canvas.draw()
> File
> "/home/brucher/local//lib/python2.5/site-packages/matplotlib/backends/backend_wx.py",
> line 953, in draw
> self.figure.draw(self.renderer)
> File
> "/home/brucher/local//lib/python2.5/site-packages/matplotlib/figure.py",
> line 612, in draw
> for a in self.axes: a.draw(renderer)
> File
> "/home/brucher/local//lib/python2.5/site-packages/matplotlib/axes.py",
> line 1342, in draw
> a.draw(renderer)
> File
> "/home/brucher/local//lib/python2.5/site-packages/matplotlib/axis.py",
> line 593, in draw
> tick.draw(renderer)
> File
> "/home/brucher/local//lib/python2.5/site-packages/matplotlib/axis.py",
> line 170, in draw
> if self.label1On: self.label1.draw(renderer)
> File
> "/home/brucher/local//lib/python2.5/site-packages/matplotlib/text.py",
> line 775, in draw
> Text.draw(self, renderer)
> File
> "/home/brucher/local//lib/python2.5/site-packages/matplotlib/text.py",
> line 327, in draw
> self._fontproperties, angle)
> File
> "/home/brucher/local//lib/python2.5/site-packages/matplotlib/backend_bases.py",
> line 383, in draw_tex
> raise NotImplementedError
>
> I just did :
>
> import pylab as pl
> pl.plot([1, 2, 3], [1, 2, 3])
> pl.xlabel('éè')
> pl.show()
>
> and the ps file is corrupted :(
>
> Matthieu
>
> 2008/2/1, Michael Droettboom <md...@st... <mailto:md...@st...>>:
>
> Can you provide an example of your code? Often, it is a matter of
> configuring/using Python correctly to indicate accents. Is the problem
> only with EPS or other backends as well?
>
> Cheers,
> Mike
>
>
> Matthieu Brucher wrote:
> > Hi,
> >
> > I'm trying to export a MAtplotlib figure which has some axes labels,
> > such as 'coût'.
> > The problème is that the generated eps is corrupted because of these
> > accents. Is there a way to generate an acceptable eps file ?
> >
> > Matthieu
> > --
> > French PhD student
> > Website : http://matthieu-brucher.developpez.com/
> > Blogs : http://matt.eifelle.com and
> http://blog.developpez.com/?blog=92
> > LinkedIn : http://www.linkedin.com/in/matthieubrucher
> >
> >
>
> >
> ------------------------------------------------------------------------
> >
> >
> -------------------------------------------------------------------------
> > This SF.net email is sponsored by: Microsoft
> > Defy all challenges. Microsoft(R) Visual Studio 2008.
> > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> >
> >
> >
> ------------------------------------------------------------------------
> >
> > _______________________________________________
> > Matplotlib-users mailing list
> > Mat...@li...
> <mailto:Mat...@li...>
> > https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
>
> --
> Michael Droettboom
> Science Software Branch
> Operations and Engineering Division
> Space Telescope Science Institute
> Operated by AURA for NASA
>
>
>
>
> --
> French PhD student
> Website : http://matthieu-brucher.developpez.com/
> Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
> LinkedIn : http://www.linkedin.com/in/matthieubrucher
>
>
> ------------------------------------------------------------------------
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Matplotlib-users mailing list
> Mat...@li...
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA
|