How to use UTF-8 for labels on Windows XP?

Hi,

How can I use font in my locale or even better UTF-8 for plot
annotations?

Right now I can see only empty rectangles instead characters when I try
to enter anything other then ASCII

Thanks

Hi,

try putting an r in front of your string:
e.g. label=r'äöü߀'

And it might help to tell your editor the used encoding, too, by
putting this as the first line:
# -*- coding: utf-8 -*-

Hope this helps,
Daniel
2011/6/9 Klonuo Umom <klonuo@...287...>:

···

Hi,

How can I use font in my locale or even better UTF-8 for plot
annotations?

Right now I can see only empty rectangles instead characters when I try
to enter anything other then ASCII

Thanks

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I use IPython (as pylab), and Qt4Agg as mpl backend

I was entering annotations in Qt4 'Figure Options' dialog

However, even if I try:

···

=======================================
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel(r'*some non ASCII chars*’)
plt.show()
---------------------------------------

I don't get anything, no even empty rectangles

If I use:

=======================================
plt.ylabel('ASCII text')
---------------------------------------

then I see y axis annotated with ASCII text

On 09.06.2011 16:49:13 Daniel Mader wrote:

Hi,

try putting an r in front of your string:
e.g. label=r'äöü߀'

And it might help to tell your editor the used encoding, too, by
putting this as the first line:
# -*- coding: utf-8 -*-

Hope this helps,
Daniel
2011/6/9 Klonuo Umom <klonuo@...287...>:
> Hi,
>
> How can I use font in my locale or even better UTF-8 for plot
> annotations?
>
> Right now I can see only empty rectangles instead characters when I try
> to enter anything other then ASCII
>
>
> Thanks
>
>
> ------------------------------------------------------------------------------
> EditLive Enterprise is the world's most technically advanced content
> authoring tool. Experience the power of Track Changes, Inline Image
> Editing and ensure content is compliant with Accessibility Checking.
> http://p.sf.net/sfu/ephox-dev2dev
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>

Here is trace if I run this UTF-8 encoded file:

···

=======================================
#-*- coding: utf-8 -*-
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel(r'Пајтон')
plt.show()
---------------------------------------

on command line:

=======================================
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\matplotlib\backends\backend_qt4.py", line 208, in resizeEvent
    self.draw()
  File "C:\Python26\lib\site-packages\matplotlib\backends\backend_qt4agg.py", line 130, in draw
    FigureCanvasAgg.draw(self)
  File "C:\Python26\lib\site-packages\matplotlib\backends\backend_agg.py", line 394, in draw
    self.figure.draw(self.renderer)
  File "C:\Python26\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python26\lib\site-packages\matplotlib\figure.py", line 798, in draw
    func(*args)
  File "C:\Python26\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python26\lib\site-packages\matplotlib\axes.py", line 1946, in draw
    a.draw(renderer)
  File "C:\Python26\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python26\lib\site-packages\matplotlib\axis.py", line 1032, in draw
    self.label.draw(renderer)
  File "C:\Python26\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python26\lib\site-packages\matplotlib\text.py", line 526, in draw
    bbox, info = self._get_layout(renderer)
  File "C:\Python26\lib\site-packages\matplotlib\text.py", line 305, in _get_layout
    clean_line, ismath = self.is_math_text(line)
  File "C:\Python26\lib\site-packages\matplotlib\text.py", line 989, in is_math_text
    if cbook.is_math_text(s):
  File "C:\Python26\lib\site-packages\matplotlib\cbook.py", line 1793, in is_math_text
    s = unicode(s)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)

Hi,

I just noticed this doesn't work here, too, as I expected :frowning:

with u'äöüß°€' I can print the string, but the labels are still broken
in the plot:

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])

xlabel = r'öäüß°€'
plt.xlabel(xlabel)
plt.show()
plt.savefig('asdf')

Would be interesting to know what's going on...

2011/6/9 Klonuo Umom <klonuo@...287...>:

···

Here is trace if I run this UTF-8 encoded file:

=======================================
#-*- coding: utf-8 -*-
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel(r'Пајтон')
plt.show()
---------------------------------------

on command line:

