matplotlib, math greek letters and latex

Hi to the list,

I'm using matplotlib to generate graphs during my master thesis. I
want to thank all the developer for providing such flexible tool.

In some plots I have to put greek letters (or sometimes small fomulas)
as axis label. I've found three way to accomplish this:

1. Use the unicode string: xlabel(u'Wavelength [μm]')
2. Use mathtext: xlabel(r'$\rm{Wavelength [\mu m]}$')
3. Use usetex: rc(text, usetex=True), xlabel(r'Wavelength [$\rm{\mu m}$]')

I have some problem with each method (any hint is appreciated).

1. This method works ok as far as I choose a unicode font with the
greek letters, for example:

Sorry for the previous mail I've hit Send for error before the mail
was complete.

Here it is the complete mail:

Hi to the list,

I'm using matplotlib to generate graphs during my master thesis. I
want to thank all the developer for providing such flexible tool.

In some plots I have to put greek letters (or sometimes small formulas)
as axis label. I've found three way to accomplish this:

1. Use the unicode string: xlabel(u'Wavelength [μm]')
2. Use mathtext: xlabel(r'$\rm{Wavelength [\mu m]}$')
3. Use usetex: rc(text, usetex=True), xlabel(r'Wavelength [$\rm{\mu m}$]')

I have some problem with each method, any hint is appreciated.

1. This method works ok as far as I choose a unicode font with the
greek letters, for example:

  rcParams['font.serif'] = 'DejaVu Serif'

However with unicode strings I'm not able to do exponent and deponent
text (i.e cm^-1)

Methods 1. and 2. are more flexible, however I'm no able to to put the
\mu character in roman fonts. I don't know if this is the intended
behavior or if it's a bug...

Furthermore, is there a simple way to set fontsize for all text
elements (labels, title, ticklabels). Currently I'm doing:

rcParams['font.size'] = 12
rcParams['xtick.labelsize'] = 14
rcParams['axes.titlesize'] = 16
rcParams['axes.labelsize'] = 14

The relative setting (large, small,...) seems to refer always to the
default font size (set in matplotlibrc). Is there a way to set the
font.size property and then update all the other relative values to
reflect the change?

Many thanks for any help.

  ~ Antonio

You should be able to do super/subscripts in unicode. For example a
superscript '-' is unicode 207B. If you haven't already found it you
should check out:
http://unicode.org/charts/symbols.html
for more codes. However, the font you choose must support the unicode
(most only support a certain subset).

Regards,
John

···

On 13/03/07, Antonino Ingargiola <tritemio@...287...> wrote:

1. This method works ok as far as I choose a unicode font with the
greek letters, for example:

  rcParams['font.serif'] = 'DejaVu Serif'

However with unicode strings I'm not able to do exponent and deponent
text (i.e cm^-1)

What is the problem with method 3?

JDH

···

On 3/13/07, Antonino Ingargiola <tritemio@...287...> wrote:

1. Use the unicode string: xlabel(u'Wavelength [μm]')
2. Use mathtext: xlabel(r'\\rm\{Wavelength \[\\mu m\]\}')
3. Use usetex: rc(text, usetex=True), xlabel(r'Wavelength [\\rm\{\\mu m\}]')

I have some problem with each method (any hint is appreciated).

Hi Antonino,

If your using the version 0.90 (or SVN) of matplotlib you can also use
mathtext2.

To enable it, put these lines in your matplotlibrc file:
mathtext.mathtext2 : True
mathtext.nonascii : FreeSerif.ttf # Or any unicode font

or, you can set it for your particular script:
rcParams['mathtext.mathtext2'] = True
rcParams['mathtext.nonascii'] = 'FreeSerif.ttf'

You can also set:
mathtext.rm : FreeSerif.ttf # Roman font
mathtext.it : FreeSerifItalic.ttf # Text italic
mathtext.tt : FreeMono.ttf # Typewriter (monospaced)
mathtext.mit : FreeSerifItalic.ttf # Math italic
mathtext.cal : FreeSansOblique.ttf # Caligraphic

Beware that you have to have the freefonts installed on your system,
or in the mpl data-dir.

Best,
Edin

···

On 3/13/07, Antonino Ingargiola <tritemio@...287...> wrote:

Sorry for the previous mail I've hit Send for error before the mail
was complete.

Here it is the complete mail:

Hi to the list,

I'm using matplotlib to generate graphs during my master thesis. I
want to thank all the developer for providing such flexible tool.

In some plots I have to put greek letters (or sometimes small formulas)
as axis label. I've found three way to accomplish this:

1. Use the unicode string: xlabel(u'Wavelength [μm]')
2. Use mathtext: xlabel(r'\\rm\{Wavelength \[\\mu m\]\}')
3. Use usetex: rc(text, usetex=True), xlabel(r'Wavelength [\\rm\{\\mu m\}]')

I have some problem with each method, any hint is appreciated.

1. This method works ok as far as I choose a unicode font with the
greek letters, for example:

  rcParams['font.serif'] = 'DejaVu Serif'

However with unicode strings I'm not able to do exponent and deponent
text (i.e cm^-1)

