Using otf fonts in matplotlib

Hi,

I would like to use .otf fonts for typesetting text (axes, titles, labels,
legends, ...) in matplotlib. Is this possible? If yes, how?
If not directly, perhaps via XeTeX?

Cheers,
Andreas.

Hi,

I would like to use .otf fonts for typesetting text (axes, titles, labels,
legends, ...) in matplotlib. Is this possible? If yes, how?

Yes. Put the font somewhere in your font search path. (Where that would be depends on your platform, but for user-local fonts, use "~/.fonts" on Linux, "~/Library/Fonts" on OS-X, I don't recall on Windows, but Googling should give you something). You will need to delete the matplotlib font cache so that it will discover new fonts. This lives in "~/.matplotlib/fontList.cache" on Unix platforms. Then you can change the default font family by setting the "font.family" rcParam to the name of the font.

Note that matplotlib does not support all of the features of otf fonts, like glyph substitution etc. -- it really only uses the basic "TrueType-like" parts of the font file.

If not directly, perhaps via XeTeX?

This is another avenue, and has the potential to use the advanced features of otf. I've never attempted this myself, but perhaps one of the other users on this list who uses usetex more often can comment.

Mike

···

On 10/10/2011 08:25 AM, Andreas H. wrote:

I would like to use .otf fonts for typesetting text (axes, titles,
labels,
legends, ...) in matplotlib. Is this possible? If yes, how?

Yes. Put the font somewhere in your font search path. (Where that
would be depends on your platform, but for user-local fonts, use
"~/.fonts" on Linux, "~/Library/Fonts" on OS-X, I don't recall on
Windows, but Googling should give you something). You will need to
delete the matplotlib font cache so that it will discover new fonts.
This lives in "~/.matplotlib/fontList.cache" on Unix platforms. Then
you can change the default font family by setting the "font.family"
rcParam to the name of the font.

Cool, this seems to work :slight_smile:

Now, I only have one issue:

If I set a text, for example via

   ylabel(r"VCD$_{\sf strat}$ O$_{\sf 3}$ [DU]")

the subscript "strat" actually gets typeset in my OTF font, but the subset
"3" gets typeset in the standard math font (the fonts Cmr10 and Cmss10 get
embedded in the PDF file). This doesn't look nice, because I am using
MyriadPro, which is a sans-serif, while the CMR/CMSS fonts are serif fonts
...

Any ideas how to solve this?

Cheers!
Andreas.

Using anything but the CM and STIX fonts in mathtext ultimately leads to a world of pain and I consider it "unsupported", because there are custom tweaks to get the alignment working that end up being missing.

However, if you really want to try it you can set the following rcParams:

   mathtext.default: regular # Use the same font for math as regular text
   mathtext.fontset: custom # Don't pull any glyphs from cm or stix

Then you can write your string entirely in math syntax, eg.:

   r"$VCD_{strat} O_3 [DU]$"

Mike

···

On 10/10/2011 12:17 PM, Andreas H. wrote:

I would like to use .otf fonts for typesetting text (axes, titles,
labels,
legends, ...) in matplotlib. Is this possible? If yes, how?

Yes. Put the font somewhere in your font search path. (Where that
would be depends on your platform, but for user-local fonts, use
"~/.fonts" on Linux, "~/Library/Fonts" on OS-X, I don't recall on
Windows, but Googling should give you something). You will need to
delete the matplotlib font cache so that it will discover new fonts.
This lives in "~/.matplotlib/fontList.cache" on Unix platforms. Then
you can change the default font family by setting the "font.family"
rcParam to the name of the font.

Cool, this seems to work :slight_smile:

Now, I only have one issue:

If I set a text, for example via

    ylabel(r"VCD$_{\sf strat}$ O$_{\sf 3}$ [DU]")

the subscript "strat" actually gets typeset in my OTF font, but the subset
"3" gets typeset in the standard math font (the fonts Cmr10 and Cmss10 get
embedded in the PDF file). This doesn't look nice, because I am using
MyriadPro, which is a sans-serif, while the CMR/CMSS fonts are serif fonts
...

Any ideas how to solve this?

Cheers!
Andreas.

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users