=======================================
Traceback (most recent call last):
File "C:\Python26\lib\site-packages\matplotlib\backends\backend_qt4.py", line 208, in resizeEvent
self.draw()
File "C:\Python26\lib\site-packages\matplotlib\backends\backend_qt4agg.py", line 130, in draw
FigureCanvasAgg.draw(self)
File "C:\Python26\lib\site-packages\matplotlib\backends\backend_agg.py", line 394, in draw
self.figure.draw(self.renderer)
File "C:\Python26\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Python26\lib\site-packages\matplotlib\figure.py", line 798, in draw
func(*args)
File "C:\Python26\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Python26\lib\site-packages\matplotlib\axes.py", line 1946, in draw
a.draw(renderer)
File "C:\Python26\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Python26\lib\site-packages\matplotlib\axis.py", line 1032, in draw
self.label.draw(renderer)
File "C:\Python26\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Python26\lib\site-packages\matplotlib\text.py", line 526, in draw
bbox, info = self._get_layout(renderer)
File "C:\Python26\lib\site-packages\matplotlib\text.py", line 305, in _get_layout
clean_line, ismath = self.is_math_text(line)
File "C:\Python26\lib\site-packages\matplotlib\text.py", line 989, in is_math_text
if cbook.is_math_text(s):
File "C:\Python26\lib\site-packages\matplotlib\cbook.py", line 1793, in is_math_text
s = unicode(s)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Yeah, that could be nice :slight_smile:

Out of curiosity I tried to complicate my life and use TeX for
annotation. It's great feature of course, it shows 'regular' text fine,
but still can't do UTF-8.

AFAIK I used this when working LaTeX in UTF-8:

···

On 09.06.2011 17:58:49 Daniel Mader wrote:

Hi,

I just noticed this doesn't work here, too, as I expected :frowning:

with u'äöüß°€' I can print the string, but the labels are still broken
in the plot:

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])

xlabel = r'öäüß°€'
plt.xlabel(xlabel)
plt.show()
plt.savefig('asdf')

Would be interesting to know what's going on...

=======================================
\usepackage[utf8] {inputenc}
\usepackage [T2A] {fontenc}
---------------------------------------

but can't now dig inside mpl code for any little detail, then try to
solve yet another...

IMHO this things should be more easier to set. I quickly browsed
matplolibrc, but can't see how I can solve this

I also use LaTeX with utf8 input encoding (and imho the required
preamble is fully OK since it is 100% LaTeX) when generating PDF
graphics -- but I do that only on a Linux box, so I can't verify for
Windows...

2011/6/9 Klonuo Umom <klonuo@...287...>:

···

AFAIK I used this when working LaTeX in UTF-8:

\usepackage[utf8] {inputenc}
\usepackage [T2A] {fontenc}
---------------------------------------

From: Daniel Mader [mailto:danielstefanmader@…982…]
Sent: Thursday, June 09, 2011 11:59

Hi,

I just noticed this doesn't work here, too, as I expected :frowning:

with u'äöüß°€' I can print the string, but the labels are still broken
in the plot:

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])

xlabel = r'öäüß°€'
plt.xlabel(xlabel)
plt.show()
plt.savefig('asdf')

Would be interesting to know what's going on...

What font was used for the label?

    >>> plt.gca().xaxis.label.get_fontname()
    'Bitstream Vera Sans'

The usual default above has a limited character set. You can select a font that has the characters you want using

    plt.xlabel(u'äöüß°€', fontdict={'family': 'Cambria'})

for a given bit of text or

    plt.rc('font', family='Cambria')

to make it the default.

Thanks Stan, you're 100% right! Usually I load a set of rcParams with
another font and that's why I don't have that problem:

## PNG output:
pylab.rcParams['text.usetex'] = False
## schöner sans-serif Font *ohne* LaTeX: 'stixsans'
pylab.rcParams['mathtext.fontset'] = 'stixsans'
## LaTeX-PDF output:
pylab.rcParams['text.usetex'] = True
pylab.rcParams['text.latex.unicode'] = True
pylab.rcParams['text.latex.preamble'] = [
    '\usepackage{lmodern}',
    '\usepackage[T1]{fontenc}',
    '\usepackage{sfmath}',
    '\usepackage{gensymb}',
    '\usepackage{amsmath}',
]

Thanks a lot for clarification!

2011/6/9 Stan West <stan.west@...706...>:

···

From: Daniel Mader [mailto:danielstefanmader@…982…]
Sent: Thursday, June 09, 2011 11:59

Hi,

I just noticed this doesn't work here, too, as I expected :frowning:

with u'äöüß°€' I can print the string, but the labels are still broken
in the plot:

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])

xlabel = r'öäüß°€'
plt.xlabel(xlabel)
plt.show()
plt.savefig('asdf')

Would be interesting to know what's going on...

What font was used for the label?

>>> plt.gca().xaxis.label.get_fontname()
'Bitstream Vera Sans'

The usual default above has a limited character set. You can select a font that has the characters you want using

plt.xlabel(u'äöüß°€', fontdict={'family': 'Cambria'})

for a given bit of text or

plt.rc('font', family='Cambria')

to make it the default.