how to use different font for serif

Hi all,

I've been trying to use a different serif font for a plot and have been
running into problems. I thought I could just do something like:

from matplotlib import rc
rc('font', family='serif', serif='Times New Roman')

but if I try that I end up getting:
findfont: Font family ['serif'] not found. Falling back to Bitstream
Vera Sans

It works fine without the serif='...' part and gives me the default
serif font. I know that Times New Roman exists on my system -- at least
the GNOME character map can find it. Perhaps I need to use a different
alias (but what would it be?). Any help would appreciated.

Jon

···

--
______________________________________________________________
Jonathan D. Slavin Harvard-Smithsonian CfA
jslavin@...1081... 60 Garden Street, MS 83
phone: (617) 496-7981 Cambridge, MA 02138-1516
cell: (781) 363-0035 USA
______________________________________________________________

Hi all,

I’ve been trying to use a different serif font for a plot and have been

running into problems. I thought I could just do something like:

from matplotlib import rc

rc(‘font’, family=‘serif’, serif=‘Times New Roman’)

but if I try that I end up getting:

findfont: Font family [‘serif’] not found. Falling back to Bitstream

Vera Sans

It works fine without the serif=’…’ part and gives me the default

serif font. I know that Times New Roman exists on my system – at least

the GNOME character map can find it. Perhaps I need to use a different

alias (but what would it be?). Any help would appreciated.

Jon

You should check what fonts are installed on your system:

from matplotlib import font_manager
font_manager.OSXInstalledFonts()

(or if you’re on a different system, try MSInstalledFonts or X11InstalledFonts—those aren’t available on my system, but presumably that’s just because I’m using OSX). If that works, then look for Times New Roman in what’s printed out. If it is, the problem may be that it’s not the right format: it appears as if the font_manager only supports .ttf and .afm fonts.

If you don’t see Times New Roman in any of those files, check the output of

mpl.font_manager.OSXFontDirectories

(replacing OSX with MS or X11, if needed). If the listed directories doesn’t match your installation of Times New Roman, that’s your problem. (I’m not sure if there’s a good way of adding directories.)

Cheers,
-Tony

···

On Fri, Dec 2, 2011 at 4:13 PM, Jonathan Slavin <jslavin@…1081…> wrote:

Tony,

Thanks for your answer. I'm running on CentOS Linux. Unfortunately
there is no such command as font_manager.X11InstalledFonts(), though
there is font_manager.x11FontDirectory(). When I do that I get a list
with a bunch of directories including '/usr/share/msttcorefonts', which
contains the times.ttf which has the Times New Roman font I wanted. And
by doing things like:
fprop = font_manager.FontProperties(fname=
        '/usr/share/fonts/msttcorefonts/times.ttf')
set_xlabel('X title',fontproperties=fprop)
xticklabels = plt.getp(plt.gca(), 'xticklabels')
plt.setp(xticklabels, fontproperties=fprop)

I can get the fonts I want for xlabel, ylabel, etc., but I still can't
get that font set as the default font (using, for example 'times' for
the value for serif, or for family). For some reason setting the
rcParam doesn't work. In the context in which it's invoked to find the
font, the font manager can't find it. So far I haven't been able to
track down the problem.

Jon

···

On Fri, 2011-12-02 at 17:30 -0500, Tony Yu wrote:

On Fri, Dec 2, 2011 at 4:13 PM, Jonathan Slavin > <jslavin@...1081...> wrote:
        Hi all,
        
        I've been trying to use a different serif font for a plot and
        have been
        running into problems. I thought I could just do something
        like:
        
        from matplotlib import rc
        rc('font', family='serif', serif='Times New Roman')
        
        but if I try that I end up getting:
        findfont: Font family ['serif'] not found. Falling back to
        Bitstream
        Vera Sans
        
        It works fine without the serif='...' part and gives me the
        default
        serif font. I know that Times New Roman exists on my system
        -- at least
        the GNOME character map can find it. Perhaps I need to use a
        different
        alias (but what would it be?). Any help would appreciated.
        
        Jon

You should check what fonts are installed on your system:

>>> from matplotlib import font_manager
>>> font_manager.OSXInstalledFonts()

(or if you're on a different system, try MSInstalledFonts or
X11InstalledFonts---those aren't available on my system, but
presumably that's just because I'm using OSX). If that works, then
look for Times New Roman in what's printed out. If it is, the problem
may be that it's not the right format: it appears as if the
font_manager only supports .ttf and .afm fonts.

If you don't see Times New Roman in any of those files, check the
output of

>>> mpl.font_manager.OSXFontDirectories

(replacing OSX with MS or X11, if needed). If the listed directories
doesn't match your installation of Times New Roman, that's your
problem. (I'm not sure if there's a good way of adding directories.)

Cheers,
-Tony

What rcParams are you setting?

  font.family:

serif

  font.serif: Times New Roman

  and

  font.family: Times New Roman

both work for me.

You have to use the name of the font as specified in the file, not

the filename to specify the font (which is probably why “times” is
not working for you).

Mike
···

<jslavin@…1081…>http://p.sf.net/sfu/splunk-novd2dMatplotlib-users@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/matplotlib-users