Methods 1. and 2. are more flexible, however I'm no able to to put the
\mu character in roman fonts. I don't know if this is the intended
behavior or if it's a bug...

Furthermore, is there a simple way to set fontsize for all text
elements (labels, title, ticklabels). Currently I'm doing:

rcParams['font.size'] = 12
rcParams['xtick.labelsize'] = 14
rcParams['axes.titlesize'] = 16
rcParams['axes.labelsize'] = 14

The relative setting (large, small,...) seems to refer always to the
default font size (set in matplotlibrc). Is there a way to set the
font.size property and then update all the other relative values to
reflect the change?

Many thanks for any help.

  ~ Antonio
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Many thanks. Now I can do superscript and subscript (using GuCharMap I
can search in symbol description so it's easy to find them).

However the only font I know that has the superscript with the minus
sign is "DejaVu Sans". Anyone know a serif font (or also another sans
font) with all those symbols?

Cheers,

~ Antonio

PS: Sorry to John Hunter my previous private reply

···

On 3/13/07, John Travers <jtravs@...287...> wrote:

On 13/03/07, Antonino Ingargiola <tritemio@...287...> wrote:
> 1. This method works ok as far as I choose a unicode font with the
> greek letters, for example:
>
> rcParams['font.serif'] = 'DejaVu Serif'
>
> However with unicode strings I'm not able to do exponent and deponent
> text (i.e cm^-1)

You should be able to do super/subscripts in unicode. For example a
superscript '-' is unicode 207B. If you haven't already found it you
should check out:
Unicode 15.1 Character Code Charts
for more codes. However, the font you choose must support the unicode
(most only support a certain subset).

Hi Antonino,

If your using the version 0.90 (or SVN) of matplotlib you can also use
mathtext2.

To enable it, put these lines in your matplotlibrc file:
mathtext.mathtext2 : True
mathtext.nonascii : FreeSerif.ttf # Or any unicode font

or, you can set it for your particular script:
rcParams['mathtext.mathtext2'] = True
rcParams['mathtext.nonascii'] = 'FreeSerif.ttf'

Interesting, I will try it when I have time to fiddle with 0.90
installation (currently I have 0.87, included in ubuntu dapper).

You can also set:
mathtext.rm : FreeSerif.ttf # Roman font
mathtext.it : FreeSerifItalic.ttf # Text italic
mathtext.tt : FreeMono.ttf # Typewriter (monospaced)
mathtext.mit : FreeSerifItalic.ttf # Math italic
mathtext.cal : FreeSansOblique.ttf # Caligraphic

Beware that you have to have the freefonts installed on your system,
or in the mpl data-dir.

Nice, thanks for the imformation. Do they work in matplotlib 0.87 too?
(In my quick test they do not work).

Currently I'm not able to change mathtext or "usetex" fonts (except
for the font size). The font.serif, font.sans, etc... properties
change only the normal text font.

Furthermore I have noted that with "usetex" ticks label are rendered
differently if they are explicitly set with set_[xy]ticklabels() or
not. Compare the ytick labels (automatic) and xtick labels (manually
set) in the attached plot. You can see also that the title and x axis
label fonts are "boldier" (weightier) than the standard latex Computer
Modern fonts (the difference become evident when the image is included
in a latex document). This effect is what I was talking about in
previous mail.

Best,
Edin

Ciao,

~ Antonio

PS: Sorry to Edin Salkovic for previous private reply

···

On 3/13/07, Edin Salkovic <edin.salkovic@...287...> wrote:

> 1. Use the unicode string: xlabel(u'Wavelength [μm]')
> 2. Use mathtext: xlabel(r'\\rm\{Wavelength \[\\mu m\]\}')
> 3. Use usetex: rc(text, usetex=True), xlabel(r'Wavelength [\\rm\{\\mu m\}]')
>
> I have some problem with each method (any hint is appreciated).

What is the problem with method 3?

The \mu character is not in roman font but in italics, while the 'm'
is correctly set to roman. In latex I use the SIunits package to write
the units, so the fonts are correctly in roman even in math
environment. It would be useful something like that in matplotlib too.
However I'll be happy even if I could write greek characters in roman
fonts.

Maybe it's a problem related to the particular font used...

Furthermore font rendering it's a bit odd: the fonts appears to be
bolder than normal Computer Modern fonts used in the latex document.
I've tried to change the font weight (in matplotlibrc) without any
effect. On the contrary mathtext seems to use a font like Times, which
should be bolder, but the overall aspect is lighter.

How can I change the font used by 'mathtext' or 'usetex'? All I want
is use the standard ae fonts I'm using in latex.

Many thanks.

JDH

Cheers,

~ Antonio

···

On 3/13/07, John Hunter <jdh2358@...287...> wrote:

On 3/13/07, Antonino Ingargiola <tritemio@...287...> wrote:

[cut]

Furthermore I have noted that with "usetex" ticks label are rendered
differently if they are explicitly set with set_[xy]ticklabels() or
not. Compare the ytick labels (automatic) and xtick labels (manually
set) in the attached plot.

Now its attached, provided that attachments can reach the list.

  ~ Antonio

image.eps.bz2 (34.4 KB)

···

On 3/14/07, Antonino Ingargiola <tritemio@...287...> wrote: